A Java program that gets images from a PDF (one image per page) as Java BufferedImage objects using Qoppa’s library jPDFImages. Once images are available as BufferedImage objects, they can easily be modified before saving using Java Graphics2D. For instance here a watermark is applied to the images.
Sample code, which uses jPDFImages library but can also be changed to use jPDFProcess or jPDFViewer.
// Load the document PDFImages pdfDoc = new PDFImages ("input.pdf", null); // Loop through pages for (int count = 0; count < pdfDoc.getPageCount(); ++count) { // Get an image of the PDF page at a resolution of 150 DPI BufferedImage pageImage = pdfDoc.getPageImage(count, 150); // create a Graphics 2D from the page image Graphics2D g2d = pageImage.createGraphics(); // BufferedImage can be modified using all drawing functions available in Graphics2D // here is an example on how to add a watermark g2d.setFont (new Font ("sansserif", Font.PLAIN, 200)); g2d.rotate(Math.toRadians(45)); g2d.setColor (new Color (128, 128, 128, 128)); g2d.drawString ("Watermark", 300, g2d.getFontMetrics().getMaxDescent()); // Save the image as a JPEG File outputFile = new File ("output_" + count + ".jpg"); ImageIO.write(pageImage, "JPEG", outputFile); |
http://www.qoppa.com/pdfimages/guide/sourcesamples/PDFToBufferedImages.java
If your goal is only to add a watermark, you may also look at this simple sample:
http://www.qoppa.com/pdfimages/guide/sourcesamples/Watermark.java