PDFOne (for Java)
Create, edit, view, print & enhance PDF documents and forms in Java SE/EE
Compatibility
J2SE J2EE Windows Linux Mac (OS X)

How to Create PDF Pages and Render Text, Shapes and Images

Learn how to create multiple pages and render text, shapes, images, and watermarks over page ranges.
By Santhanam L.

In last month's article, we learned about creating PDF documents and writing text. In particular, we saw that when a PdfDocument object is created, a page is created by default and that all subsequent calls to the writeText() method* method will render text on that "default" page.

In this article, we will see how to explicitly create new pages and render various kinds of PDF elements on those pages.

Creating Multiple Pages

In PDFOne Java, a page is represented by the class PdfPage. You can create a page using any one of the several PdfPage overloaded constructors.

// Create a PdfDocument instance 
PdfDocument doc1 = new PdfDocument();

// Create a page
PdfPage page1 = new PdfPage(
	PdfPageSize.A4, // page size
        25,  // header height
	25,  // footer height
	50,  // left margin
	50,  // top margin
	50,  // right margin
	50,  // bottom margin
	PdfMeasurement.MU_POINTS // measurement unit
);

An easy way to create multiple pages is to clone them. And, after creating the pages, you need to add the pages to the document.

// Create more pages by cloning the above
PdfPage page2 = (PdfPage) page1.clone();
PdfPage page3 = (PdfPage) page1.clone();

// Add the pages to the document
doc1.add(page1);
doc1.add(page2);
doc1.add(page3);

Using Page Ranges

Several methods in the PdfDocument class have a page range parameter that allow you to render PDF elements on several pages in single call. Before using such a method, you should ensure that you have already one or more PdfPage objects to the document, as shown above.

// Write text
doc1.writeText(
    "Four score and seven years ago, our fathers " +
    "brought forth on this continent a new " +
    "nation conceived in liberty and dedicated " +
    "to the proposition that all men are created " +
    "equal.",
    150, // x-coordinate of top-left position 
    100, // y-coordinate of top-left position
    PdfTextFormatter.LEFT, // text alignment 
    PdfTextFormatter.WRAP, // text wrapping
    "1-3" // page range (pages 1, 2 & 3)
    );

Using Similar PdfDocument and PdfPage Methods

The PdfPage object has several of the same methods that a PdfDocument object has, although they do not have a page range parameter. That's because a document object can be considered as representing several pages while a page object would represent only one page. In the code snippet below, we will see the addHeaderText() method used to illustrate this.

// Create a PDF font
PdfFont font1 = PdfFont.create(
  "Helvetica",
  25,
  PdfEncodings.WINANSI);

// Add text header to pages 1 and 2
doc1.addHeaderText(
 	"The five boxing wizards jump quickly.",
	// font
        font1,
        // vertical and horizontal alignment
        PdfPage.VP_CENTRE | PdfPage.HP_MIDDLE,
        // underlay
        true,
        // page range
        "1-2");

// Add text header to pages 3
page3.addHeaderText(
    "The five boxing wizards jump quickly.",
    // font
    font1,
    // vertical and horizontal alignment
    PdfPage.VP_CENTRE | PdfPage.HP_LEFT,
    // underlay
    true);

When used with a PdfDocument object, the addHeaderText() method can add a header to several pages, as specified in the page range parameter. When used with a PdfPage object, the method adds a header only to that page.

Finally, here is a code snippet where a shape element (in this case a circle) is drawn, an image is rendered, and a text watermark is placed on an individual page as well as across several pages in a specified page range.

// Set measurement unit to inches for document object
doc1.setMeasurementUnit(PdfMeasurement.MU_INCHES);	

// Draw a circle on pages 1 & 2
doc1.drawCircle(3, 4.5, 2.5, false, true, "1-2");

// Draw an image on pages 1 & 3
doc1.drawImage(
	// pathname of image
	"C:\\Documents and Settings\\All Users"  
	+ "\\Documents\\My Pictures\\Sample Pictures"
	+ "\\Sunset.jpg", 
	// x-y coordinates of the location where
	// the image needs to be drawn
	1, 3,
	// width and height of the image
	4, 3,				       
	// angle of rotation of the image
	315,
	// page range
	"1,3");

// Increase font size
font1.setSize(45);
// Add a text watermark to pages 2 & 3  
doc1.addWatermarkText(
	// watermark text
	"Do Not Print. For Eyes Only", 
	// watermark font
	font1, 
	// vertical and horizontal alignment of watermark
	PdfPage.VP_CENTRE | PdfPage.HP_MIDDLE, 
	// apply margins? 
	true, 
	// angle of rotation
	45, 
	// underlay
	true, 
	// page range
	"2-3");
// Add a text watermark to page 1
page1.addWatermarkText(
	"Do Not Print. For Eyes Only", 
	font1, 
	PdfPage.VP_CENTRE | PdfPage.HP_MIDDLE, 
	false, 
	45,
	false);
---o0O0o---

* - when used without a specified page range parameter

† - The page range parameter can be specified in the same manner you would do in a print dialog box.

---o0O0o---

Our .NET Developer Tools
XtremeDocumentStudio .NET

Multi-format document-processing component suite for .NET developers.

PDFOne .NET

A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.

Our Delphi/C++Builder developer tools
XtremeDocumentStudio Delphi

Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.

eDocEngine VCL

A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.

PDFtoolkit VCL

A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.

Our Java developer tools
XtremeDocumentStudio (for Java)

Multi-format document-processing component suite for Java developers.

PDFOne (for Java)

A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.

Our Platform-Agnostic Cloud and On-Premises APIs
StarDocs

Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs

Privacy | Legal | Feedback | Newsletter | Blog | Resellers © 2002-2017 Gnostice Information Technologies Private Limited. All rights reserved.