You are on page 1of 4

How to create PDF file from Visual C++: "Hello, PDF!

" example
This tutorial will teach you how to generate simplePDFdocument from VC++ using PDF Creator Pilot library. (1)InstallPDF Creator Pilotlibrary on your computer and runVisual C++ (2)Select the "New.." command from "File" menu (as it is shown below) to run Project Wizard :

(3)New Project Wizard dialog will appear on the screen. Select "Win32 Console Application" project type and enter "HelloPDF" as the Project name as shown on the screenshot below:

(4)Wizard will ask what kind of Console Application you want to create. Select"Hello, World!" application"project.

ClickFinishto close Wizard and VC++ will generate application code withmain()function. Code editor window will appear. To editmain()function just open the class tree on the left and double click on "HelllPDF Classes" | " Globals" | "main (int argc, char* argv[1])" and you will get the code edit window as it’s shown on the screenshot below:

(5)To use PDF Creator Pilot from our application you have to tell the compiler to import library information in the project. To do this, you just have to add#importcompiler directive after#includedirective: #import"PDFCreatorPilot.dll" using namespacePDFCreatorPilot; and thenHelloPDF.cppwill look like this:

(6)The code snippet that creates PDF document is quite simple and its algorithm is quite simple too. To create PDF file, you have to do the following things: 1. connect to the PDF Creator Pilot library 2. set the filename for the PDF file; 3. draw "Hello, PDF!" message on the document; 4. disconnect from the library. You will need to add code which generates PDF file into themain()function. Here is the full code ofmain()function: intmain(intargc,char* argv[]) { // initialize OLE HRESULT hr = CoInitialize(NULL); // check for errors if(FAILED(hr)) { MessageBox(0,"OLE initialization errp","error",MB_OK); return-1; }; // declare PDF Creator Pilot object IpiPDFDocument* PDF = NULL; CLSID clsid; // get inuque ID for PDF Creator Pilot

hr = CLSIDFromProgID(OLESTR("PDFCreatorPilot.piPDFDocument"), &clsid); // check for errors if(FAILED(hr)) { MessageBox(0,"Can't get CLSID for PDF Creator Pilot","error",MB_OK); gotoUninit; }; // create PDF Creator Pilot object hr = CoCreateInstance(clsid, NULL, CLSCTX_ALL,__uuidof(IpiPDFDocument), (LPVOID*)&PDF); // check for errors if(FAILED(hr)) { MessageBox(0,"Can't create PDF Creator Pilot object","error",MB_OK); gotoUninit; }; // initialize PDF Engine PDF->StartEngine("demo","demo"); // set PDF ouput filename PDF->FileName = "HelloPDF_VC.PDF"; PDF->AutoLaunch =true;// auto-open generated pdf document // start document generation PDF->BeginDoc(); // draw "HELLO, PDF" message on the current PDF page PDF->PDFPAGE_BeginText(); // set active font name, parameters, size and charset PDF->PDFPAGE_SetActiveFont("Verdana",true,false,false,false, 14, charsetANSI_CHARSET); // draw text PDF->PDFPAGE_TextOut(10, 20, 0, "HELLO, PDF!"); PDF->PDFPAGE_EndText(); // finalize document generation PDF->EndDoc(); // disconnect from library PDF->Release(); // uninitialize OLE libraries Uninit: CoUninitialize(); return0; } This code will generate PDF file that will be saved using "HelloPDF_VC.PDF"filename Hint: you can simply copy the code from the sample above and then paste it in the VC++ code editor as shown on the screenshot below:

(7)Run the application by pressingF5(you can also use "Build" | "Debug" | "Go")

VC++ will compile and run "Hello, PDF!" application. It will generateHelloPDF_VC.PDFfile and the library will open it in the default PDF Viewer if you have any installed on your computer (for example,Adobe Acrobat Reader):

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