Linking to the Database

A document created with the PDFCreator can be linked to a database. Information can be retrieved from a table or a view to populate various fields of the document.

The database can be of type Microsoft Access®(.MDB) database, or an ODBC source. The .MDB file name or ODBC source should be specified in the "DataSource" property of the Document object. The main table or view from which to retrieve data should be specified in the "DataTableOrView" property of the Document object.

A field or a cell can contain an expression derived from a database by using the following syntax:

TableName.FieldName

Here is a sample showing how to create an object linked to a database:

Example

Download the test.mdb from this link and save it in c:\temp

Sub Sample()

    ' Constants for Activation codes

    Const strLicenseTo As String = "Amyuni PDF Creator Evaluation"

    Const strActivationCode As String = "07EFCDAB010001004282943F2AF19A88F332D9E781E40460727DF8A42847A1BDE06DB61C71E94E2D90424BF8762385335F9D6884E9FC"

 

    ' Initializing PDFCreativeX Object

    Dim pdf As ACPDFCREACTIVEX.PDFCreactiveX = New ACPDFCREACTIVEX.PDFCreactiveX()

 

    ' Set license key

    pdf.SetLicenseKey(strLicenseTo, strActivationCode)

 

    ' set the data source to an Access file

    pdf.DataSource = "c:\temp\test.mdb"

 

    ' set the main table

    pdf.DataTableOrView = "DOCUMENTS"

 

    ' Create a Field in the first page

    pdf.CurrentPage = 1

    pdf.CreateObject(ACPDFCREACTIVEX.ObjectTypeConstants.acObjectTypeField, "Field1")

 

    ' Define Object

    Dim oField As ACPDFCREACTIVEX.IacObject = pdf.GetObjectByName("Field1")

    ' General Attributes

    oField("Left") = 1000

    oField("Right") = 3250

    oField("Top") = 3000

    oField("Bottom") = 3500

    ' set object value to be the field TITLE from the table DOCUMENTS

    oField("Text") = "=DOCUMENTS.TITLE"

 

    ' compile the document and switch to Run mode. this will fill

    ' the "Field 1" object with the first record of the table each

    ' time this method is called, a new value is fetched from the table

    pdf.ReportState = ACPDFCREACTIVEX.ReportStateConstants.acReportStateRun

 

    ' Save PDF

    pdf.Save("c:\temp\CreatePDFDocument_resulting.pdf", ACPDFCREACTIVEX.FileSaveOptionConstants.acFileSaveView)

 

    ' destroy objects

    pdf = Nothing

    oField = Nothing

End Sub

static void Sample()

{

    // Constants for Activation codes

    const string strLicenseTo = "Amyuni PDF Creator Evaluation";

    const string strActivationCode = "07EFCDAB010001004282943F2AF19A88F332D9E781E40460727DF8A42847A1BDE06DB61C71E94E2D90424BF8762385335F9D6884E9FC";

 

    // Initializing PDFCreativeX Object

    ACPDFCREACTIVEX.PDFCreactiveX pdf = new ACPDFCREACTIVEX.PDFCreactiveX();

 

    // Set license key

    pdf.SetLicenseKey(strLicenseTo, strActivationCode);

 

    // set the data source to an Access file

    pdf.DataSource = @"c:\temp\test.mdb";

 

    // set the main table

    pdf.DataTableOrView = "DOCUMENTS";

 

    // Create a Field in the first page

    pdf.CurrentPage = 1;

    pdf.CreateObject(ACPDFCREACTIVEX.ObjectTypeConstants.acObjectTypeField, "Field1");

    // Define Object

    ACPDFCREACTIVEX.IacObject oField = pdf.GetObjectByName("Field1".ToString());

    // General Attributes

    oField["Left"] = 1000;

    oField["Right"] = 3250;

    oField["Top"] = 3000;

    oField["Bottom"] = 3500;

    // set object value to be the field TITLE from the table DOCUMENTS

    oField["Text"] = "=DOCUMENTS.TITLE";

 

    // compile the document and switch to Run mode. this will fill

    // the "Field 1" object with the first record of the table each

    // time this method is called, a new value is fetched from the table

    pdf.ReportState = ACPDFCREACTIVEX.ReportStateConstants.acReportStateRun;

 

    // Save PDF

    pdf.Save(@"c:\temp\CreatePDFDocument_resulting.pdf", ACPDFCREACTIVEX.FileSaveOptionConstants.acFileSaveView);

 

    // destroy objects

    pdf = null;

    oField = null;

}

#import "c:\users\amyuni\pdfcreactivex.dll" no_namespace

 

using namespace std;

 

int main()

{

    // Constants for Activation codes

    bstr_t strLicenseTo = "Amyuni PDF Creator Evaluation";

    bstr_t strActivationCode = "07EFCDAB010001004282943F2AF19A88F332D9E781E40460727DF8A42847A1BDE06DB61C71E94E2D90424BF8762385335F9D6884E9FC";

 

    // Initialize the COM subsystem

    CoInitialize(0);

 

    // IPDFCreactiveXPtr is a smart pointer type defined in pdfcreactivex.tlh,

    // the type library header file generated by the #import instruction above

    IPDFCreactiveXPtr pdf;

 

    // Create the PDFCreactiveX instance

    pdf.CreateInstance(__uuidof(PDFCreactiveX));

 

    // set license key

    pdf->SetLicenseKey(_bstr_t(strLicenseTo), _bstr_t(strActivationCode));

 

    // set the data source to an Access file

    pdf->DataSource = "c:\\temp\\test.mdb";

 

    // set the main table

    pdf->DataTableOrView = "DOCUMENTS";

 

    // Create a Field in the first page

    pdf->CurrentPage = 1;

    pdf->CreateObject(acObjectTypeField, "Field1");

    // Define Object

    IacObjectPtr oField = pdf->GetObjectByName("Field1");

    // General Attributes

    oField->Attribute["Left"] = 1000;

    oField->Attribute["Right"] = 3250;

    oField->Attribute["Top"] = 3000;

    oField->Attribute["Bottom"] = 3500;

    // set object value to be the field TITLE from the table DOCUMENTS

    oField->Attribute["Text"] = "=DOCUMENTS.TITLE";

 

    // Switch to run mode after objects to the document, the attribute Text will evaluate and save it in Value attribute

    pdf->ReportState = acReportStateRun;

 

    // Save PDF

    pdf->Save("c:\\temp\\CreatePDFDocument_resulting.pdf", acFileSaveView);

 

    // destroy objects

    pdf = NULL;

    oField = NULL;

 

    return 0;

}

' ReportStateConstants

Const acReportStateRun = 0

Const acReportStateDesign = 1

Const acReportStateLoading = 2

Const acReportStatePrintPreview = 3

Const acReportStateAnnotate = 4

 

' ObjectTypeConstants

Const acObjectTypeArrow = 36

Const acObjectTypeBarcode = 45

Const acObjectTypeCell = 33

Const acObjectTypeCheckBox = 21

Const acObjectTypeEllipse = 4

Const acObjectTypeExcel = 17

Const acObjectTypeField = 6

Const acObjectTypeGraph = 19

Const acObjectTypeHighlight = 23

Const acObjectTypeLine = 1

Const acObjectTypePicture = 7

Const acObjectTypePolygon = 8

Const acObjectTypeRoundFrame = 3

Const acObjectTypeRow = 32

Const acObjectTypeSelection = 24

Const acObjectTypeSignature = 39

Const acObjectTypeStickyNote = 22

Const acObjectTypeTable = 16

Const acObjectTypeText = 5

 

' FileSaveOptionConstants

Const acFileSaveAll = 0

Const acFileSaveDefault = -1

Const acFileSaveView = 1

Const acFileSaveDesign = 2

Const acFileSavePDFA_7 = 3

Const acFileSavePDFA = 4

Const acFileSavePDF14 = 5

 

' Constants for Activation codes

Const strLicenseTo = "Amyuni PDF Creator Evaluation"

Const strActivationCode = "07EFCDAB010001004282943F2AF19A88F332D9E781E40460727DF8A42847A1BDE06DB61C71E94E2D90424BF8762385335F9D6884E9FC"

 

' Initializing PDFCreativeX Object

Dim pdf

Set pdf = CreateObject("PDFCreactiveX.PDFCreactiveX.6.5")

 

' Set license key

pdf.SetLicenseKey strLicenseTo, strActivationCode

 

' set the data source to an Access file

pdf.DataSource = "c:\temp\test.mdb"

 

' set the main table

pdf.DataTableOrView = "DOCUMENTS"

 

' Create a Field in the current Page

pdf.CreateObject acObjectTypeField, "Field1"

' Define Object

Dim oField 

Set oField = pdf.GetObjectByName("Field1")

' General Attributes

oField("Left") = 1000

oField("Right") = 3250

oField("Top") = 3000

oField("Bottom") = 3500

' set object value to be the field TITLE from the table DOCUMENTS

oField("Text") = "=DOCUMENTS.TITLE"

 

' compile the document and switch to Run mode. this will fill

' the "Field 1" object with the first record of the table each

' time this method is called, a new value is fetched from the table

pdf.ReportState = acReportStateRun

 

' Save PDF

pdf.Save "c:\temp\CreatePDFDocument_resulting.pdf", acFileSaveView

 

' destroy Objects

Set pdf = Nothing

Set oField = Nothing