/** * Creates a thumbnail Image XObject from the given COSBase and name. * @param cosStream the COS stream * @return an XObject * @throws IOException if there is an error creating the XObject. */ public static PDImageXObject createThumbnail(COSStream cosStream) throws IOException { // thumbnails are special, any non-null subtype is treated as being "Image" PDStream pdStream = new PDStream(cosStream); return new PDImageXObject(pdStream, null); }
/** * Create a PDImageXObject using the Flate filter. * * @param document The document. * @param byteArray array with data. * @param width the image width * @param height the image height * @param bitsPerComponent the bits per component * @param initColorSpace the color space * @return the newly created PDImageXObject with the data compressed. * @throws IOException */ private static PDImageXObject prepareImageXObject(PDDocument document, byte [] byteArray, int width, int height, int bitsPerComponent, PDColorSpace initColorSpace) throws IOException { //pre-size the output stream to half of the input ByteArrayOutputStream baos = new ByteArrayOutputStream(byteArray.length/2); Filter filter = FilterFactory.INSTANCE.getFilter(COSName.FLATE_DECODE); filter.encode(new ByteArrayInputStream(byteArray), baos, new COSDictionary(), 0); ByteArrayInputStream encodedByteStream = new ByteArrayInputStream(baos.toByteArray()); return new PDImageXObject(document, encodedByteStream, COSName.FLATE_DECODE, width, height, bitsPerComponent, initColorSpace); }
private static PDImageXObject createJPEG(PDDocument document, BufferedImage image, float quality, int dpi) throws IOException { // extract alpha channel (if any) BufferedImage awtColorImage = getColorImage(image); BufferedImage awtAlphaImage = getAlphaImage(image); // create XObject ByteArrayOutputStream baos = new ByteArrayOutputStream(); encodeImageToJPEGStream(awtColorImage, quality, dpi, baos); ByteArrayInputStream byteStream = new ByteArrayInputStream(baos.toByteArray()); PDImageXObject pdImage = new PDImageXObject(document, byteStream, COSName.DCT_DECODE, awtColorImage.getWidth(), awtColorImage.getHeight(), awtColorImage.getColorModel().getComponentSize(0), getColorSpaceFromAWT(awtColorImage)); // alpha -> soft mask if (awtAlphaImage != null) { PDImage xAlpha = JPEGFactory.createFromImage(document, awtAlphaImage, quality); pdImage.getCOSObject().setItem(COSName.SMASK, xAlpha); } return pdImage; }
private static PDImageXObject prepareImageXObject(PDDocument document, byte[] byteArray, int width, int height, PDColorSpace initColorSpace) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Filter filter = FilterFactory.INSTANCE.getFilter(COSName.CCITTFAX_DECODE); COSDictionary dict = new COSDictionary(); dict.setInt(COSName.COLUMNS, width); dict.setInt(COSName.ROWS, height); filter.encode(new ByteArrayInputStream(byteArray), baos, dict, 0); ByteArrayInputStream encodedByteStream = new ByteArrayInputStream(baos.toByteArray()); PDImageXObject image = new PDImageXObject(document, encodedByteStream, COSName.CCITTFAX_DECODE, width, height, 1, initColorSpace); dict.setInt(COSName.K, -1); image.getCOSObject().setItem(COSName.DECODE_PARMS, dict); return image; }
PDImageXObject pdImage = new PDImageXObject(document, encodedByteStream, COSName.CCITTFAX_DECODE,
/** * Returns the Soft Mask Image XObject associated with this image, or null if there is none. * @return the SMask Image XObject, or null. * @throws java.io.IOException */ public PDImageXObject getSoftMask() throws IOException { COSStream cosStream = (COSStream) getCOSObject().getDictionaryObject(COSName.SMASK); if (cosStream != null) { // always DeviceGray return new PDImageXObject(new PDStream(cosStream), null); } return null; }
/** * Provide the image for stream. The stream must be image XObject. * * @param resources PDResources for the XObject. * @return A BufferedImage. */ public BufferedImage getImage(PDResources resources) { try { PDImageXObject imageXObject; if (isThumb) { imageXObject = PDImageXObject.createThumbnail(stream); } else { imageXObject = new PDImageXObject(new PDStream(stream), resources); } return imageXObject.getImage(); } catch (IOException e) { e.printStackTrace(); } return null; }
return new PDImageXObject(new PDStream(stream), resources);
/** * Returns the Mask Image XObject associated with this image, or null if there is none. * @return Mask Image XObject * @throws java.io.IOException */ public PDImageXObject getMask() throws IOException { COSBase mask = getCOSObject().getDictionaryObject(COSName.MASK); if (mask instanceof COSArray) { // color key mask, no explicit mask to return return null; } else { COSStream cosStream = (COSStream) getCOSObject().getDictionaryObject(COSName.MASK); if (cosStream != null) { // always DeviceGray return new PDImageXObject(new PDStream(cosStream), null); } return null; } }
PDImageXObject pdImage = new PDImageXObject(document, byteStream, COSName.DCT_DECODE, raster.getWidth(), raster.getHeight(), 8, colorSpace);
PDImageXObject imageXObject = new PDImageXObject(document, new ByteArrayInputStream(stream.toByteArray()), COSName.FLATE_DECODE, w, h, bitsPerComponent, pdColorSpace);
/** * Creates a thumbnail Image XObject from the given COSBase and name. * @param cosStream the COS stream * @return an XObject * @throws IOException if there is an error creating the XObject. */ public static PDImageXObject createThumbnail(COSStream cosStream) throws IOException { // thumbnails are special, any non-null subtype is treated as being "Image" PDStream pdStream = new PDStream(cosStream); return new PDImageXObject(pdStream, null); }
/** * Creates a thumbnail Image XObject from the given COSBase and name. * @param cosStream the COS stream * @return an XObject * @throws IOException if there is an error creating the XObject. */ public static PDImageXObject createThumbnail(COSStream cosStream) throws IOException { // thumbnails are special, any non-null subtype is treated as being "Image" PDStream pdStream = new PDStream(cosStream); return new PDImageXObject(pdStream, null); }
private static PDImageXObject createJPEG(PDDocument document, BufferedImage image, float quality, int dpi) throws IOException { // extract alpha channel (if any) BufferedImage awtColorImage = getColorImage(image); BufferedImage awtAlphaImage = getAlphaImage(image); // create XObject ByteArrayOutputStream baos = new ByteArrayOutputStream(); encodeImageToJPEGStream(awtColorImage, quality, dpi, baos); ByteArrayInputStream byteStream = new ByteArrayInputStream(baos.toByteArray()); PDImageXObject pdImage = new PDImageXObject(document, byteStream, COSName.DCT_DECODE, awtColorImage.getWidth(), awtColorImage.getHeight(), awtColorImage.getColorModel().getComponentSize(0), getColorSpaceFromAWT(awtColorImage)); // alpha -> soft mask if (awtAlphaImage != null) { PDImage xAlpha = JPEGFactory.createFromImage(document, awtAlphaImage, quality); pdImage.getCOSObject().setItem(COSName.SMASK, xAlpha); } return pdImage; }
private static PDImageXObject prepareImageXObject(PDDocument document, byte[] byteArray, int width, int height, PDColorSpace initColorSpace) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Filter filter = FilterFactory.INSTANCE.getFilter(COSName.CCITTFAX_DECODE); COSDictionary dict = new COSDictionary(); dict.setInt(COSName.COLUMNS, width); dict.setInt(COSName.ROWS, height); filter.encode(new ByteArrayInputStream(byteArray), baos, dict, 0); ByteArrayInputStream encodedByteStream = new ByteArrayInputStream(baos.toByteArray()); PDImageXObject image = new PDImageXObject(document, encodedByteStream, COSName.CCITTFAX_DECODE, width, height, 1, initColorSpace); dict.setInt(COSName.K, -1); image.getCOSObject().setItem(COSName.DECODE_PARMS, dict); return image; }
private static PDImageXObject createJPEG(PDDocument document, BufferedImage image, float quality, int dpi) throws IOException { // extract alpha channel (if any) BufferedImage awtColorImage = getColorImage(image); BufferedImage awtAlphaImage = getAlphaImage(image); // create XObject ByteArrayOutputStream baos = new ByteArrayOutputStream(); encodeImageToJPEGStream(awtColorImage, quality, dpi, baos); ByteArrayInputStream byteStream = new ByteArrayInputStream(baos.toByteArray()); PDImageXObject pdImage = new PDImageXObject(document, byteStream, COSName.DCT_DECODE, awtColorImage.getWidth(), awtColorImage.getHeight(), awtColorImage.getColorModel().getComponentSize(0), getColorSpaceFromAWT(awtColorImage)); // alpha -> soft mask if (awtAlphaImage != null) { PDImage xAlpha = JPEGFactory.createFromImage(document, awtAlphaImage, quality); pdImage.getCOSObject().setItem(COSName.SMASK, xAlpha); } return pdImage; }
private static PDImageXObject prepareImageXObject(PDDocument document, byte[] byteArray, int width, int height, PDColorSpace initColorSpace) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); Filter filter = FilterFactory.INSTANCE.getFilter(COSName.CCITTFAX_DECODE); COSDictionary dict = new COSDictionary(); dict.setInt(COSName.COLUMNS, width); dict.setInt(COSName.ROWS, height); filter.encode(new ByteArrayInputStream(byteArray), baos, dict, 0); ByteArrayInputStream encodedByteStream = new ByteArrayInputStream(baos.toByteArray()); PDImageXObject image = new PDImageXObject(document, encodedByteStream, COSName.CCITTFAX_DECODE, width, height, 1, initColorSpace); dict.setInt(COSName.K, -1); image.getCOSObject().setItem(COSName.DECODE_PARMS, dict); return image; }
/** * Returns the Soft Mask Image XObject associated with this image, or null if there is none. * @return the SMask Image XObject, or null. * @throws java.io.IOException */ public PDImageXObject getSoftMask() throws IOException { COSStream cosStream = (COSStream) getCOSObject().getDictionaryObject(COSName.SMASK); if (cosStream != null) { // always DeviceGray return new PDImageXObject(new PDStream(cosStream), null); } return null; }
/** * Returns the Soft Mask Image XObject associated with this image, or null if there is none. * @return the SMask Image XObject, or null. * @throws java.io.IOException */ public PDImageXObject getSoftMask() throws IOException { COSStream cosStream = (COSStream) getCOSObject().getDictionaryObject(COSName.SMASK); if (cosStream != null) { // always DeviceGray return new PDImageXObject(new PDStream(cosStream), null); } return null; }
/** * Returns the Mask Image XObject associated with this image, or null if there is none. * @return Mask Image XObject * @throws java.io.IOException */ public PDImageXObject getMask() throws IOException { COSBase mask = getCOSObject().getDictionaryObject(COSName.MASK); if (mask instanceof COSArray) { // color key mask, no explicit mask to return return null; } else { COSStream cosStream = (COSStream) getCOSObject().getDictionaryObject(COSName.MASK); if (cosStream != null) { // always DeviceGray return new PDImageXObject(new PDStream(cosStream), null); } return null; } }