Java Code Examples for org.apache.pdfbox.rendering.PDFRenderer#renderImage()

The following examples show how to use org.apache.pdfbox.rendering.PDFRenderer#renderImage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PdfRenderer.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
public static BufferedImage create(PDDocument pdf, int pageIndex, float scale, String textToHighlight) {
    try {
        PDFRenderer renderer = new PDFRenderer(pdf);
        scale = scale * Toolkit.getDefaultToolkit().getScreenResolution() / 72.0f;
        BufferedImage img = renderer.renderImage(pageIndex, scale);
        if (textToHighlight != null) {
            Graphics2D gc = img.createGraphics();
            gc.setStroke(new BasicStroke(0.1f));
            gc.scale(scale, scale);
            PdfRenderer processor = new PdfRenderer(gc, textToHighlight);
            processor.setSortByPosition(true);
            processor.setStartPage(pageIndex + 1);
            processor.setEndPage(pageIndex + 1);
            try (DummyWriter writer = new DummyWriter()) {
                processor.writeText(pdf, writer);
            }
            gc.dispose();
        }
        return img;
    } catch (Exception exception) {
        Log.error(exception);
        return null;
    }
}
 
Example 2
Source File: MCRPdfThumbnailGenerator.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
@Override
public Optional<BufferedImage> getThumbnail(MCRPath path, int size) throws IOException {
    try (InputStream fileIS = Files.newInputStream(path); PDDocument pdf = PDDocument.load(fileIS)) {
        final PDPage page = resolveOpenActionPage(pdf);
        float pdfWidth = page.getCropBox().getWidth();
        float pdfHeight = page.getCropBox().getHeight();
        final int newWidth = pdfWidth > pdfHeight ? (int) Math.ceil(size * pdfWidth / pdfHeight) : size;
        final float scale = newWidth / pdfWidth;

        PDFRenderer pdfRenderer = new PDFRenderer(pdf);
        BufferedImage pdfRender = pdfRenderer.renderImage(pdf.getPages().indexOf(page), scale);
        int imageType = MCRThumbnailUtils.getImageType(pdfRender);
        if (imageType == BufferedImage.TYPE_BYTE_BINARY || imageType == BufferedImage.TYPE_BYTE_GRAY) {
            BufferedImage thumbnail = new BufferedImage(pdfRender.getWidth(), pdfRender.getHeight(),
                imageType);
            Graphics g = thumbnail.getGraphics();
            g.drawImage(pdfRender, 0, 0, null);
            g.dispose();
            return Optional.of(thumbnail);
        }
        return Optional.of(pdfRender);
    }
}
 
Example 3
Source File: SetCropBox.java    From testarea-pdfbox2 with Apache License 2.0 6 votes vote down vote up
/**
 * <a href="http://stackoverflow.com/questions/39689004/pdfbox-2-0-3-set-cropbox-using-textposition-coordinates">
 * PDFBox 2.0.3 Set cropBox using TextPosition coordinates
 * </a>
 * <br/>
 * <a href="http://downloadcenter.samsung.com/content/UM/201504/20150407095631744/ENG-US_NMATSCJ-1.103-0330.pdf">
 * ENG-US_NMATSCJ-1.103-0330.pdf
 * </a>
 * <p>
 * This test shows how to set the crop box on page twelve and render the cropped page as image.
 * </p>
 */
@Test
public void testSetCropBoxImgENG_US_NMATSCJ_1_103_0330() throws IOException
{
    try (   InputStream resource = getClass().getResourceAsStream("ENG-US_NMATSCJ-1.103-0330.pdf"))
    {
        PDDocument pdDocument = Loader.loadPDF(resource);
        PDPage page = pdDocument.getPage(12-1);
        page.setCropBox(new PDRectangle(40f, 680f, 510f, 100f));

        PDFRenderer renderer = new PDFRenderer(pdDocument);
        BufferedImage img = renderer.renderImage(12 - 1, 4f);
        ImageIOUtil.writeImage(img, new File(RESULT_FOLDER, "ENG-US_NMATSCJ-1.103-0330-page12cropped.jpg").getAbsolutePath(), 300);
        pdDocument.close();
    }
}
 
Example 4
Source File: PDFPreviewBuilder.java    From Open-LaTeX-Studio with MIT License 6 votes vote down vote up
public static Image buildPDFPreview(int page, int zoom) {
    float newScale = ((float) zoom) / 100.0f;
    BufferedImage pageImage = null;
    
    try (PDDocument pdDocument = PDFService.getPDDocument()) {
        PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);
        if (pdDocument.getNumberOfPages() >= page && page > 0){
            pageImage = pdfRenderer.renderImage(page - 1);
            int width = (int) (newScale * pageImage.getWidth());
            int height = (int) (newScale * pageImage.getHeight());
        
            return pageImage.getScaledInstance(width, height, SCALE_TYPE);
        }
        
                    
        return pageImage;
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
    
    return null;
}
 
Example 5
Source File: PdfInformation.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public BufferedImage readPageAsImage(int page, ImageType imageType) {
    try {
        if (doc == null) {
            return null;
        }
        PDFRenderer renderer = new PDFRenderer(doc);
        BufferedImage image = renderer.renderImage(page, 1, imageType);
        return image;
    } catch (Exception e) {
        return null;
    }
}
 
Example 6
Source File: PdfTools.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static BufferedImage page2image(File file, int page) {
    try {
        try ( PDDocument doc = PDDocument.load(file, null, AppVariables.pdfMemUsage)) {
            PDFRenderer renderer = new PDFRenderer(doc);
            BufferedImage image = renderer.renderImage(page, 1, ImageType.ARGB);
            doc.close();
            return image;
        }
    } catch (Exception e) {
        return null;
    }
}
 
Example 7
Source File: PdfTools.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static BufferedImage page2image(File file, String password, int page,
        float scale, ImageType imageType) {
    try {
        try ( PDDocument doc = PDDocument.load(file, password, AppVariables.pdfMemUsage)) {
            PDFRenderer renderer = new PDFRenderer(doc);
            BufferedImage image = renderer.renderImage(page, scale, imageType);
            doc.close();
            return image;
        }
    } catch (Exception e) {
        return null;
    }
}
 
Example 8
Source File: PdfScreenshotUtils.java    From dss with GNU Lesser General Public License v2.1 4 votes vote down vote up
private static BufferedImage getRendering(DSSDocument dssDoc) throws IOException {
	try (InputStream is = dssDoc.openStream(); PDDocument doc = PDDocument.load(is)) {
		PDFRenderer renderer = new PDFRenderer(doc);
		return renderer.renderImage(0);
	}
}
 
Example 9
Source File: MCRPDFTools.java    From mycore with GNU General Public License v3.0 3 votes vote down vote up
/**
 * This method returns a Buffered Image as thumbnail if an initial page was set,
 * it will be return - if not the first page
 *
 * @param thumbnailSize - the size: size = max(width, height) 
 *                        a size &lt; 0 will return the original size and centered parameter will be ignored
 * @param pdfFile       - the file from which the thumbnail will be taken
 * @param centered      - if true, a square (thumbnail with same width and
 *                      height) will be returned
 * @return a BufferedImage as thumbnail
 *
 * @throws IOException
 */
public static BufferedImage getThumbnail(int thumbnailSize, Path pdfFile, boolean centered) throws IOException {
    InputStream fileIS = Files.newInputStream(pdfFile);
    try (PDDocument pdf = PDDocument.load(fileIS)) {
        PDFRenderer pdfRenderer = new PDFRenderer(pdf);
        final PDPage page = resolveOpenActionPage(pdf);
        BufferedImage level1Image = pdfRenderer.renderImage(pdf.getPages().indexOf(page));
        if (thumbnailSize < 0) {
            return level1Image;
        }
        return scalePage(thumbnailSize, centered, level1Image);
    }
}