Example usage for org.apache.pdfbox.pdmodel.graphics.image PDImageXObject createFromFileByExtension

List of usage examples for org.apache.pdfbox.pdmodel.graphics.image PDImageXObject createFromFileByExtension

Introduction

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

Prototype

public static PDImageXObject createFromFileByExtension(File file, PDDocument doc) throws IOException 

Source Link

Document

Create a PDImageXObject from an image file.

Usage

From source file:costumetrade.common.util.PdfUtils.java

License:Open Source License

public static PDDocument createImagePdf(File file) {

    PDPageContentStream contentStream = null;
    try {/*from   w w w. jav  a  2 s. c om*/
        BufferedImage bimg = ImageIO.read(file);
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(new PDRectangle(bimg.getWidth(), bimg.getHeight()));
        document.addPage(page);
        PDImageXObject image = PDImageXObject.createFromFileByExtension(file, document);
        contentStream = new PDPageContentStream(document, page);
        contentStream.drawImage(image, 0, 0);
        return document;
    } catch (IOException e) {
        throw new RuntimeException("?PDF", e);
    } finally {
        IOUtils.closeQuietly(contentStream);
    }

}