API Reference For Application Communication
API Reference For Application Communication
No part of this document can be reproduced, transferred, distributed or stored in any format without the prior written
permission of Foxit.
Permission to copy, use, modify, sell and distribute this software is granted provided this copyright notice appears in all
copies. This software is provided "as is" without express or implied warranty, and with no claim as to its suitability for any
purpose.
Contents
Contents ........................................................................................................... 3
Preface ............................................................................................................. 5
What’s in this guide?........................................................................................................................................... 5
PhantomPDF.Creator ..................................................................................................................................... 11
ConvertToPDF .......................................................................................................................................................................... 12
CombineFiles ............................................................................................................................................................................ 12
PhantomPDF.Document ............................................................................................................................... 13
Export ........................................................................................................................................................................................ 13
Save ........................................................................................................................................................................................... 14
RotatePage ............................................................................................................................................................................... 14
GetFieldValue ........................................................................................................................................................................... 14
SetFieldValue............................................................................................................................................................................ 15
AddWatermark ........................................................................................................................................................................ 16
OCRAndExportToExcel ............................................................................................................................................................ 16
PhantomPDF.Page ......................................................................................................................................... 17
PhantomPDF.WatermarkElementInfo ........................................................................................................ 17
Serialize ..................................................................................................................................................................................... 19
PhantomPDF.CombineFlags ......................................................................................................................... 19
COMBINE_DEFAULT ................................................................................................................................................................ 19
COMBINE_STOP_CONVERTFAIL ............................................................................................................................................ 20
COMBINE_ADD_CONTENTS ................................................................................................................................................... 20
COMBINE_TRAVEL_SUBDIR ................................................................................................................................................... 20
You can use all the APIs easily if you are familiar with VBA or C++.
Related documentation
For information about See
FoxitPhantomPDF_Manual.pdf FoxitPhantomPDF83_Manual.pdf
OLE Automation
This chapter describes the objects, data types, and methods in the OLE automation interface.
The name PhantomPDF.Application is the external strings OLE clients use to create object of application type.
For convenience, the PhantomPDF developer type libraries also call it PhantomPDF.Application.
For all the other automation objects (for example, Document and Creator), you can get them through
PhantomPDF.Application.
The following table summarizes the available objects and data types.
Object Description
Methods
The Application object has the following methods.
Method Description
Properties
The Application object has the following Properties.
Property Description
Creator Creator object that is used to convert non-pdf to PDF, read only.
Syntax
HRESULT CloseDocument(IDocument* doc, VARIANT_BOOL bPromptToSave);
Parameters
doc The document to be closed.
Return value
S_OK if successful, S_FALSE if not.
See Also
CreateBlankDoc
OpenDiskFileAsMenPDF
GetDocument
OpenDocument
CreateBlankDoc
Create a blank document.
Syntax
IDocument* CreateBlankDoc(FLOAT fWidth, FLOAT fHeight);
Parameters
fWidth Width, the unit is inches. (0.14~200).
Return value
Pointer of Document if successful, NULL if failed.
See Also
CloseDocument
OpenDiskFileAsMenPDF
GetDocument
OpenDocument
Exit
Exit PhantomPDF. You should close all the documents before exiting PhantomPDF. The function will return a
value immediately after sending the exit command to PhantomPDF.
Syntax
HRESULT Exit();
Return value
S_OKif exit command is sent to PhantomPDF, S_FAILif not.
GetDocument
Get a Document object by page index.
Syntax
IDocument* GetDocument(SHORT index);
Parameters
index Page index, beginning with 0.
Return value
Pointer of Document if successful, NULL if failed.
See Also
CloseDocument
OpenDiskFileAsMenPDF
CreateBlankDoc
OpenDocument
IsValidDocument
Check whether a Document object is valid.
Syntax
VARIANT_BOOL IsValidDocument(IDocument* doc);
Parameters
doc A Document object.
Return value
-1 if the Document object is valid, 0 if not.
See Also
CloseDocument
OpenDiskFileAsMenPDF
GetDocument
OpenDocument
OpenDiskFileAsMemPDF
Open a PDF document as a memory document in PhantomPDF.
Syntax
IDocument* OpenDiskFileAsMemPDF(BSTR PDFFilePath, BSTR Title);
Parameters
PDFFilePath The PDF document Path.
Return value
Pointer of Document if successful, NULL if failed.
See Also
CloseDocument
CreateBlankDoc
GetDocument
OpenDocument
OpenDocument
Open a PDF document in PhantomPDF.
Syntax
IDocument* OpenDocument(BSTR PDFFilePath, BSTR Password, VARIANT_BOOL bMakeVisible, VARIANT_BOOL
bAddToMRU);
Parameters
PDFFilePath The PDF document Path.
Password If the document does not have a password, you should set it to an empty
string.
Return value
Pointer of Document if successful, NULL if failed.
See Also
CloseDocument
OpenDiskFileAsMenPDF
GetDocument
CreateBlankDoc
CreateWatermarkElementInfo
Create a WatermarkElementInfo object.
Syntax
IWatermarkElementInfo* CreateWatermarkElementInfo();
Return value
A IWatermarkElementInfo object.
See Also
Document.AddWatermark
PhantomPDF.Creator
The Creator object is used to convert non-pdf to PDF.
Methods
The Creator object has the following methods.
Method Description
ConvertToPDF
Convert non-pdf to PDF.
Syntax
HRESULT ConvertToPDF(BSTR bstrSrcPathName, BSTR bstrDestPathName);
Parameters
bstrSrcPathName The source document path.
Return value
S_OKif successful, S_FAILif not.
See Also
CombineFiles
CombineFiles
Combine multiple documents to a PDF.
Syntax
SHORT CombineFiles(BSTR bstrFiles, BSTR DestPDFFile, SHORT uCombineFlags);
Parameters
bstrFiles The source files.
Return value
Number of combined files, less than zero if there are any errors.
Remarks
The parameter bstrFiles can be either a folder path or a string that is made from different file paths
joining together and between each file path separated by '|'.
The flag COMBINE_TRAVEL_SUBDIR of uCombineFlags can be ignored if bstrFiles is not a folder path.
See Also
ConvertToPDF
PhantomPDF.Document
Represent a document that is currently opened in PhantomPDF.
Methods
The Document object has the following methods.
Method Description
Export
Export the document to disk.
Syntax
HRESULT Export(BSTR SaveAsPath);
Parameters
SaveAsPath The dest pdf path.
Return value
S_OKif successful, S_FAILif not.
See Also
Save
Save
Save the document.
Syntax
HRESULT Save();
Return value
S_OK if successful, S_FAIL if not.
See Also
Export
RotatePage
Rotate specified pages.
Syntax
HRESULT RotatePage(SHORT nPage, SHORT nRotate);
Parameters
nPage The pages index that begins with 0.
nRotate nRotate , 0 for 0°, 1 for 90°, 2 for 180°, -1 for -90°
Return value
S_OKif successful, S_FAILif not.
Remarks
The parameter nRotate is only 0, 1, 2 or -1, and any other value will result in failure.
GetFieldValue
Get the value of a specified form.
Syntax
BSTR GetFieldValue(BSTR FieldName);
Parameters
FieldName The form name.
Return value
Value of specified form.
Remarks
At present for the form types supported, see the following:
Text Field
Push Button
List Box
Combo Box
Radio Button
Check Box
See Also
SetFieldValue
SetFieldValue
Set the value of a specified form.
Syntax
HRESULT SetFieldValue(BSTR FieldName, BSTR value);
Parameters
FieldName The form name.
Return value
S_OKif successful, S_FAILif not.
Remarks
At present for the form types supported, see the following:
Text Field
Push Button
List Box
Combo Box
Radio Button
Check Box
See Also
GetFieldValue
AddWatermark
Add a watermark.
Syntax
HRESULT AddWatermark(IWatermarkElementInfo* pWatermarkInfo);
Parameters
pWatermarkInfo A watermark information object.
Return value
S_OKif successful, S_FAILif not.
Remarks
The parameter pWatermarkInfo is always created through calling Application.CreateWatermarkElementInfo,
See Also
Application.CreateWatermarkElementInfo
OCRAndExportToExcel
OCR and export the document to an Excel document.
Syntax
HRESULT OCRAndExportToExcel(BSTR ExcelPathname, SHORT start, SHORT end, VARIANT_BOOL bAllPages,
VARIANT_BOOL bText);
Parameters
ExcelPathname An Excel Pathname.
Return value
S_OKif successful, S_FAILif not.
Remarks
The parameter ExcelPathname must add .xlsx as suffix, otherwise it will automatically append .xlsx as suffix. The
parameters Start and End both begin with 1, and will both be ignored if bAllPages is true.
If ExcelPathname already exists, the old file will be replaced with a new generated excel file directly, and you need
to pay attention to it.
See Also
Export
PhantomPDF.Page
Represent a Page in a Document. Not yet supported.
PhantomPDF.WatermarkElementInfo
Represent a data type of watermark info. WatermarkElementInfo is usually a parameter of the interface
Document.AddWatermark.
Methods
The WatermarkElementInfo object has the following methods.
Method Description
Properties
The WatermarkElementInfo object has the following Properties.
Property Description
WMScale Scale relative to the target page, (-1 ~ 1). -1 means no scale, and
FontSize will be ignored if WMScale greater than or equal to zero.
Start The Start page that needs watermark, with the page index beginning
with 1.
End The End page that needs watermark, with the page index beginning
with 1.
Even The even pages between the Start page and the End page that need
watermark.
Odd The odd pages between the Start page and the End page that need
watermark.
WMFilePageIndex The page index of the Watermark file. Beginning from 1. Ignored if Type
is 0.
Remarks
When the properties Start and End are -1 at the same time, it will mean all pages, and without doubt
that the properties Even and Odd are still working.
For VBA code, the property TextColorRef can be assigned to &H0, &HFFFFFF.For more details of
common color, please see the following table.
Serialize
Serialized as a string.
Syntax
BSTR Serialize();
Return value
A string representing the WatermarkElementInfo.
Remarks
In general, you don’t need to care the interface. But if you want to save a WatermarkElementInfo
permanently, serializing it as a string is a great way.
PhantomPDF.CombineFlags
An enum use for Creator.CombineFiles’s parameter uCombineFlags.
Constants
The PhantomPDF.CombineFlags enum has the following constants.
Constants value
COMBINE_DEFAULT 0x00
COMBINE_STOP_CONVERTFAIL 0x01
COMBINE_ADD_CONTENTS 0x02
COMBINE_TRAVEL_SUBDIR 0x04
COMBINE_DEFAULT
0x00, Default.
Remarks
Skip the error file and continue. Don't build the contents index page. Don't traverse subdirectory.
COMBINE_STOP_CONVERTFAIL
0x01, Abort the combination and exit the function if any error occurs.
COMBINE_ADD_CONTENTS
0x02, Add contents index to the first page.
COMBINE_TRAVEL_SUBDIR
0x04, Traverse subdirectory.
Demo for VBA
The following are some sample VBA codes you can use for your application.
If bValidDoc Then
Call phRotateDoc.RotatePage(0, 1)
phRotateDoc.Export ("D: \rotateDest.pdf")
End If
phApp.Exit
Example 2 Adding a text watermark to the center of all pages
Dim phWmInfo As PhantomPDF.WatermarkElementInfo
Set phWmInfo = phApp.CreateWatermarkElementInfo()
phWmInfo.Type = 0
phWmInfo.WMText = "Foxit PhantomPDF"
phWmInfo.FontName = "Helvetiva"
phWmInfo.FontSize = 24
phWmInfo.TextColorRef = &H0
phWmInfo.Rotation = 0
phWmInfo.Opacity = 1
phWmInfo.WMScale = 1
phWmInfo.Top = True
phWmInfo.VerticalDistance = 0
phWmInfo.VerticalDistanceFrom = 1
phWmInfo.HorizontalDistance = 0
phWmInfo.HorizontalDistanceFrom = 1
phWmInfo.Start = -1
phWmInfo.End = -1
phWmInfo.Even = True
phWmInfo.Odd = True
Call phRotateDoc.AddWatermark(phWmInfo)
phRotateDoc.Export ("D:\watermark.pdf")
Example 3 Adding an image watermark to the center of all
pages
Dim phWmInfo As PhantomPDF.WatermarkElementInfo
Set phWmInfo = phApp.CreateWatermarkElementInfo()
phWmInfo.Type = 1
phWmInfo.WMFile = "D:\image.png"
phWmInfo.Rotation = 0
phWmInfo.Opacity = 1
phWmInfo.WMScale = 1
phWmInfo.Top = True
phWmInfo.VerticalDistance = 0
phWmInfo.VerticalDistanceFrom = 1
phWmInfo.HorizontalDistance = 0
phWmInfo.HorizontalDistanceFrom = 1
phWmInfo.Start = -1
phWmInfo.End = -1
phWmInfo.Even = True
phWmInfo.Odd = True
phWmInfo.WMFilePageIndex = 1
Call phRotateDoc.AddWatermark(phWmInfo)
phRotateDoc.Export ("D:\watermark.pdf ")
Example 4 Converting non-PDF to PDF
'Text Field
Dim strTextField0 As String
strTextField0 = phFormDoc.GetFieldValue("Text Field0")
Call phFormDoc.SetFieldValue("Text Field0", "sally zhong for Testing")
strTextField0 = phFormDoc.GetFieldValue("Text Field0")
'Push Button
Dim strPushButton1 As String
strPushButton1 = phFormDoc.GetFieldValue("Push Button1")
Call phFormDoc.SetFieldValue("Push Button1", "I am Button")
strPushButton1 = phFormDoc.GetFieldValue("Push Button1")
'Check Box
Dim strCheckBox0 As String
strCheckBox0 = phFormDoc.GetFieldValue("Check Box0")
'Radio Button
Dim strRadioButton0 As String
strRadioButton0 = phFormDoc.GetFieldValue("Radio Button0")
'List Box
Dim strListBox0 As String
strListBox0 = phFormDoc.GetFieldValue("List Box0")
Call phFormDoc.SetFieldValue("List Box0", " List Data1")
strListBox0 = phFormDoc.GetFieldValue("List Box0")
Call phFormDoc.SetFieldValue("List Box0", "out of term")
strListBox0 = phFormDoc.GetFieldValue("List Box0")
phFormDoc.Save
Call phApp.CloseDocument(phFormDoc, False)
Appendix 1 Last-Error Code List
The Last-Error Code is an integer that you can use to know about reasons for errors when calling the API fails. You can
get a LastError value by the following code.
EC_OK 0 OK
EC_CANCEL 1 Cancel