Skip to content

Creating PDF Pages

gal kahana edited this page May 12, 2023 · 6 revisions

Adding pages to the PDF file is easy.

You start by creating an instance of the PDFPage class:
PDFPage* page = new PDFPage();

you should then set its media box with the SetMediaBox method. the following example set the media box to A4:
page->SetMediaBox(PDFRectangle(0,0,595,842));

Then use either WritePage method or WritePageAndRelease of the pdfWriter object:
pdfWriter.WritePage(page);
or
pdfWriter.WritePageAndRelease(page);

the difference between the methods is just that the latter method also deletes the page object. if you used WritePage, then when ready to delete it you can simply call delete page;. the only reason you would use WritePage and delete the object directly, is if you wish to reuse the page object (for example, in Creating multiple copies of the same page).

Let’s see all this code together (with the initial PDF file creation):

PDFWriter pdfWriter;
pdfWriter.StartPDF("c:\\test.pdf",ePDFVersion13);
PDFPage* page = new PDFPage();
page->SetMediaBox(PDFRectangle(0,0,595,842));
pdfWriter.WritePage(page);
delete page;
pdfWriter.EndPDF();

Note that a PDF Page has more boxes definition than just a Media Box.
PDF defines a Crop Box, a Bleed Box, A Trim Box and an Art Box. you can set these boxes up by using SetCropBox, SetBleedBox, SetTrimBox and SetArtBox respectively.

For a complete code example of how to create pages, check EmptyPagesTest

You would normally like to add content to the PDF pages. read about adding content here – Adding Content to PDF Pages

Clone this wiki locally