You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2015/04/28 21:00:13 UTC
svn commit: r1676594 -
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
Author: tilman
Date: Tue Apr 28 19:00:12 2015
New Revision: 1676594
URL: http://svn.apache.org/r1676594
Log:
PDFBOX-2777: add convenience method to create a PDImageXObject object, as suggested by John Hewson
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java?rev=1676594&r1=1676593&r2=1676594&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/graphics/image/PDImageXObject.java Tue Apr 28 19:00:12 2015
@@ -34,9 +34,12 @@ import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
+import java.io.File;
+import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
+import javax.imageio.ImageIO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -115,6 +118,61 @@ public final class PDImageXObject extend
{
this(stream, resources, stream.getStream().getDecodeResult());
}
+
+ /**
+ * Create a PDImageXObject from an image file, see {@link #createFromFile(File, PDDocument)} for
+ * more details.
+ *
+ * @param imagePath the image file path.
+ * @param doc the document that shall use this PDImageXObject.
+ * @return a PDImageXObject.
+ * @throws IOException if there is an error when reading the file or creating the
+ * PDImageXObject, or if the image type is not supported.
+ */
+ public static PDImageXObject createFromFile(String imagePath, PDDocument doc) throws IOException
+ {
+ return createFromFile(new File(imagePath), doc);
+ }
+
+ /**
+ * Create a PDImageXObject from an image file. The file format is determined by the file name
+ * suffix. The following suffixes are supported: jpg, jpeg, tif, tiff, gif, bmp and png. This is
+ * a convenience method that calls {@link JPEGFactory#createFromStream},
+ * {@link CCITTFactory#createFromFile} or {@link ImageIO#read} combined with
+ * {@link LosslessFactory#createFromImage}. (The later can also be used to create a
+ * PDImageXObject from a BufferedImage).
+ *
+ * @param file the image file.
+ * @param doc the document that shall use this PDImageXObject.
+ * @return a PDImageXObject.
+ * @throws IOException if there is an error when reading the file or creating the
+ * PDImageXObject.
+ * @throws IllegalArgumentException if the image type is not supported.
+ */
+ public static PDImageXObject createFromFile(File file, PDDocument doc) throws IOException
+ {
+ String name = file.getName();
+ int dot = file.getName().lastIndexOf('.');
+ if (dot == -1)
+ {
+ throw new IOException("Image type not supported: " + name);
+ }
+ String ext = name.substring(dot + 1).toLowerCase();
+ if ("jpg".equals(ext) || "jpeg".equals(ext))
+ {
+ return JPEGFactory.createFromStream(doc, new FileInputStream(file));
+ }
+ if ("tif".equals(ext) || "tiff".equals(ext))
+ {
+ return CCITTFactory.createFromFile(doc, file);
+ }
+ if ("gif".equals(ext) || "bmp".equals(ext) || "png".equals(ext))
+ {
+ BufferedImage bim = ImageIO.read(file);
+ return LosslessFactory.createFromImage(doc, bim);
+ }
+ throw new IOException("Image type not supported: " + name);
+ }
// repairs parameters using decode result
private PDImageXObject(PDStream stream, PDResources resources, DecodeResult decodeResult)
@@ -326,7 +384,8 @@ public final class PDImageXObject extend
COSStream cosStream = (COSStream)getCOSStream().getDictionaryObject(COSName.MASK);
if (cosStream != null)
{
- return new PDImageXObject(new PDStream(cosStream), null); // always DeviceGray
+ // always DeviceGray
+ return new PDImageXObject(new PDStream(cosStream), null);
}
return null;
}
@@ -355,7 +414,8 @@ public final class PDImageXObject extend
COSStream cosStream = (COSStream)getCOSStream().getDictionaryObject(COSName.SMASK);
if (cosStream != null)
{
- return new PDImageXObject(new PDStream(cosStream), null); // always DeviceGray
+ // always DeviceGray
+ return new PDImageXObject(new PDStream(cosStream), null);
}
return null;
}
@@ -484,6 +544,7 @@ public final class PDImageXObject extend
* This will get the suffix for this image type, e.g. jpg/png.
* @return The image suffix or null if not available.
*/
+ @Override
public String getSuffix()
{
List<COSName> filters = getPDStream().getFilters();