Example usage for org.apache.pdfbox.pdmodel.common PDRectangle getHeight

List of usage examples for org.apache.pdfbox.pdmodel.common PDRectangle getHeight

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.common PDRectangle getHeight.

Prototype

public float getHeight() 

Source Link

Document

This will get the height of this rectangle as calculated by upperRightY - lowerLeftY.

Usage

From source file:org.fit.cssbox.pdf.CSSBoxTree.java

License:Open Source License

/**
 * Creates a style definition used for pages.
 * @return The page style definition./*from w  ww .  ja va 2  s  . c  o m*/
 */
protected NodeData createPageStyle() {
    NodeData ret = createBlockStyle();
    TermFactory tf = CSSFactory.getTermFactory();
    ret.push(createDeclaration("position", tf.createIdent("relative")));
    ret.push(createDeclaration("border-width", tf.createLength(1f, Unit.px)));
    ret.push(createDeclaration("border-style", tf.createIdent("solid")));
    ret.push(createDeclaration("border-color", tf.createColor(0, 0, 255)));
    ret.push(createDeclaration("margin", tf.createLength(0.5f, Unit.em)));

    PDRectangle layout = getCurrentMediaBox();
    if (layout != null) {
        float w = layout.getWidth();
        float h = layout.getHeight();
        final int rot = pdpage.getRotation();
        if (rot == 90 || rot == 270) {
            float x = w;
            w = h;
            h = x;
        }

        ret.push(createDeclaration("width", tf.createLength(w, unit)));
        ret.push(createDeclaration("height", tf.createLength(h, unit)));
    } else
        log.warn("No media box found");

    return ret;
}

From source file:org.fit.pdfdom.PDFBoxTree.java

License:Open Source License

protected AffineTransform createCurrentPageTransformation() {
    PDRectangle cb = pdpage.getCropBox();
    AffineTransform pageTransform = new AffineTransform();

    switch (pdpage.getRotation()) {
    case 90://w  w w.  j a va 2 s. co m
        pageTransform.translate(cb.getHeight(), 0);
        break;
    case 180:
        pageTransform.translate(cb.getWidth(), cb.getHeight());
        break;
    case 270:
        pageTransform.translate(0, cb.getWidth());
        break;
    }

    pageTransform.rotate(Math.toRadians(pdpage.getRotation()));
    pageTransform.translate(0, cb.getHeight());
    pageTransform.scale(1, -1);
    pageTransform.translate(-cb.getLowerLeftX(), -cb.getLowerLeftY());

    return pageTransform;
}

From source file:org.fit.pdfdom.PDFDomTree.java

License:Open Source License

/**
 * Creates an element that represents a single page.
 * @return the resulting DOM element/*w ww .  j  a  va 2 s  .  c o m*/
 */
protected Element createPageElement() {
    String pstyle = "";
    PDRectangle layout = getCurrentMediaBox();
    if (layout != null) {
        /*System.out.println("x1 " + layout.getLowerLeftX());
        System.out.println("y1 " + layout.getLowerLeftY());
        System.out.println("x2 " + layout.getUpperRightX());
        System.out.println("y2 " + layout.getUpperRightY());
        System.out.println("rot " + pdpage.findRotation());*/

        float w = layout.getWidth();
        float h = layout.getHeight();
        final int rot = pdpage.getRotation();
        if (rot == 90 || rot == 270) {
            float x = w;
            w = h;
            h = x;
        }

        pstyle = "width:" + w + UNIT + ";" + "height:" + h + UNIT + ";";
        pstyle += "overflow:hidden;";
    } else
        log.warn("No media box found");

    Element el = doc.createElement("div");
    el.setAttribute("id", "page_" + (pagecnt++));
    el.setAttribute("class", "page");
    el.setAttribute("style", pstyle);
    return el;
}

From source file:org.github.jipsg.pdfbox.PdfToImageConverter.java

License:Apache License

/**
 * Split a PDF document into images.//www . j a  va 2 s.  c  om
 *
 * @param pdDocument  the source document
 * @param imageFormat the requested image format, e.g. "jpeg"
 * @param startPage   the first extracted page
 * @param endPage     the las extracted page
 * @param resolution  the resolution of the extracted images
 * @param color       the color model, e.g. "rgb", "gray"
 * @return a list of images
 * @throws Exception the conversion failed
 */
@SuppressWarnings("unchecked")
public List<BufferedImage> toImages(PDDocument pdDocument, String imageFormat, int startPage, int endPage,
        int resolution, String color) throws Exception {
    /**
     Validate.notNull(pdDocument, "pdDocument is null");
     Validate.notEmpty(imageFormat, "imageFormat is null");
     Validate.isTrue(startPage > 0, "invalid start page : " + startPage);
     Validate.isTrue(endPage >= startPage, "invalid end page : " + endPage);
     Validate.isTrue(resolution >= 0, "invalid resolution : " + resolution);
     */

    List<BufferedImage> result = new ArrayList<BufferedImage>();

    int imageType = getImageType(color);
    List<PDPage> pages = pdDocument.getDocumentCatalog().getAllPages();
    int pagesSize = pages.size();

    for (int i = startPage - 1; i < endPage && i < pagesSize; i++) {
        PDPage page = pages.get(i);
        PDRectangle cropBox = page.findCropBox();
        int currResolution = calculateResolution(resolution, cropBox.getWidth(), cropBox.getHeight());
        BufferedImage image = page.convertToImage(imageType, currResolution);
        result.add(image);
    }

    return result;
}

From source file:org.haplo.component.pdfbox.PDF.java

License:Mozilla Public License

/**
 * Open a PDF and read it's data. close() must be called to clean up nicely.
 *//*from w ww  . j a v  a2 s.  c om*/
public PDF(String filename) throws IOException {
    if (!Operation.isThreadMarkedAsWorker()) {
        throw new RuntimeException("PDF manipulation can only be performed in a worker process");
    }

    // Not valid by default
    isValid = false;

    // Try to load the page
    try {
        // Open the PDF for reading
        this.pdf = PDDocument.load(new File(filename));

        this.numberOfPages = this.pdf.getNumberOfPages();

        PDPage page = this.pdf.getPage(0);

        // Width and height
        PDRectangle cropBox = page.getCropBox();
        width = (int) cropBox.getWidth();
        height = (int) cropBox.getHeight();

        isValid = true;
    } catch (Exception e) {
        // Ignore exception, but do clean up nicely
        close();
    }
}

From source file:org.haplo.component.pdfbox.PDF.java

License:Mozilla Public License

/**
 * Render the PDF as an image/*  www .  j  a v  a  2  s .  com*/
 */
public void render(String outFilename, String outFormat, int page, int outWidth, int outHeight)
        throws IOException {
    BufferedImage img = null;
    try {
        PDPage pdfPage = this.pdf.getPage(page - 1);
        PDRectangle cropBox = pdfPage.getCropBox();
        int pageWidth = (int) cropBox.getWidth();
        int pageHeight = (int) cropBox.getHeight();
        if (pageHeight <= 0) {
            pageHeight = 1;
        }

        int resolution = (96 * outHeight) / pageHeight;
        if (resolution < 4) {
            resolution = 4;
        }
        if (resolution > 1000) {
            resolution = 1000;
        }
        if (outHeight < 100 || outWidth < 100) {
            resolution *= 2;
        }

        PDFRenderer pdfRenderer = new PDFRenderer(this.pdf);

        img = pdfRenderer.renderImageWithDPI(page - 1, resolution,
                outFormat.equals("png") ? ImageType.ARGB : ImageType.RGB);
    } catch (Exception e) {
        Logger.getLogger("org.haplo.app").error("Error rendering PDF: " + e.toString());
        throw new RuntimeException("Couldn't render PDF page", e);
    }

    // Did it convert? (most likely cause of null return is requested a page which didn't exist)
    if (img == null) {
        throw new RuntimeException("Failed to render PDF - did the requested page exist?");
    }

    // Scale the image to the right size
    BufferedImage original = null;
    if (img.getWidth() != outWidth || img.getHeight() != outHeight) {
        original = img;
        Image scaled = img.getScaledInstance(outWidth, outHeight, Image.SCALE_SMOOTH);
        img = new BufferedImage(outWidth, outHeight, original.getType());
        Graphics2D graphics = img.createGraphics();
        graphics.setBackground(Color.WHITE);
        graphics.clearRect(0, 0, outWidth, outHeight);
        graphics.drawImage(scaled, 0, 0, null);
        graphics.dispose();
        scaled.flush();
    }

    // Write the image to a file
    ImageIO.write(img, outFormat, new File(outFilename));

    // Free resources
    img.flush();
    if (original != null) {
        original.flush();
    }
}

From source file:org.mycore.media.MCRMediaPDFParser.java

License:Open Source License

/**
 * Parse file and store metadata in related Object.
 * /*from  w w w. j ava 2s .  com*/
 * @return MCRMediaObject
 *              can be held any MCRMediaObject
 * @see MCRMediaObject#clone()
 */
@SuppressWarnings("unchecked")
public synchronized MCRMediaObject parse(File file) throws Exception {
    if (!file.exists())
        throw new IOException("File \"" + file.getName() + "\" doesn't exists!");

    MCRPDFObject media = new MCRPDFObject();

    LOGGER.info("parse " + file.getName() + "...");

    PDDocument pdf = PDDocument.load(file);
    try {
        media.fileName = file.getName();
        media.fileSize = file.length();
        media.folderName = (file.getAbsolutePath()).replace(file.getName(), "");

        PDPageTree pages = pdf.getDocumentCatalog().getPages();

        media.numPages = pdf.getNumberOfPages();

        PDPage page = (PDPage) pages.get(0);
        PDRectangle rect = page.getMediaBox();

        media.width = Math.round(rect.getWidth());
        media.height = Math.round(rect.getHeight());

        PDDocumentInformation info = pdf.getDocumentInformation();
        if (info != null) {
            media.tags = new MCRMediaTagObject();
            media.tags.author = info.getAuthor();
            media.tags.creator = info.getCreator();
            media.tags.producer = info.getProducer();
            media.tags.title = info.getTitle();
            media.tags.subject = info.getSubject();
            media.tags.keywords = info.getKeywords();
        }
    } catch (Exception e) {
        LOGGER.error(e.getMessage());
        throw new Exception(e.getMessage());
    } finally {
        pdf.close();
    }

    return media;
}

From source file:org.nmrfx.processor.gui.graphicsio.PDFWriter.java

License:Open Source License

public void create(boolean landScape, double width, double height, String fileName) throws GraphicsIOException {
    // the document
    this.landScape = landScape;
    this.fileName = fileName;
    doc = new PDDocument();
    try {// w w  w.ja v a2  s.  c o m
        PDPage page = new PDPage(PDRectangle.LETTER);
        doc.addPage(page);
        PDRectangle pageSize = page.getMediaBox();
        pageWidth = pageSize.getWidth();
        pageHeight = pageSize.getHeight();
        contentStream = new PDPageContentStream(doc, page, false, false);
        // add the rotation using the current transformation matrix
        // including a translation of pageWidth to use the lower left corner as 0,0 reference
        if (landScape) {
            page.setRotation(90);
            contentStream.transform(new Matrix(0, 1, -1, 0, pageWidth, 0));
        }
    } catch (IOException ioE) {
        throw new GraphicsIOException(ioE.getMessage());
    }
}

From source file:org.nuxeo.pdf.PDFInfo.java

License:Open Source License

/**
 * After building the object with the correct constructor, and after
 * possibly having set some parsing property (<code>setParseWithXMP()</code>
 * for example), this method will extract the information from the PDF.
 * <p>// ww w  .j  av  a2  s . c  o  m
 * After extraction, caller get the info: Either all of them (
 * <code>toHashMap()</code> or <code>toString()</code>) or individual info
 * (see all getters)
 *
 * @throws ClientException
 *
 * @since 5.9.5
 */
public void run() throws ClientException {

    // In case the caller calls several time the run() method
    if (!alreadyParsed) {

        fileName = pdfBlob.getFilename();
        // Getting the file size os ok only if the blob is already backed by
        // a
        // File. If it is pure Stream, we give up
        File pdfFile = BlobHelper.getFileFromBlob(pdfBlob);
        if (pdfFile == null) {
            fileSize = -1;
        } else {
            fileSize = pdfFile.length();
        }

        try {
            pdfDoc = PDDocument.load(pdfBlob.getStream());

            isEncrypted = pdfDoc.isEncrypted();
            if (isEncrypted) {
                pdfDoc.openProtection(new StandardDecryptionMaterial(password));
            }

            numberOfPages = pdfDoc.getNumberOfPages();
            PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog();
            pageLayout = checkNotNull(docCatalog.getPageLayout());
            pdfVersion = "" + pdfDoc.getDocument().getVersion();

            PDDocumentInformation docInfo = pdfDoc.getDocumentInformation();
            author = checkNotNull(docInfo.getAuthor());
            contentCreator = checkNotNull(docInfo.getCreator());
            keywords = checkNotNull(docInfo.getKeywords());
            creationDate = docInfo.getCreationDate();
            modificationDate = docInfo.getModificationDate();
            producer = checkNotNull(docInfo.getProducer());
            subject = checkNotNull(docInfo.getSubject());
            title = checkNotNull(docInfo.getTitle());

            // Getting dimension is a bit tricky
            mediaBoxWidthInPoints = -1;
            mediaBoxHeightInPoints = -1;
            cropBoxWidthInPoints = -1;
            cropBoxHeightInPoints = -1;
            List<PDPage> allPages = docCatalog.getAllPages();
            boolean gotMediaBox = false;
            boolean gotCropBox = false;
            for (PDPage page : allPages) {

                if (page != null) {
                    PDRectangle r = page.findMediaBox();
                    if (r != null) {
                        mediaBoxWidthInPoints = r.getWidth();
                        mediaBoxHeightInPoints = r.getHeight();
                        gotMediaBox = true;
                    }
                    r = page.findCropBox();
                    if (r != null) {
                        cropBoxWidthInPoints = r.getWidth();
                        cropBoxHeightInPoints = r.getHeight();
                        gotCropBox = true;
                    }
                }
                if (gotMediaBox && gotCropBox) {
                    break;
                }
            }

            if (doXMP) {
                xmp = null;
                PDMetadata metadata = docCatalog.getMetadata();
                if (metadata != null) {
                    xmp = "";
                    InputStream xmlInputStream = metadata.createInputStream();

                    InputStreamReader isr = new InputStreamReader(xmlInputStream);
                    BufferedReader reader = new BufferedReader(isr);
                    String line;
                    do {
                        line = reader.readLine();
                        if (line != null) {
                            xmp += line + "\n";
                        }
                    } while (line != null);
                    reader.close();
                }
            }

        } catch (IOException | BadSecurityHandlerException | CryptographyException e) {
            throw new ClientException(/*
                                       * "Cannot get PDF info: " +
                                       * e.getMessage(),
                                       */e);
        } finally {
            if (pdfDoc != null) {
                try {
                    pdfDoc.close();
                } catch (IOException e) {
                    // Ignore
                }
                pdfDoc = null;
            }
            alreadyParsed = true;
        }
    }
}

From source file:org.nuxeo.pdf.PDFLinks.java

License:Apache License

protected void loadAndPreflightPdf() throws NuxeoException {

    if (pdfDoc == null) {
        pdfDoc = PDFUtils.load(pdfBlob, password);

        @SuppressWarnings("unchecked")
        List<PDPage> allPages = pdfDoc.getDocumentCatalog().getAllPages();
        try {//from   w  ww. ja  va2 s . co  m
            stripper = new PDFTextStripperByArea();
            for (PDPage page : allPages) {
                List<PDAnnotation> annotations = page.getAnnotations();
                for (int j = 0; j < annotations.size(); j++) {
                    PDAnnotation annot = (PDAnnotation) annotations.get(j);
                    if (annot instanceof PDAnnotationLink) {
                        PDAnnotationLink link = (PDAnnotationLink) annot;
                        PDRectangle rect = link.getRectangle();
                        // need to reposition link rectangle to match text space
                        float x = rect.getLowerLeftX();
                        float y = rect.getUpperRightY();
                        float width = rect.getWidth();
                        float height = rect.getHeight();
                        int rotation = page.findRotation();
                        if (rotation == 0) {
                            PDRectangle pageSize = page.findMediaBox();
                            y = pageSize.getHeight() - y;
                        } else if (rotation == 90) {
                            // do nothing
                        }

                        Rectangle2D.Float awtRect = new Rectangle2D.Float(x, y, width, height);
                        stripper.addRegion("" + j, awtRect);
                    }
                }
            }
        } catch (IOException e) {
            throw new NuxeoException("Cannot prefilght and prepare regions", e);
        }
    }
}