How to create PDF file with Delphi

187 downloads 1100 Views 1MB Size Report
Delphi will suggest you to install PDF Creator Pilot into the user component ... 7) Double-click theButton1button on the form and code editor window will appear:.
How to create PDF file with Delphi: "Hello, PDF!" example This page contains step by step tutorial how to createPDFwith Delphi using PDF Creator Pilot library. 1)InstallPDF Creator Pilotlibrary on your computer and runDelphi. 2)First, you have to add PDF Creator Pilot library as a component to the component palette. Select "Project" | "Import Type Library" command as shown below:

3)Import dialog will appear. Find and selectPDF Creator Pilotin the libraries list and press "Install...":

Delphi will suggest you to install PDF Creator Pilot into the user component package and Install dialog will appear:

PressOKand confirm it by pressingYes:

Dephi will recompile user components package and PDF Creator Pilot will be installed as theTpiPDFDocumentcomponent on the "ActiveX"components page.

4)Now we will create new application. Go to the "File" menu and select "New Application" command.

5)New application project will be created. Go to components panel and clickButtoncontrol icon on the palette:

Then click on the form and Delphi will create and place theButton1object on the form as shown on the screenshot below:

6)Now selectActiveXpage on the comonents palette, selectpiPDFDocumentcomponent: (this component was generated automatically for PDF Creator Pilot on steps 2 and 3).

Then click on the form and Delphi will create and placepiPDFDocument1object on the form. This object will control PDF Creator Pilot library. 7)Double-click theButton1button on the form and code editor window will appear:

8)Code snippet for theTForm1.Button1Clickprocedure is shown below. This code is quite simple and its algorithm is quite simple too. To create PDF file you have to do the following things: 1.

initialize PDF Creator Pilot PDF engine;

2.

set the filename for the PDF file;

3.

draw "Hello, PDF!" message on the PDF document;

4.

disconnect from the library.

procedureTForm1.Button1Click(Sender: TObject); begin // initialize PDF Engine piPDFDocument1.StartEngine('demo@demo', 'demo'); // set PDF output filename piPDFDocument1.FileName := 'HelloPDF_DELPHI.pdf'; // set option to auto-open generated pdf document piPDFDocument1.AutoLaunch := True; // start document generation piPDFDocument1.BeginDoc; // draw 'HELLO, PDF' message on the current PDF page piPDFDocument1.PDFPAGE_BeginText; piPDFDocument1.PDFPAGE_SetActiveFont('Verdana', True, False, False, False, 14, charsetANSI_CHARSET); piPDFDocument1.PDFPAGE_TextOut(10, 20, 0, 'HELLO, PDF!'); piPDFDocument1.PDFPAGE_EndText; // finalize document generation piPDFDocument1.EndDoc; end; This code will create PDF file "HelloPDF_Delphi.PDF": You can simply copy this listed code to the clipboard and paste it in the Delphi code editor as it is shown below:

9)Now clickF9to compile and run application. Delphi will runProject1andForm1will appear. Click theButton1and application will create "Hello, PDF!" document. If you have PDF viewer installed (for example,Adobe Acrobat Reader), then application will openHelloPDF_DELPHI.PDFfile as it shown below:

You can find the source code of this example in the"\Examples\Delphi\"sub-folder.