Example usage for org.apache.pdfbox.pdmodel.graphics.image LosslessFactory createFromImage

List of usage examples for org.apache.pdfbox.pdmodel.graphics.image LosslessFactory createFromImage

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.graphics.image LosslessFactory createFromImage.

Prototype

public static PDImageXObject createFromImage(PDDocument document, BufferedImage image) throws IOException 

Source Link

Document

Creates a new lossless encoded image XObject from a BufferedImage.

Usage

From source file:org.gfbio.idmg.util.PDFUtil.java

private void printGFBioLogo() {
    // Get GFBio Logo and print it to pdf document
    try {/*from  ww  w.  j ava  2s.  c  om*/
        URL url = new URL(themePath + "/gfbiologo-transparent_122_60.png");
        BufferedImage awtImage = ImageIO.read(url);
        if (awtImage != null) {
            PDImageXObject pdImage = LosslessFactory.createFromImage(document, awtImage);
            content.drawImage(pdImage, 450, 710, 122, 60);
        }
    } catch (IOException e) {
        _log.error("GFBio Logo couldn't be found!", e);
    }
}

From source file:richtercloud.document.scanner.gui.DefaultMainPanel.java

License:Open Source License

@Override
public void exportActiveDocumentItem(OutputStream out, int exportFormat)
        throws IOException, ImageWrapperException {
    if (exportFormat == EXPORT_FORMAT_PDF) {
        //There seems to be no PNG support in Apache PDFBox, but
        //transforming into JPEG isn't too much of an effort and allows to
        //limit dependencies to Apache PDFBox
        PDDocument document = new PDDocument();
        PDRectangle documentRectangle = PDRectangle.A4;
        for (OCRSelectPanel oCRSelectPanel : oCRSelectComponent.getoCRSelectPanelPanel().getoCRSelectPanels()) {
            ImageWrapper imageWrapper = oCRSelectPanel.getImage();
            PDPage page = new PDPage(documentRectangle);
            document.addPage(page);/*  www  . ja  v  a  2 s  .  c  om*/
            //@TODO: figure out how to create PDImageXObject from stream
            //since this was possible in 1.8 and it's unlikely that there's
            //such a severe regression
            InputStream inputStream = imageWrapper.getOriginalImageStream();
            if (inputStream == null) {
                //cache has been shut down
                return;
            }
            BufferedImage awtImage = ImageIO.read(inputStream);
            PDImageXObject pdImageXObject = LosslessFactory.createFromImage(document, awtImage);
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            contentStream.drawImage(pdImageXObject, 0, 0, documentRectangle.getWidth(),
                    documentRectangle.getHeight());
            //in case width and height exceed the size of
            //documentRectangle, the page is empty (or the content might
            //be placed outside the page which has the same effect)
            contentStream.setFont(PDType1Font.COURIER, 10);
            contentStream.close();
        }
        document.save(out);
        document.close();
        out.flush();
        out.close();
    } else {
        throw new IllegalArgumentException("export format %s isn't supported");
    }
}