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; }
return JPEGFactory.createFromStream(doc, fis);
/** * Creates a new JPEG Image XObject from an input stream containing JPEG data. * * The input stream data will be preserved and embedded in the PDF file without modification. * @param document the document where the image will be created * @param stream a stream of JPEG data * @return a new Image XObject * * @throws IOException if the input stream cannot be read */ public static PDImageXObject createFromStream(PDDocument document, InputStream stream) throws IOException { return createFromByteArray(document, IOUtils.toByteArray(stream)); }
/** * Creates a new JPEG PDImageXObject from a BufferedImage. * <p> * Do not read a JPEG image from a stream/file and call this method; you'll get more speed and * quality by calling {@link #createFromStream(org.apache.pdfbox.pdmodel.PDDocument, * java.io.InputStream) createFromStream()} instead. * * @param document the document where the image will be created * @param image the BufferedImage to embed * @return a new Image XObject * @throws IOException if the JPEG data cannot be written */ public static PDImageXObject createFromImage(PDDocument document, BufferedImage image) throws IOException { return createFromImage(document, image, 0.75f); }
/** * Creates a new JPEG Image XObject from a BufferedImage, a given quality and dpi metadata. * <p> * Do not read a JPEG image from a stream/file and call this method; you'll get more speed and * quality by calling {@link #createFromStream(org.apache.pdfbox.pdmodel.PDDocument, * java.io.InputStream) createFromStream()} instead. * * @param document the document where the image will be created * @param image the BufferedImage to embed * @param quality The desired JPEG compression quality; between 0 (best * compression) and 1 (best image quality). See * {@link ImageWriteParam#setCompressionQuality(float)} for more details. * @param dpi the desired dpi (resolution) value of the JPEG to be stored in metadata. This * value has no influence on image content or size. * @return a new Image XObject * @throws IOException if the JPEG data cannot be written */ public static PDImageXObject createFromImage(PDDocument document, BufferedImage image, float quality, int dpi) throws IOException { return createJPEG(document, image, quality, dpi); }
imageWriter = getJPEGImageWriter(); ios = ImageIO.createImageOutputStream(out); imageWriter.setOutput(ios);
Raster raster = readJPEGRaster(byteStream); byteStream.reset();
/** * Creates a new JPEG PDImageXObject from a BufferedImage and a given quality. * <p> * Do not read a JPEG image from a stream/file and call this method; you'll get more speed and * quality by calling {@link #createFromStream(org.apache.pdfbox.pdmodel.PDDocument, * java.io.InputStream) createFromStream()} instead. * * The image will be created with a dpi value of 72 to be stored in metadata. * @param document the document where the image will be created * @param image the BufferedImage to embed * @param quality The desired JPEG compression quality; between 0 (best * compression) and 1 (best image quality). See * {@link ImageWriteParam#setCompressionQuality(float)} for more details. * @return a new Image XObject * @throws IOException if the JPEG data cannot be written */ public static PDImageXObject createFromImage(PDDocument document, BufferedImage image, float quality) throws IOException { return createFromImage(document, image, quality, 72); }
/** * Creates a new JPEG Image XObject from a BufferedImage, a given quality and dpi metadata. * <p> * Do not read a JPEG image from a stream/file and call this method; you'll get more speed and * quality by calling {@link #createFromStream(org.apache.pdfbox.pdmodel.PDDocument, * java.io.InputStream) createFromStream()} instead. * * @param document the document where the image will be created * @param image the BufferedImage to embed * @param quality the desired JPEG compression quality * @param dpi the desired dpi (resolution) value of the JPEG to be stored in metadata. This * value has no influence on image content or size. * @return a new Image XObject * @throws IOException if the JPEG data cannot be written */ public static PDImageXObject createFromImage(PDDocument document, BufferedImage image, float quality, int dpi) throws IOException { return createJPEG(document, image, quality, dpi); }
imageWriter = getJPEGImageWriter(); ios = ImageIO.createImageOutputStream(out); imageWriter.setOutput(ios);
Raster raster = readJPEGRaster(byteStream); byteStream.reset();
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; }
return JPEGFactory.createFromStream(doc, fis);
/** * Creates a new JPEG PDImageXObject from a BufferedImage. * <p> * Do not read a JPEG image from a stream/file and call this method; you'll get more speed and * quality by calling {@link #createFromStream(org.apache.pdfbox.pdmodel.PDDocument, * java.io.InputStream) createFromStream()} instead. * * @param document the document where the image will be created * @param image the BufferedImage to embed * @return a new Image XObject * @throws IOException if the JPEG data cannot be written */ public static PDImageXObject createFromImage(PDDocument document, BufferedImage image) throws IOException { return createFromImage(document, image, 0.75f); }
return JPEGFactory.createFromByteArray(document, byteArray);
/** * Creates a new JPEG Image XObject from a BufferedImage, a given quality and dpi metadata. * <p> * Do not read a JPEG image from a stream/file and call this method; you'll get more speed and * quality by calling {@link #createFromStream(org.apache.pdfbox.pdmodel.PDDocument, * java.io.InputStream) createFromStream()} instead. * * @param document the document where the image will be created * @param image the BufferedImage to embed * @param quality The desired JPEG compression quality; between 0 (best * compression) and 1 (best image quality). See * {@link ImageWriteParam#setCompressionQuality(float)} for more details. * @param dpi the desired dpi (resolution) value of the JPEG to be stored in metadata. This * value has no influence on image content or size. * @return a new Image XObject * @throws IOException if the JPEG data cannot be written */ public static PDImageXObject createFromImage(PDDocument document, BufferedImage image, float quality, int dpi) throws IOException { return createJPEG(document, image, quality, dpi); }
imageWriter = getJPEGImageWriter(); ios = ImageIO.createImageOutputStream(out); imageWriter.setOutput(ios);
Raster raster = readJPEGRaster(byteStream); byteStream.reset();
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; }
PDImageXObject imageXObject = JPEGFactory.createFromStream(doc, fis); fis.close(); return imageXObject;