jPDFWriter Developer Guide

jPDFWriter Developer Guide


Distribution and JAR Files

Javadoc API

Source Code Samples


jPDFWriter is a Java library that allows you to create PDF documents directly from your Java programs.

jPDFWriter can create PDF files in two ways:

  • PDF files can be created directly using jPDFWriter’s very simple API. Simply create a PDFDocument object, create as many PDFPage objects as necessary, draw strings, graphics or any other elements supported by Graphics2D to the pages and then save the document.
  • jPDFWriter also extends the standard Java PrinterJob so that you can create PDF files in the same way that you would print to a physical printer. This allows for reuse of existing printing code and for an application to decide, at runtime, whether to send the output to a printer or to a PDF file.

PDF files can be saved to files on a disk, written to a or written directly to a javax.servlet.ServletOutputStream to show in a browser when working within a J2EE application server.

Creating a Document

// Create a new document
PDFDocument pdfDoc = new PDFDocument ();

Adding a Page

// Create page
PDFPage page = pdfDoc.createPage(null);
// Add page to document
pdfDoc.addPage (page);

Writing and Drawing on a Page using Graphics Object

Once you have created a PDFDocument and added a page to it, you can simply use all standard graphics commands from the Java Graphics2D class to draw to the page.

// get graphics object from the page
Graphics2D g2d = page.createGraphics();
// draw a red rectangle
g2d.fillRect(100, 100, 400, 200);
// draw a round border to the rectangle
g2d.setStroke(new BasicStroke(6));
g2d.drawRoundRect(100, 100, 400, 200, 10, 10);
// draw a string
g2d.setFont(new Font ("Helvetica", Font.BOLD, 36));
g2d.drawString("Qoppa Software", 150, 200);

This would be the output to PDF when using the graphics commands above to write to a page.
Graphics Drawing Result

Printing JComponent to PDF using Graphics Object

You can use the JComponent method print(Graphics g) to print any Swing component. Note that components have to be showing on the screen to be printable.

// get graphics object from the page
Graphics2D g2d = page.createGraphics();
// myComponent being a JComponent showing on the screen

Saving a Document

// get an output file name
File outFile = getOutputFile ();
// save document
if (outFile != null) {
  pdfDoc.saveDocument (outFile.getAbsolutePath());

Printing to PDF Using PrinterJob

Another convenient way to print to PDF and reuse any existing printing code that you already have is to use the PDFPrinterJob class. PDFPrinterJob class extends the standard Java PrinterJob and replicates its functionality except that the output goes to a PDF document instead of a printer.

Calling the static method getPrinterJob will create a PDFPrinterJob object. You can then print any Printable or Pageable object to this printer job as you would with a standard Java PrinterJob. Users will be prompted for a file name and the PDFPrinterJob will print to the given PDF file.

// get a PDFPrinterJob
PDFPrinterJob printer = (PDFPrinterJob)PDFPrinterJob.getPrinterJob ();
// myPrintable being a class that implements the Printable interface
printer.setPrintable (myPrintable);
printer.setCopies (1);

Distribution and JAR Files

jPDFWriter is packaged entirely in a single jar file, jPDFWriter.jar that is installedas part of the sample demo application and needs to be included in the class path when running the application.

Javadoc API
Source Code Samples