Holger's
Java API

com.antelmann.util
Class PDFUtils

java.lang.Object
  extended by com.antelmann.util.PDFUtils

@ThirdParty(value={"uses itext - see itextpdf.com","JFreeChart - see jfree.org"})
public class PDFUtils
extends Object

a few utility methods surrounding PDF. This class uses the IText package from lowagie.com: iText Library - Copyright (C) 1999-2005 by Bruno Lowagie and Paulo Soares. All Rights Reserved. This class also uses the JFreeChart library (www.jfree.org). Since November 2015, this implementation has been upgraded to work w/ com.itextpdf instead of the old com.lowagie!

Author:
Holger Antelmann

Field Summary
static int ALLOW_ASSEMBLY
           
static int ALLOW_COPY
           
static int ALLOW_DEGRADED_PRINTING
           
static int ALLOW_FILL_IN
           
static int ALLOW_MODIFY_ANNOTATIONS
           
static int ALLOW_MODIFY_CONTENTS
           
static int ALLOW_PRINTING
           
static int ALLOW_SCREENREADERS
           
 
Method Summary
static void concat(File target, File... pdfFiles)
           
static void concat(OutputStream out, InputStream... pdfStreams)
           
static void concat(OutputStream out, Iterator<? extends InputStream> pdfStreams)
           
static void encryptPDF(InputStream in, OutputStream out, String userPassword, String ownerPassword, int permissions)
          allows to encrypt a PDF-input with a password and write the protected result into the PDF-output
static void exportChartAsPDF(OutputStream out, org.jfree.chart.JFreeChart chart)
           
static void exportChartAsPDF(OutputStream out, org.jfree.chart.JFreeChart chart, int width, int height)
           
static void exportChartAsPDF(OutputStream out, org.jfree.chart.JFreeChart chart, int width, int height, com.itextpdf.awt.FontMapper fontMapper, float scale)
           
static void extract(OutputStream out, InputStream pdfContent, int... pages)
          first page is 1
static AbstractIterator<BufferedImage> extractImages(InputStream pdfStream)
          only works on a limited subset of PDF documents, unfortunately; happens to work just fine w/ PDFs produced by common scanners :)
static void generatePDF(String text, File file)
          uses the IText package from lowagie.com
static int getNumberOfPages(InputStream pdfContent)
           
static void merge(OutputStream out, InputStream mainPdfContent, InputStream underPdfContent)
          the new document (in regards to its size) is based on the mainPdfContent
static void merge(OutputStream out, InputStream mainPdfContent, InputStream underPdfContent, int mainStartPage)
          merges the main content with the other content starting with the given page on the main content; first page is 1
static void place(OutputStream out, InputStream pdfSource, Image image, boolean ontop, int pageNumber, Point position)
          uses 300 dpi; see place(OutputStream, InputStream, Image, boolean, int, Point, int)
static void place(OutputStream out, InputStream pdfSource, Image image, boolean ontop, int pageNumber, Point position, int dpi)
          places the given image either on top or under the content of the specified page
static void place(OutputStream out, InputStream pdfSource, InputStream pdfContent, boolean ontop, int pageNumber, Point position)
          calls place(OutputStream, InputStream, InputStream, boolean, int, Point, float) with a scale of 1
static void place(OutputStream out, InputStream pdfSource, InputStream pdfContent, boolean ontop, int pageNumber, Point position, float scale)
          places the first page of the given pdfContent either on top or under the pdfSource on the specified page(s).
static void placeChartOnPage(OutputStream out, InputStream pdfSource, org.jfree.chart.JFreeChart chart, boolean ontop, int pageNumber, Point position)
           
static void placeChartOnPage(OutputStream out, InputStream pdfSource, org.jfree.chart.JFreeChart chart, int width, int height, boolean ontop, int pageNumber, Point position)
           
static void placeChartOnPage(OutputStream out, InputStream pdfSource, org.jfree.chart.JFreeChart chart, int width, int height, boolean ontop, int pageNumber, Point position, float scale)
           
static Process printPDF(File pdfFile)
          platform dependent system call to print the given file to the default printer.
static void rotatePages(InputStream pdfSource, OutputStream target, double degrees)
          rotates all pages by the given degrees
static void rotatePages(InputStream pdfSource, OutputStream target, Map<Integer,? extends Number> pageDegrees)
          rotates each given page number in the map with the mapped degrees
static void splitIntoPages(File pdfFile, Monitor mon)
          splits the given document into separate files, where each page becomes a file named after the given file plus the page number
static void stamp(OutputStream out, InputStream pdfSource, Image image, int pageNumber)
          see place(OutputStream, InputStream, Image, boolean, int, Point)
static void toPDF(Component c, File file)
           
static void toPDF(Component c, OutputStream pdfStream)
          uses 300 dpi
static void toPDF(Component c, OutputStream pdfStream, int dpi)
          produces a PDF document with the given output stream and places the given component on the page; the orientation of the document follows the width/height ratio of the component.
static void toPDF(String txt, OutputStream pdfStream)
           
static void watermark(OutputStream out, InputStream pdfSource, Image image, int pageNumber)
          see place(OutputStream, InputStream, Image, boolean, int, Point)
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALLOW_ASSEMBLY

public static final int ALLOW_ASSEMBLY
See Also:
Constant Field Values

ALLOW_COPY

public static final int ALLOW_COPY
See Also:
Constant Field Values

ALLOW_DEGRADED_PRINTING

public static final int ALLOW_DEGRADED_PRINTING
See Also:
Constant Field Values

ALLOW_FILL_IN

public static final int ALLOW_FILL_IN
See Also:
Constant Field Values

ALLOW_MODIFY_ANNOTATIONS

public static final int ALLOW_MODIFY_ANNOTATIONS
See Also:
Constant Field Values

ALLOW_MODIFY_CONTENTS

public static final int ALLOW_MODIFY_CONTENTS
See Also:
Constant Field Values

ALLOW_PRINTING

public static final int ALLOW_PRINTING
See Also:
Constant Field Values

ALLOW_SCREENREADERS

public static final int ALLOW_SCREENREADERS
See Also:
Constant Field Values
Method Detail

exportChartAsPDF

public static void exportChartAsPDF(OutputStream out,
                                    org.jfree.chart.JFreeChart chart)
                             throws IOException
Throws:
IOException

exportChartAsPDF

public static void exportChartAsPDF(OutputStream out,
                                    org.jfree.chart.JFreeChart chart,
                                    int width,
                                    int height)
                             throws IOException
Throws:
IOException

exportChartAsPDF

@ThirdParty(value="jfreechart")
public static void exportChartAsPDF(OutputStream out,
                                                    org.jfree.chart.JFreeChart chart,
                                                    int width,
                                                    int height,
                                                    com.itextpdf.awt.FontMapper fontMapper,
                                                    float scale)
                             throws IOException
Throws:
IOException

placeChartOnPage

public static void placeChartOnPage(OutputStream out,
                                    InputStream pdfSource,
                                    org.jfree.chart.JFreeChart chart,
                                    boolean ontop,
                                    int pageNumber,
                                    Point position)
                             throws IOException,
                                    com.itextpdf.text.DocumentException
Throws:
IOException
com.itextpdf.text.DocumentException
See Also:
placeChartOnPage(OutputStream, InputStream, JFreeChart, int, int, boolean, int, Point)

placeChartOnPage

public static void placeChartOnPage(OutputStream out,
                                    InputStream pdfSource,
                                    org.jfree.chart.JFreeChart chart,
                                    int width,
                                    int height,
                                    boolean ontop,
                                    int pageNumber,
                                    Point position)
                             throws IOException,
                                    com.itextpdf.text.DocumentException
Throws:
IOException
com.itextpdf.text.DocumentException
See Also:
place(OutputStream, InputStream, InputStream, boolean, int, Point), exportChartAsPDF(OutputStream, JFreeChart, int, int)

placeChartOnPage

public static void placeChartOnPage(OutputStream out,
                                    InputStream pdfSource,
                                    org.jfree.chart.JFreeChart chart,
                                    int width,
                                    int height,
                                    boolean ontop,
                                    int pageNumber,
                                    Point position,
                                    float scale)
                             throws IOException,
                                    com.itextpdf.text.DocumentException
Throws:
IOException
com.itextpdf.text.DocumentException
See Also:
place(OutputStream, InputStream, InputStream, boolean, int, Point), exportChartAsPDF(OutputStream, JFreeChart, int, int, FontMapper, float)

watermark

public static void watermark(OutputStream out,
                             InputStream pdfSource,
                             Image image,
                             int pageNumber)
                      throws IOException,
                             com.itextpdf.text.DocumentException
see place(OutputStream, InputStream, Image, boolean, int, Point)

Throws:
IOException
com.itextpdf.text.DocumentException

stamp

public static void stamp(OutputStream out,
                         InputStream pdfSource,
                         Image image,
                         int pageNumber)
                  throws IOException,
                         com.itextpdf.text.DocumentException
see place(OutputStream, InputStream, Image, boolean, int, Point)

Throws:
IOException
com.itextpdf.text.DocumentException

place

public static void place(OutputStream out,
                         InputStream pdfSource,
                         Image image,
                         boolean ontop,
                         int pageNumber,
                         Point position)
                  throws IOException,
                         com.itextpdf.text.DocumentException
uses 300 dpi; see place(OutputStream, InputStream, Image, boolean, int, Point, int)

Throws:
IOException
com.itextpdf.text.DocumentException

place

public static void place(OutputStream out,
                         InputStream pdfSource,
                         Image image,
                         boolean ontop,
                         int pageNumber,
                         Point position,
                         int dpi)
                  throws IOException,
                         com.itextpdf.text.DocumentException
places the given image either on top or under the content of the specified page

Parameters:
out - the stream where the resulting PDF should be written
pdfSource - the original PDF document source
image - the image to be placed on the page(s)
pageNumber - specifies the page where the content is to be placed; if negative, the content is written on every page
ontop - if true, the image is placed on top like a stamp; if false, the image is place beneath like a watermark
position - denotes the absolute position of the image; if null, the image is centered on the page
Throws:
IOException
com.itextpdf.text.DocumentException

place

public static void place(OutputStream out,
                         InputStream pdfSource,
                         InputStream pdfContent,
                         boolean ontop,
                         int pageNumber,
                         Point position)
                  throws IOException,
                         com.itextpdf.text.DocumentException
calls place(OutputStream, InputStream, InputStream, boolean, int, Point, float) with a scale of 1

Throws:
IOException
com.itextpdf.text.DocumentException

place

public static void place(OutputStream out,
                         InputStream pdfSource,
                         InputStream pdfContent,
                         boolean ontop,
                         int pageNumber,
                         Point position,
                         float scale)
                  throws IOException,
                         com.itextpdf.text.DocumentException
places the first page of the given pdfContent either on top or under the pdfSource on the specified page(s).

Parameters:
out - the stream where the resulting PDF should be written
pdfSource - the original PDF document source
pdfContent - the content to be placed on the page(s) (only the first page is used)
pageNumber - specifies the page where the content is to be placed; if less than 1, the content is written on every page
ontop - if true, the image is placed on top like a stamp; if false, the image is place beneath like a watermark
position - denotes the absolute position of the image; if null, the image is centered on each page
scale - specifies the scale (1 for no scale) that is to be applied to the pdfContent before placing it
Throws:
IOException
com.itextpdf.text.DocumentException

getNumberOfPages

public static int getNumberOfPages(InputStream pdfContent)
                            throws IOException
Throws:
IOException

extract

public static void extract(OutputStream out,
                           InputStream pdfContent,
                           int... pages)
                    throws IOException,
                           com.itextpdf.text.DocumentException
first page is 1

Throws:
IOException
com.itextpdf.text.DocumentException

splitIntoPages

public static void splitIntoPages(File pdfFile,
                                  Monitor mon)
                           throws IOException,
                                  com.itextpdf.text.DocumentException
splits the given document into separate files, where each page becomes a file named after the given file plus the page number

Throws:
IOException
com.itextpdf.text.DocumentException

merge

public static void merge(OutputStream out,
                         InputStream mainPdfContent,
                         InputStream underPdfContent)
                  throws IOException,
                         com.itextpdf.text.DocumentException
the new document (in regards to its size) is based on the mainPdfContent

Throws:
IOException
com.itextpdf.text.DocumentException

merge

public static void merge(OutputStream out,
                         InputStream mainPdfContent,
                         InputStream underPdfContent,
                         int mainStartPage)
                  throws IOException,
                         com.itextpdf.text.DocumentException
merges the main content with the other content starting with the given page on the main content; first page is 1

Throws:
IOException
com.itextpdf.text.DocumentException

concat

public static void concat(File target,
                          File... pdfFiles)
                   throws IOException,
                          com.itextpdf.text.DocumentException
Throws:
IOException
com.itextpdf.text.DocumentException

concat

public static void concat(OutputStream out,
                          InputStream... pdfStreams)
                   throws IOException,
                          com.itextpdf.text.DocumentException
Throws:
IOException
com.itextpdf.text.DocumentException

concat

public static void concat(OutputStream out,
                          Iterator<? extends InputStream> pdfStreams)
                   throws IOException,
                          com.itextpdf.text.DocumentException
Throws:
IOException
com.itextpdf.text.DocumentException

printPDF

public static Process printPDF(File pdfFile)
                        throws IOException
platform dependent system call to print the given file to the default printer. Method uses application.pdfPrintCommand from the Settings. The switches used for the AcroRd32.exe are "/p /h".

Throws:
IOException
See Also:
Settings.getProperties()

generatePDF

public static void generatePDF(String text,
                               File file)
                        throws IOException,
                               PrintException
uses the IText package from lowagie.com

Throws:
IOException
PrintException

toPDF

public static void toPDF(Component c,
                         File file)
                  throws IOException,
                         com.itextpdf.text.DocumentException,
                         PrintException
Throws:
IOException
com.itextpdf.text.DocumentException
PrintException
See Also:
toPDF(Component, OutputStream)

toPDF

public static void toPDF(Component c,
                         OutputStream pdfStream)
                  throws IOException,
                         PrintException
uses 300 dpi

Throws:
IOException
PrintException
See Also:
toPDF(Component, OutputStream, int)

toPDF

public static void toPDF(Component c,
                         OutputStream pdfStream,
                         int dpi)
                  throws IOException,
                         PrintException
produces a PDF document with the given output stream and places the given component on the page; the orientation of the document follows the width/height ratio of the component. Do not forget to set the component's size appropriately!

Parameters:
c - the component to be printed on a PDF-page
pdfStream - the stream to where the PDF is to be written; the stream is closed within this method
Throws:
IOException
PrintException

toPDF

public static void toPDF(String txt,
                         OutputStream pdfStream)
                  throws IOException,
                         PrintException
Throws:
IOException
PrintException

rotatePages

public static void rotatePages(InputStream pdfSource,
                               OutputStream target,
                               double degrees)
                        throws com.itextpdf.text.DocumentException,
                               IOException
rotates all pages by the given degrees

Throws:
com.itextpdf.text.DocumentException
IOException

rotatePages

public static void rotatePages(InputStream pdfSource,
                               OutputStream target,
                               Map<Integer,? extends Number> pageDegrees)
                        throws IOException,
                               com.itextpdf.text.DocumentException
rotates each given page number in the map with the mapped degrees

Parameters:
pageDegrees - maps page numbers to the rotation degrees
Throws:
IOException
com.itextpdf.text.DocumentException

extractImages

public static AbstractIterator<BufferedImage> extractImages(InputStream pdfStream)
                                                     throws IOException
only works on a limited subset of PDF documents, unfortunately; happens to work just fine w/ PDFs produced by common scanners :)

Throws:
IOException

encryptPDF

@ThirdParty(value="org.bouncycastle")
public static void encryptPDF(InputStream in,
                                              OutputStream out,
                                              String userPassword,
                                              String ownerPassword,
                                              int permissions)
                       throws IOException,
                              IllegalArgumentException,
                              PrintException
allows to encrypt a PDF-input with a password and write the protected result into the PDF-output

Parameters:
in - the PDF-input
out - the PDF-output
userPassword - the password used to be able to read the encrypted output document
ownerPassword - the password that allows not only read the document, but also to change the permissions of the PDF-file (in order to create an unprotected version)
permissions - permissions 'or'-like combined user permissions like ALLOW_PRINTING
Throws:
IOException
IllegalArgumentException
PrintException


(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads