Skip to content

First Steps In Creating a PDF file

galkahana edited this page Aug 15, 2011 · 7 revisions

All usages of the PDF library begin with initializing the main library object – PDFWriter – with a file path. All usages end with finalizing that file and closing it. Between opening and closing of the PDF file pages are added, content is drawn, object defined etc.

You start using the library by creating an instance of the PDFWriter object:
PDFWriter pdfWriter;

Then you open a PDF file for writing. for example, to write the PDF file to c:\myFile.pdf use the following code:
pdfWriter.StartPDF("c:\\myFile.pdf",ePDFVersion13);

StartPDF receives 2 required parameters that determine the file location and the PDF file version. With the current library setting the version does nothing other than writing it at the header of the file. The file name should be UTF8 encoded.
StartPDF method has optional parameters for determining PDF creation parameters and logging. These are explained in other sections.

Now go ahead and add content to the PDF. finally finish the PDF writing with the following code:
pdfWriter.EndPDF();
The EndPDF command finalizes the PDF and closes the file. Finalization step writes the pages tree, catalog object, info dictionary, Xref table and Trailer.
Now the PDF file is ready.

Let’s see all this code together:

PDFWriter pdfWriter;
pdfWriter.StartPDF("c:\\myFile.pdf",ePDFVersion13);
// ...add content to PDF file...
pdfWriter.EndPDF();

All methods return EStatusCode which is either eSuccess or eFailure (it is defined under PDFHummus namespace). if they return objects a NULL return value would normally mean that an error occurred. You should check status to make sure all goes well. here the checking code was omitted for the sake of brevity.

Note that you can create PDF output not just in files, but in streams in general. for more on this read Custom input and output

Clone this wiki locally