Unicode C
Unicode C
Add File Attachments to a PDF
See more PDF Signatures Examples
Demonstrates how to attach files to a PDF. This is also known as embedding a file within a PDF.Note: This example requires Chilkat v9.5.0.97 or greater.
Chilkat Unicode C Downloads
#include <C_CkPdfW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkBinDataW.h>
void ChilkatSample(void)
{
BOOL success;
HCkPdfW pdf;
HCkJsonObjectW json;
int i;
HCkBinDataW bd;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
pdf = CkPdfW_Create();
// We'll be adding attachments to this PDF file..
// Note: We are loading the PDF file into memory. The PDF file is not kept open at this point.
success = CkPdfW_LoadFile(pdf,L"qa_data/pdf/helloWorld.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
return;
}
// Note: The ability to attach files to a PDF was added in Chilkat v9.5.0.97
// Build JSON to provide information about the files to be embedded in the PDF.
json = CkJsonObjectW_Create();
CkJsonObjectW_putEmitCompact(json,FALSE);
i = 0;
// We can attach multiple files in one operation.
// Here we specify the local relative file path of the 1st file to be embedded.
CkJsonObjectW_putI(json,i);
CkJsonObjectW_UpdateString(json,L"files[i].description",L"Hello World");
CkJsonObjectW_UpdateString(json,L"files[i].localFilePath",L"qa_data/xml/helloWorld.xml");
i = i + 1;
// Specify the 2nd file to be attached.
CkJsonObjectW_putI(json,i);
CkJsonObjectW_UpdateString(json,L"files[i].description",L"Image of starfish");
CkJsonObjectW_UpdateString(json,L"files[i].localFilePath",L"qa_data/jpg/starfish.jpg");
// You can explicitly specify the subType (i.e. MIME type) of the file.
// Otherwise Chilkat will use the default MIME type based on the filename extension.
// If no default MIME type exists, then "application/octet-stream" is used.
CkJsonObjectW_UpdateString(json,L"files[i].subType",L"image/jpeg");
i = i + 1;
// You can alternatively provide the file data from base64 encoded data rather than from a local file.
CkJsonObjectW_putI(json,i);
CkJsonObjectW_UpdateString(json,L"files[i].description",L"Hello World from Data");
CkJsonObjectW_UpdateString(json,L"files[i].fileData",L"SGVsbG8gV29ybGQsIEhlbGxvIFdvcmxkLCBIZWxsbyBXb3JsZCwgSGVsbG8gV29ybGQsIEhlbGxvIFdvcmxk");
CkJsonObjectW_UpdateString(json,L"files[i].embeddedFilename",L"abc.txt");
i = i + 1;
// By default, the filename used within the PDF is the filename part of the local file path.
// You can change it by specifying the embeddedFilename.
CkJsonObjectW_putI(json,i);
CkJsonObjectW_UpdateString(json,L"files[i].description",L"Invoice");
CkJsonObjectW_UpdateString(json,L"files[i].localFilePath",L"qa_data/xml/invoice.xml");
CkJsonObjectW_UpdateString(json,L"files[i].embeddedFilename",L"invoice_1234.xml");
CkJsonObjectW_UpdateString(json,L"files[i].subType",L"application/xml");
// The above JSON provides instructions for attaching 4 files to the PDF.
// Let's attach the files..
// It is perfectly acceptable to write over the PDF file that was loaded,
// but in this example we'll write the PDF with attachments to a new PDF file.
success = CkPdfW_AddEmbeddedFiles(pdf,json,L"qa_output/helloWorld_withAttachments.pdf");
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
return;
}
// You can alternatively write the PDF file with attachments to a Chilkat BinData object..
bd = CkBinDataW_Create();
success = CkPdfW_AddEmbeddedFilesBd(pdf,json,bd);
if (success == FALSE) {
wprintf(L"%s\n",CkPdfW_lastErrorText(pdf));
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkBinDataW_Dispose(bd);
return;
}
// Then you can do what you want with the BinData, which contains the binary image of the PDF with attachments.
wprintf(L"Success.\n");
CkPdfW_Dispose(pdf);
CkJsonObjectW_Dispose(json);
CkBinDataW_Dispose(bd);
}