DataFlex
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoPdf
Variant vJson
Handle hoJson
Integer i
Variant vBd
Handle hoBd
String sTemp1
Move False To iSuccess
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Get Create (RefClass(cComChilkatPdf)) To hoPdf
If (Not(IsComObjectCreated(hoPdf))) Begin
Send CreateComObject of hoPdf
End
// 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.
Get ComLoadFile Of hoPdf "qa_data/pdf/helloWorld.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// 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.
Get Create (RefClass(cComChilkatJsonObject)) To hoJson
If (Not(IsComObjectCreated(hoJson))) Begin
Send CreateComObject of hoJson
End
Set ComEmitCompact Of hoJson To False
Move 0 To i
// We can attach multiple files in one operation.
// Here we specify the local relative file path of the 1st file to be embedded.
Set ComI Of hoJson To i
Get ComUpdateString Of hoJson "files[i].description" "Hello World" To iSuccess
Get ComUpdateString Of hoJson "files[i].localFilePath" "qa_data/xml/helloWorld.xml" To iSuccess
Move (i + 1) To i
// Specify the 2nd file to be attached.
Set ComI Of hoJson To i
Get ComUpdateString Of hoJson "files[i].description" "Image of starfish" To iSuccess
Get ComUpdateString Of hoJson "files[i].localFilePath" "qa_data/jpg/starfish.jpg" To iSuccess
// 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.
Get ComUpdateString Of hoJson "files[i].subType" "image/jpeg" To iSuccess
Move (i + 1) To i
// You can alternatively provide the file data from base64 encoded data rather than from a local file.
Set ComI Of hoJson To i
Get ComUpdateString Of hoJson "files[i].description" "Hello World from Data" To iSuccess
Get ComUpdateString Of hoJson "files[i].fileData" "SGVsbG8gV29ybGQsIEhlbGxvIFdvcmxkLCBIZWxsbyBXb3JsZCwgSGVsbG8gV29ybGQsIEhlbGxvIFdvcmxk" To iSuccess
Get ComUpdateString Of hoJson "files[i].embeddedFilename" "abc.txt" To iSuccess
Move (i + 1) To i
// 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.
Set ComI Of hoJson To i
Get ComUpdateString Of hoJson "files[i].description" "Invoice" To iSuccess
Get ComUpdateString Of hoJson "files[i].localFilePath" "qa_data/xml/invoice.xml" To iSuccess
Get ComUpdateString Of hoJson "files[i].embeddedFilename" "invoice_1234.xml" To iSuccess
Get ComUpdateString Of hoJson "files[i].subType" "application/xml" To iSuccess
// 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.
Get pvComObject of hoJson to vJson
Get ComAddEmbeddedFiles Of hoPdf vJson "qa_output/helloWorld_withAttachments.pdf" To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// You can alternatively write the PDF file with attachments to a Chilkat BinData object..
Get Create (RefClass(cComChilkatBinData)) To hoBd
If (Not(IsComObjectCreated(hoBd))) Begin
Send CreateComObject of hoBd
End
Get pvComObject of hoJson to vJson
Get pvComObject of hoBd to vBd
Get ComAddEmbeddedFilesBd Of hoPdf vJson vBd To iSuccess
If (iSuccess = False) Begin
Get ComLastErrorText Of hoPdf To sTemp1
Showln sTemp1
Procedure_Return
End
// Then you can do what you want with the BinData, which contains the binary image of the PDF with attachments.
Showln "Success."
End_Procedure