AddFileAttachment, DocAddFileAttachment

The AddFileAttachment and DocAddFileAttachment add a file attachment to the current document.

 

Syntax

ActiveX:

System.Boolean AddFileAttachment(System.String Name, System.String Filename, System.String Description, System.String ContentType)

DLL:

int DocAddFileAttachment(EXTDOCHANDLE edhDocument, LPCSTR Name, LPCSTR Filename, LPCSTR Description, LPCSTR ContentType)

 

Parameters

Name

Name of the object that is stored in the PDF Names dictionary, but it is not the "Display Name".  This allows to locate the object in the PDF file.

Filename

Path of the file to attach.

Description

Text that describes the file to attach.  Can be an empty string.

ContentType

If specified, it is embedded as it is inside the pdf. If not, the content-type is looked up from the mime types registered in the system, according to the extension of the attached file. It is in the form of "image/jpeg" "image/png", "application/pdf", etc.

edhDocument

Handle Returned by DocOpen.

 

Return Value

The return value is True if the AddFileAttachment method succeed. Otherwise, False If the AddFileAttachment method fails.

The return value is zero if the DocAddFileAttachment method succeed. If the DocAddFileAttachment method fails, a negative value will returned.

 

Remarks

The AddFileAttachment and DocAddFileAttachment will mark the relationship of the file attachment to the main document as Unspecified.  Please, use AddFileAttachmentEx/DocAddFileAttachmentEx to configure other relationships.

 

From the PDF specs 1.7, section 3.6.3, we have that some categories of objects in a PDF file can be referred to by Name rather than by Object Reference. The correspondence between names and objects is established by the document’s name dictionary (PDF 1.2), located by means of the Names entry in the document’s catalog (see Section 3.6.1, “Document Catalog”).

 

So if we use “amyuni.jpg” for the Name parameter.  For instance, using PDF Creator, we can do:

pdf.GetObjectByName("Attachments[amyuni.jpg]")

and this will return the Attachment object directly rather than having to loop through all the attachments.  

 

The Name that is displayed by Acrobat® or other readers depends on the reader, there is nothing in the specs that specifies a “Display Name” for file attachments.

 

Member of CDIntfEx.Document.

 

Example

Private Sub Sample()

    ' Constants for Activation codes

    Const strLicenseTo As String = "Amyuni PDF Converter Evaluation"

    Const strActivationCode As String = "07EFCDAB0100010025AFF1801CB9441306C5739F7D452154D8833B9CECBA2ADE79E3762A69FFC354528A5F4A5811BE3204A0A439F5BA"

 

    ' Declare a new cdintfex document if it does not exist in the form.

    Dim pdfDoc As New CDIntfEx.Document

 

    ' The SetLicenseKey method should be called after creating an object of type 

    ' CDIntfEx.Document to activate the advanced methods that require the object 

    ' activation code to work properly

    pdfDoc.SetLicenseKey(strLicenseTo, strActivationCode)

 

    ' Open the document

    pdfDoc.Open("c:\temp\test.pdf")

 

    ' Add files attachments to the current document

    pdfDoc.AddFileAttachment("photo.jpg", "c:\temp\photo.jpg", "Portrait Photo", "image/jpeg")

 

    ' Save the document

    pdfDoc.Save("c:\temp\WithAttachment.pdf")

End Sub

private void Sample()

{

    // Constants for Activation codes

    const string strLicenseTo = "Amyuni PDF Converter Evaluation";

    const string strActivationCode = "07EFCDAB0100010025AFF1801CB9441306C5739F7D452154D8833B9CECBA2ADE79E3762A69FFC354528A5F4A5811BE3204A0A439F5BA";

 

    // Declare a new cdintfex document if it does not exist in the form.

    CDIntfEx.Document pdfDoc = new CDIntfEx.Document();

 

    // The SetLicenseKey method should be called after creating an object of type 

    // CDIntfEx.Document to activate the advanced methods that require the object 

    // activation code to work properly

    pdfDoc.SetLicenseKey(strLicenseTo, strActivationCode);

 

    // Open the document

    pdfDoc.Open("c:\\temp\\test.pdf");

 

    // Add files attachments to the current document

    pdfDoc.AddFileAttachment("photo.jpg", "c:\\temp\\photo.jpg", "Portrait Photo", "image/jpeg");

 

    // Save the document

    pdfDoc.Save("c:\\temp\\WithAttachment.pdf");

}

// PDF Converter Cpp.cpp : Defines the entry point for the console application.

// 

#include <Windows.h>

#include <string>

#include <iostream>

#include "CdIntf.h"

#pragma comment (lib, "CDIntf.lib")

 

using namespace std;

int main()

{

     // Constants for Activation codes

    #define strLicenseTo  (LPCSTR)"Amyuni PDF Converter Evaluation"

    #define strActivationCode (LPCSTR)"07EFCDAB0100010025AFF1801CB9441306C5739F7D452154D8833B9CECBA2ADE79E3762A69FFC354528A5F4A5811BE3204A0A439F5BA"

 

    // Declare a new cdintfex document if it does not exist in the form.

    EXTDOCHANDLE pdfDoc;

 

    // The SetLicenseKey method should be called after creating an object of type 

    // CDIntfEx.Document to activate the advanced methods that require the object 

    // activation code to work properly

    SetLicenseKeyA(strLicenseTo, strActivationCode);

 

    // Open the document

    LPBYTE passWord = nullptr;

    DocOpenA(&pdfDoc, (LPCSTR)"c:\\temp\\test.pdf", passWord);

 

    // Add files attachments to the current document

    DocAddFileAttachmentA(pdfDoc,"photo.jpg", "c:\\temp\\photo.jpg", "Portrait Photo", "image/jpeg");

 

    // Save the document

    DocSaveA(pdfDoc, (LPCSTR)"c:\\temp\\WithAttachment.pdf");

 

    // Destroy pdfDoc object

    DocClose(pdfDoc);

    pdfDoc = nullptr;    

 

    return 0;

}

 

import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.Dispatch;

import com.jacob.com.Variant;

 

public class Sample {

    public static void main(String[] args)

    {

        // Constants for Activation codes

        String strLicenseTo  = "Amyuni PDF Converter Evaluation";

        String strActivationCode = "07EFCDAB0100010025AFF1801CB9441306C5739F7D452154D8833B9CECBA2ADE79E3762A69FFC354528A5F4A5811BE3204A0A439F5BA";

 

        // Declare a new cdintfex document if it does not exist in the form.

        ActiveXComponent pdfDoc = new ActiveXComponent("CDIntfEx.Document.6.5"); 

 

        // The SetLicenseKey method should be called after creating an object of type 

        // CDIntfEx.Document to activate the advanced methods that require the object 

        // activation code to work properly

        Dispatch.call(pdfDoc, "SetLicenseKey", strLicenseTo, strActivationCode);

 

        // Open the document

        Dispatch.call(pdfDoc, "Open", "c:\\temp\\test.pdf");

 

        // Add files attachments to the current document

        Dispatch.call(pdfDoc, "AddFileAttachment", "photo.jpg", "c:\\temp\\photo.jpg", "Portrait Photo", "image/jpeg");

 

        // Save the document

        Dispatch.call(pdfDoc, "Save", "c:\\temp\\WithAttachment.pdf");

 

         // Destroy pdfDoc object

        pdfDoc = null;

    }

}

# Constants for Activation codes

$strLicenseTo  =  "Amyuni PDF Converter Evaluation"

$strActivationCode = "07EFCDAB0100010025AFF1801CB9441306C5739F7D452154D8833B9CECBA2ADE79E3762A69FFC354528A5F4A5811BE3204A0A439F5BA"

 

#Declare a new cdintfex document if it does not exist in the form.

$pdfDoc = New-Object -ComObject CDIntfEx.Document.6.5

 

#The SetLicenseKey method should be called after creating an object of type 

#CDIntfEx.Document to activate the advanced methods that require the object 

#activation code to work properly

[System.__ComObject].InvokeMember('SetLicenseKey', [System.Reflection.BindingFlags]::InvokeMethod,$null,$pdfDoc, @($strLicenseTo, $strActivationCode))

 

#Open the document

[System.__ComObject].InvokeMember('Open', [System.Reflection.BindingFlags]::InvokeMethod,$null,$pdfDoc,"c:\temp\test.pdf") 

 

#Add files attachments to the current document

[System.__ComObject].InvokeMember('AddFileAttachment', [System.Reflection.BindingFlags]::InvokeMethod,$null,$pdfDoc,

    @("photo.jpg", "c:\temp\photo.jpg", "Portrait Photo", "image/jpeg")) 

 

#Save the document

[System.__ComObject].InvokeMember('Save', [System.Reflection.BindingFlags]::InvokeMethod,$null,$pdfDoc,"c:\temp\WithAttachment.pdf")

 

#Destroy pdfDoc object

$pdfDoc = $null

' Constants for Activation codes

Const strLicenseTo = "Amyuni PDF Converter Evaluation"

Const strActivationCode = "07EFCDAB0100010025AFF1801CB9441306C5739F7D452154D8833B9CECBA2ADE79E3762A69FFC354528A5F4A5811BE3204A0A439F5BA"

 

' Declare a new cdintfex document if it does not exist in the form.

Dim pdfDoc 

Set pdfDoc = CreateObject("CDIntfEx.Document.6.5")

 

' The SetLicenseKey method should be called after creating an object of type 

' CDIntfEx.Document to activate the advanced methods that require the object 

' activation code to work properly

pdfDoc.SetLicenseKey strLicenseTo, strActivationCode

 

' Open the document

pdfDoc.Open "c:\temp\test.pdf"

 

' Add files attachments to the current document

pdfDoc.AddFileAttachment "photo.jpg", "c:\temp\photo.jpg", "Portrait Photo", "image/jpeg"

 

' Save the document

pdfDoc.Save "c:\temp\WithAttachment.pdf"

 

' Destroy pdfDoc object

Set pdfDoc = Nothing