pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1587975 - in /pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel: AddImageToPDF.java ImageToPDF.java
Date Wed, 16 Apr 2014 16:51:45 GMT
Author: tilman
Date: Wed Apr 16 16:51:45 2014
New Revision: 1587975

URL: http://svn.apache.org/r1587975
Log:
PDFBOX-1990: improve example(s) to allow lossless images, and scaling

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=1587975&r1=1587974&r2=1587975&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
Wed Apr 16 16:51:45 2014
@@ -16,9 +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.io.RandomAccessFile;
 
@@ -29,6 +31,7 @@ import org.apache.pdfbox.pdmodel.edit.PD
 
 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;
 
 /**
@@ -71,15 +74,23 @@ public class AddImageToPDF
             {
                 ximage = CCITTFactory.createFromRandomAccess(doc, new RandomAccessFile(new
File(image),"r"));
             }
+            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
             {
-                //BufferedImage awtImage = ImageIO.read( new File( image ) );
-                //ximage = new PDPixelMap(doc, awtImage);
-                throw new IOException( "Image type not supported:" + image );
+                throw new IOException( "Image type not supported: " + image );
             }
             PDPageContentStream contentStream = new PDPageContentStream(doc, page, true,
true);
 
-            contentStream.drawImage( ximage, 20, 20 );
+            //contentStream.drawImage(ximage, 20, 20 );
+            // better method inspired by http://stackoverflow.com/a/22318681/535646
+            float scale = 1f; // reduce this value if the image is too large
+            contentStream.drawXObject(ximage, 20, 20, ximage.getWidth()*scale, ximage.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=1587975&r1=1587974&r2=1587975&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
Wed Apr 16 16:51:45 2014
@@ -82,13 +82,14 @@ public class ImageToPDF
             }
             else
             {
-                //BufferedImage awtImage = ImageIO.read( new File( image ) );
-                //ximage = new PDPixelMap(doc, awtImage);
-                throw new IOException( "Image type not supported:" + image );
+                throw new IOException( "Image type not supported: " + image );
             }
             PDPageContentStream contentStream = new PDPageContentStream(doc, page);
 
-            contentStream.drawImage( pdImage, 20, 20 );
+            //contentStream.drawImage(pdImage, 20, 20 );
+            // better method inspired by http://stackoverflow.com/a/22318681/535646
+            float scale = 1f; // reduce this value if the image is too large
+            contentStream.drawXObject(pdImage, 20, 20, pdImage.getWidth()*scale, pdImage.getHeight()*scale);
 
             contentStream.close();
             doc.save( file );



Mime
View raw message