Crionics LOGO

TUTORIALS

 

How to use Creator

Draw on an existing document

As easy as 1, 2, 3...

 

Open the document

In this tutorial we will explain how to open an existing document in order to add graphic primitives and pages to it. Please note that this feature is only available with jPDF Creator.

First, we need an existing PDF document to start with. Let's pick the one from the Basics Tutorial.

PdfDocument doc = new PdfDocument("SimpleJava2DDemo.pdf");

Now we need to get the page we want to change.

// Get the first page in the document:
Page page=doc.getCreatorModule().getPage(1);

// Get the page format:
PageFormat pf=page.getPageFormat();
int page_y=(int)pf.getHeight();

Note that we could also append a new page and/or reorganize the page structure if we wished to.

 

Draw on the page

Now that we have the page, let's add some graphical primitives. All new graphic elements will be displayed on top of the existing ones.

Graphics2D g2d=page.getGraphics();

g2d.setPaint(Color.yellow);
g2d.setFont(new Font("Sans", Font.BOLD, 30));
g2d.drawString("This document was modified ",15, page_y/4);

// Important: DO NOT forget to dispose():
g2d.dispose();

 

Save the document and run the application

Final step: Save the document to a file and close it in order to release memory.

doc.saveAs("SimpleJava2DDemo_modif.pdf");
doc.close();

Note how we saved the document to a new file. We could have used the save() method which saves the update into the existing document with an incremental update.

The full source code of this tutorial is part of the distribution.
Please click on this link to display the generated PDF.


© 2000-2007 Crionics Inc. Report issues with this page