pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1676595 - in /pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel: AddImageToPDF.java ImageToPDF.java
Date Tue, 28 Apr 2015 19:05:25 GMT
Author: tilman
Date: Tue Apr 28 19:05:24 2015
New Revision: 1676595

URL: http://svn.apache.org/r1676595
Log:
PDFBOX-2777: use the new convenience method, as suggested by John Hewson

Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java?rev=1676595&r1=1676594&r2=1676595&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java
(original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/AddImageToPDF.java
Tue Apr 28 19:05:24 2015
@@ -16,17 +16,11 @@
  */
 package org.apache.pdfbox.examples.pdmodel;
 
-import java.awt.image.BufferedImage;
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
-import javax.imageio.ImageIO;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.pdfbox.pdmodel.PDPageContentStream;
-import org.apache.pdfbox.pdmodel.graphics.image.CCITTFactory;
-import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory;
-import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
 import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
 
 /**
@@ -42,12 +36,12 @@ public class AddImageToPDF
      * Add an image to an existing PDF document.
      *
      * @param inputFile The input PDF to add the image to.
-     * @param image The filename of the image to put in the PDF.
+     * @param imagePath The filename of the image to put in the PDF.
      * @param outputFile The file to write to the pdf to.
      *
      * @throws IOException If there is an error writing the data.
      */
-    public void createPDFFromImage( String inputFile, String image, String outputFile )
+    public void createPDFFromImage( String inputFile, String imagePath, String outputFile
)
             throws IOException
     {
         // the document
@@ -59,33 +53,17 @@ public class AddImageToPDF
             //we will add the image to the first page.
             PDPage page = doc.getPage(0);
 
-            PDImageXObject ximage;
-            if( image.toLowerCase().endsWith( ".jpg" ) )
-            {
-                ximage = JPEGFactory.createFromStream(doc, new FileInputStream(image));
-            }
-            else if (image.toLowerCase().endsWith(".tif") || image.toLowerCase().endsWith(".tiff"))
-            {
-                ximage = CCITTFactory.createFromFile(doc, new File(image));
-            }
-            else if (image.toLowerCase().endsWith(".gif") || 
-                    image.toLowerCase().endsWith(".bmp") || 
-                    image.toLowerCase().endsWith(".png"))
-            {
-                BufferedImage bim = ImageIO.read(new File(image));
-                ximage = LosslessFactory.createFromImage(doc, bim);
-            }
-            else
-            {
-                throw new IOException( "Image type not supported: " + image );
-            }
+            // createFromFile is the easiest way with an image file
+            // if you already have the image in a BufferedImage, 
+            // call LosslessFactory.createFromImage() instead
+            PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc);
             PDPageContentStream contentStream = new PDPageContentStream(doc, page, true,
true);
 
             // contentStream.drawImage(ximage, 20, 20 );
             // better method inspired by http://stackoverflow.com/a/22318681/535646
             // reduce this value if the image is too large
             float scale = 1f;
-            contentStream.drawImage(ximage, 20, 20, ximage.getWidth()*scale, ximage.getHeight()*scale);
+            contentStream.drawImage(pdImage, 20, 20, pdImage.getWidth()*scale, pdImage.getHeight()*scale);
 
             contentStream.close();
             doc.save( outputFile );

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java?rev=1676595&r1=1676594&r2=1676595&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java
(original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java
Tue Apr 28 19:05:24 2015
@@ -16,17 +16,10 @@
  */
 package org.apache.pdfbox.examples.pdmodel;
 
-import java.awt.image.BufferedImage;
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
-import javax.imageio.ImageIO;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.pdfbox.pdmodel.PDPageContentStream;
-import org.apache.pdfbox.pdmodel.graphics.image.CCITTFactory;
-import org.apache.pdfbox.pdmodel.graphics.image.JPEGFactory;
-import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
 import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
 
 /**
@@ -59,27 +52,10 @@ public class ImageToPDF
             PDPage page = new PDPage();
             doc.addPage(page);
 
-            PDImageXObject pdImage;
-            if (imagePath.toLowerCase().endsWith(".jpg"))
-            {
-                pdImage = JPEGFactory.createFromStream(doc, new FileInputStream(imagePath));
-            }
-            else if (imagePath.toLowerCase().endsWith(".tif") ||
-                     imagePath.toLowerCase().endsWith(".tiff"))
-            {
-                pdImage = CCITTFactory.createFromFile(doc, new File(imagePath));
-            }
-            else if (imagePath.toLowerCase().endsWith(".gif") ||
-                     imagePath.toLowerCase().endsWith(".bmp") ||
-                     imagePath.toLowerCase().endsWith(".png"))
-            {
-                BufferedImage bim = ImageIO.read(new File(imagePath));
-                pdImage = LosslessFactory.createFromImage(doc, bim);
-            }
-            else
-            {
-                throw new IOException("Image type not supported: " + imagePath);
-            }
+            // createFromFile is the easiest way with an image file
+            // if you already have the image in a BufferedImage, 
+            // call LosslessFactory.createFromImage() instead
+            PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc);
             
             PDPageContentStream contents = new PDPageContentStream(doc, page);
             



Mime
View raw message