Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) Add File Attachments to a PDFSee more PDF Signatures ExamplesDemonstrates 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.
integer li_rc oleobject loo_Pdf integer li_Success oleobject loo_Json integer i oleobject loo_Bd // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. loo_Pdf = create oleobject // Use "Chilkat_9_5_0.Pdf" for versions of Chilkat < 10.0.0 li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf") if li_rc < 0 then destroy loo_Pdf MessageBox("Error","Connecting to COM object failed") return end if // 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. li_Success = loo_Pdf.LoadFile("qa_data/pdf/helloWorld.pdf") if li_Success = 0 then Write-Debug loo_Pdf.LastErrorText destroy loo_Pdf return end if // 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. loo_Json = create oleobject // Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0 li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject") loo_Json.EmitCompact = 0 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. loo_Json.I = i loo_Json.UpdateString("files[i].description","Hello World") loo_Json.UpdateString("files[i].localFilePath","qa_data/xml/helloWorld.xml") i = i + 1 // Specify the 2nd file to be attached. loo_Json.I = i loo_Json.UpdateString("files[i].description","Image of starfish") loo_Json.UpdateString("files[i].localFilePath","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. loo_Json.UpdateString("files[i].subType","image/jpeg") i = i + 1 // You can alternatively provide the file data from base64 encoded data rather than from a local file. loo_Json.I = i loo_Json.UpdateString("files[i].description","Hello World from Data") loo_Json.UpdateString("files[i].fileData","SGVsbG8gV29ybGQsIEhlbGxvIFdvcmxkLCBIZWxsbyBXb3JsZCwgSGVsbG8gV29ybGQsIEhlbGxvIFdvcmxk") loo_Json.UpdateString("files[i].embeddedFilename","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. loo_Json.I = i loo_Json.UpdateString("files[i].description","Invoice") loo_Json.UpdateString("files[i].localFilePath","qa_data/xml/invoice.xml") loo_Json.UpdateString("files[i].embeddedFilename","invoice_1234.xml") loo_Json.UpdateString("files[i].subType","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. li_Success = loo_Pdf.AddEmbeddedFiles(loo_Json,"qa_output/helloWorld_withAttachments.pdf") if li_Success = 0 then Write-Debug loo_Pdf.LastErrorText destroy loo_Pdf destroy loo_Json return end if // You can alternatively write the PDF file with attachments to a Chilkat BinData object.. loo_Bd = create oleobject // Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 li_rc = loo_Bd.ConnectToNewObject("Chilkat.BinData") li_Success = loo_Pdf.AddEmbeddedFilesBd(loo_Json,loo_Bd) if li_Success = 0 then Write-Debug loo_Pdf.LastErrorText destroy loo_Pdf destroy loo_Json destroy loo_Bd return end if // Then you can do what you want with the BinData, which contains the binary image of the PDF with attachments. Write-Debug "Success." destroy loo_Pdf destroy loo_Json destroy loo_Bd |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.