Components for Developers © 2000-2008, Two Pilots

PDF Library Download Features Manual Tutorials FAQ Pricing HTML2PDF Add-on History In the Lab

PDF version of this article is available.
Click here to download.

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

Step by Step:

 

This page contains a step-by-step tutorial that will teach you how to create simple PDF files from VC++ using PDF Creator Pilot library.

1) Install PDF Creator Pilot library on your computer and run Visual C++

2) Select the "New.." command from "File" menu (as it is shown below) to run Project Wizard :

VC++ File menu

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:

Select type of new project

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

Win32 Console Application type dialog

Click Finish to close Wizard and VC++ will generate application code with main() function

Code editor window will appear. To edit main() 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:

VC++ code editor and class explorer view

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 #import compiler directive after #include directive:

  #import "PDFCreatorPilot3.dll"
  using namespace PDFCreatorPilot3Lib;

and then HelloPDF.cpp will look like this:

HelloPDF.cpp in the VC++ code editor

 

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 the main() function. Here is the full code of main() function:



int main(int argc, 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
IPDFDocument3* PDF = NULL;
CLSID clsid;

// get inuque ID for PDF Creator Pilot
hr = CLSIDFromProgID(OLESTR("PDFCreatorPilot3.PDFDocument3"), &clsid);

// check for errors
if (FAILED(hr)) {
MessageBox(0,"Can't get CLSID for PDF Creator Pilot","error",MB_OK);
goto Uninit;
};

// create PDF Creator Pilot object
hr = CoCreateInstance(clsid, NULL, CLSCTX_ALL,__uuidof(IPDFDocument3), (LPVOID*)&PDF);

// check for errors
if (FAILED(hr)) {
MessageBox(0,"Can't create PDF Creator Pilot object","error",MB_OK);
goto Uninit;
};

// 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
// 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!");

// finalize document generation
PDF->EndDoc();

// disconnect from library
PDF->Release();

// uninitialize OLE libraries
Uninit:
CoUninitialize();

return 0;
}

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:

Select main() function code

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

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

HelloPDF_VC.PDF in the Adobe Reader window

You can download the source code for the described "Hello, PDF!" application here.

 

PDF Creator Pilot can be used with different languages to generate PDF files. See "Hello, PDF" example for:

See also:

 

If you have questions related to PDF Creator Pilot, contact us via our online support form.

PDF Library Download Features Manual Tutorials FAQ Pricing HTML2PDF Add-on History In the Lab

 

 

PDF Library | Virtual Printer | Converters to PDF

Support | Blog | Forum | Contacts | Site map

© 2000-2008, Two Pilots