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

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

Introduction

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

Prototype

public float getLowerLeftX() 

Source Link

Document

This will get the lower left x coordinate.

Usage

From source file:org.xmlcml.pdf2svg.PDFPage2SVGConverter.java

License:Apache License

/**
 * DUPLICATE OF SUPER SO WE CAN DEBUG/*w w  w .  j a v a  2s.  c  o  m*/
 * This will draw the page to the requested context.
 *
 * @param g The graphics context to draw onto.
 * @param p The page to draw.
 * @param pageDimension The size of the page to draw.
 *
 * @throws IOException If there is an IO error while drawing the page.
 */
public void drawPage(Graphics g, PDPage p, Dimension pageDimension) throws IOException {
    super.drawPage(g, p, pageDimension);
    // cannot use this because private
    // graphics = (Graphics2D)g;
    Graphics2D g2d = (Graphics2D) g;
    //           g2d = (Graphics2D)g;
    page = p;
    pageSize = pageDimension;
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
    // Only if there is some content, we have to process it. 
    // Otherwise we are done here and we will produce an empty page
    if (page.getContents() != null) {
        PDResources resources = page.findResources();
        processStream(page, resources, page.getContents().getStream());
    }
    List annotations = page.getAnnotations();
    if (annotations.size() > 0) {
        throw new RuntimeException("ANNOTATIONS");
    }
    for (int i = 0; i < annotations.size(); i++) {
        PDAnnotation annot = (PDAnnotation) annotations.get(i);
        PDRectangle rect = annot.getRectangle();
        String appearanceName = annot.getAppearanceStream();
        PDAppearanceDictionary appearDictionary = annot.getAppearance();
        if (appearDictionary != null) {
            if (appearanceName == null) {
                appearanceName = "default";
            }
            Map appearanceMap = appearDictionary.getNormalAppearance();
            if (appearanceMap != null) {
                PDAppearanceStream appearance = (PDAppearanceStream) appearanceMap.get(appearanceName);
                if (appearance != null) {
                    g.translate((int) rect.getLowerLeftX(), (int) -rect.getLowerLeftY());
                    processSubStream(page, appearance.getResources(), appearance.getStream());
                    g.translate((int) -rect.getLowerLeftX(), (int) +rect.getLowerLeftY());
                }
            }
        }
    }
}

From source file:org.xwiki.test.misc.PDFTest.java

License:Open Source License

/**
 * Code adapted from http://www.docjar.com/html/api/org/apache/pdfbox/examples/pdmodel/PrintURLs.java.html
 *//*from  ww w  . j av  a  2 s  .c o  m*/
private Map<String, PDAction> extractLinks(PDPage page) throws Exception {
    Map<String, PDAction> links = new HashMap<String, PDAction>();
    PDFTextStripperByArea stripper = new PDFTextStripperByArea();
    List<PDAnnotation> annotations = page.getAnnotations();
    // First setup the text extraction regions.
    for (int j = 0; j < annotations.size(); j++) {
        PDAnnotation annotation = annotations.get(j);
        if (annotation instanceof PDAnnotationLink) {
            PDAnnotationLink link = (PDAnnotationLink) annotation;
            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.getRotation();
            if (rotation == 0) {
                PDRectangle pageSize = page.getMediaBox();
                y = pageSize.getHeight() - y;
            } else if (rotation == 90) {
                // Do nothing.
            }

            Rectangle2D.Float awtRect = new Rectangle2D.Float(x, y, width, height);
            stripper.addRegion(String.valueOf(j), awtRect);
        }
    }

    stripper.extractRegions(page);

    for (int j = 0; j < annotations.size(); j++) {
        PDAnnotation annotation = annotations.get(j);
        if (annotation instanceof PDAnnotationLink) {
            PDAnnotationLink link = (PDAnnotationLink) annotation;
            String label = stripper.getTextForRegion(String.valueOf(j)).trim();
            links.put(label, link.getAction());
        }
    }

    return links;
}

From source file:paper2ebook.Transformer.java

License:Apache License

/**
 * Heuristic search of the list of interesting areas in page, returned by
 * natural read order./* www.java2 s  .  c  om*/
 */
public List<PDRectangle> getFragments(PDPage page) {
    List<PDRectangle> fragments = new ArrayList<PDRectangle>();

    // TODO: naive 2 columns hack: rewrite me to introspect the document
    // structure instead

    PDRectangle origBox = page.findCropBox();
    float width = origBox.getWidth();
    float height = origBox.getHeight();

    // top left
    PDRectangle box = new PDRectangle();
    box.setLowerLeftX(origBox.getLowerLeftX());
    box.setLowerLeftY(origBox.getLowerLeftY() + height / 2);
    box.setUpperRightX(origBox.getUpperRightX() / 2);
    box.setUpperRightY(origBox.getUpperRightY());
    fragments.add(box);

    // bottom left
    box = new PDRectangle();
    box.setLowerLeftX(origBox.getLowerLeftX());
    box.setLowerLeftY(origBox.getLowerLeftY());
    box.setUpperRightX(origBox.getUpperRightX() / 2);
    box.setUpperRightY(origBox.getUpperRightY() / 2);
    fragments.add(box);

    // top right
    box = new PDRectangle();
    box.setLowerLeftX(origBox.getLowerLeftX() + width / 2);
    box.setLowerLeftY(origBox.getLowerLeftY() + height / 2);
    box.setUpperRightX(origBox.getUpperRightX());
    box.setUpperRightY(origBox.getUpperRightY());
    fragments.add(box);

    // bottom right
    box = new PDRectangle();
    box.setLowerLeftX(origBox.getLowerLeftX() + width / 2);
    box.setLowerLeftY(origBox.getLowerLeftY());
    box.setUpperRightX(origBox.getUpperRightX());
    box.setUpperRightY(origBox.getUpperRightY() / 2);
    fragments.add(box);

    return fragments;
}

From source file:PDF.RotatePDF.java

private void transformPage(PDDocument document, PDPage page, AffineTransform at)
        throws IOException, COSVisitorException {
    PDRectangle cropBox = page.findCropBox();
    float xOffset = (cropBox.getUpperRightX() + cropBox.getLowerLeftX()) / 2f;
    float yOffset = (cropBox.getUpperRightY() + cropBox.getLowerLeftY()) / 2f;
    AffineTransform transform = AffineTransform.getTranslateInstance(xOffset, yOffset);
    transform.concatenate(at);/*from  w ww  .  j  a  v a2  s .  co  m*/
    transform.concatenate(AffineTransform.getTranslateInstance(-xOffset, -yOffset));

    PDPageContentStream stream = new PDPageContentStream(document, page, true, false);
    stream.concatenate2CTM(transform);
    stream.close();

    COSBase contents = page.getCOSDictionary().getDictionaryObject(COSName.CONTENTS);
    if (contents instanceof COSStreamArray) {
        COSStreamArray contentsArray = (COSStreamArray) contents;
        COSArray newArray = new COSArray();
        newArray.add(contentsArray.get(contentsArray.getStreamCount() - 1));

        for (int i = 0; i < contentsArray.getStreamCount() - 1; i++) {
            newArray.add(contentsArray.get(i));
        }

        COSStreamArray newStreamArray = new COSStreamArray(newArray);
        page.getCOSDictionary().setItem(COSName.CONTENTS, newStreamArray);
    }
}

From source file:se.streamsource.streamflow.web.application.pdf.PdfDocument.java

License:Apache License

public PDDocument generateHeaderAndPageNumbers(PdfFont font, String... headers) {
    try {/*from  w  w w  . jav a2 s. co m*/
        int pageTotal = pdf.getNumberOfPages();
        int pageCount = 1;
        float stringWidth = 0.0f;
        float positionX = 0.0f;

        for (Object o : pdf.getDocumentCatalog().getAllPages()) {
            String numbering = "" + pageCount + " (" + pageTotal + ")";

            PDPage page = (PDPage) o;
            PDRectangle pageSize = page.findMediaBox();
            float positionY = pageSize.getHeight() - headerMargin + font.height;
            PDPageContentStream stream = new PDPageContentStream(pdf, page, true, true);
            stream.beginText();

            stream.setFont(font.font, font.size);
            stream.moveTextPositionByAmount(0, positionY);

            for (String header : headers) {
                stringWidth = font.font.getStringWidth(header);
                positionX = (pageSize.getWidth() - rightMargin - (stringWidth * font.size) / 1000f);

                stream.moveTextPositionByAmount(positionX, 0);
                stream.drawString(header);
                stream.moveTextPositionByAmount(-positionX, -font.height);
                positionY -= font.height;
            }

            stringWidth = font.font.getStringWidth(numbering);
            positionX = (pageSize.getWidth() - rightMargin - (stringWidth * font.size) / 1000f);
            stream.moveTo(pageSize.getLowerLeftX(), pageSize.getLowerLeftY());
            stream.moveTextPositionByAmount(positionX, 30 - positionY);
            stream.drawString(numbering);

            stream.endText();
            stream.close();
            pageCount++;
        }
    } catch (IOException ioe) {
        close();
    }
    return closeAndReturn();
}

From source file:so.rezervacija.StampajRezervaciju.java

@Override
protected void izvrsiKonkretnuOperaciju() throws Exception {
    PDDocument doc = null;/*from w  w  w  .j  av a 2  s . c  o  m*/
    PDPage page = null;

    try {
        doc = new PDDocument();
        page = new PDPage();

        doc.addPage(page);
        PDFont pdfFont = PDType1Font.HELVETICA_BOLD;
        float fontSize = 25;
        float leading = 1.5f * fontSize;

        PDPageContentStream contentStream = new PDPageContentStream(doc, page);

        PDRectangle mediabox = page.findMediaBox();
        float margin = 72;
        float width = mediabox.getWidth() - 2 * margin;
        float startX = mediabox.getLowerLeftX() + margin;
        float startY = mediabox.getUpperRightY() - margin;

        String text = "Izvrsili ste rezervaciju za tretman " + r.getTretman() + ", vreme rezervacije:"
                + new SimpleDateFormat("YYYY-MM-dd HH:mm").format(r.getVreme())
                + " zaposleni koji ce vrsiti tretman:" + r.getZaposleni().getImePrezime();
        List<String> lines = new ArrayList<String>();
        int lastSpace = -1;
        while (text.length() > 0) {
            int spaceIndex = text.indexOf(' ', lastSpace + 1);
            if (spaceIndex < 0) {
                lines.add(text);
                text = "";
            } else {
                String subString = text.substring(0, spaceIndex);
                float size = fontSize * pdfFont.getStringWidth(subString) / 1000;
                if (size > width) {
                    if (lastSpace < 0) // So we have a word longer than the line... draw it anyways
                    {
                        lastSpace = spaceIndex;
                    }
                    subString = text.substring(0, lastSpace);
                    lines.add(subString);
                    text = text.substring(lastSpace).trim();
                    lastSpace = -1;
                } else {
                    lastSpace = spaceIndex;
                }
            }
        }

        contentStream.beginText();
        contentStream.setFont(pdfFont, fontSize);
        contentStream.moveTextPositionByAmount(startX, startY);
        for (String line : lines) {
            contentStream.drawString(line);
            contentStream.moveTextPositionByAmount(0, -leading);
        }
        contentStream.endText();
        contentStream.close();

        doc.save("PotvrdaRezervacije.pdf");

        if (Desktop.isDesktopSupported()) {
            try {
                File myFile = new File("../ServerProjekat/PotvrdaRezervacije.pdf");
                Desktop.getDesktop().open(myFile);
            } catch (IOException ex) {
                // no application registered for PDFs
            }
        }

        doc.close();
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:uk.ac.leeds.ccg.andyt.rdl.web.RDL_ParsePDF.java

/**
 * https://svn.apache.org/viewvc/pdfbox/trunk/examples/ Based on
 * https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/PrintURLs.java?view=markup&pathrev=1703066
 *
 * @param f//  www . j  a v a2s  .  c  o  m
 * @param filter
 * @param fis
 * @return
 * @throws IOException
 * @throws TikaException
 * @throws SAXException
 */
public static ArrayList<String[]> parseForLinks(File f, String filter, FileInputStream fis)
        throws IOException, TikaException, SAXException {
    ArrayList<String[]> result;
    result = new ArrayList<String[]>();
    PDDocument doc = PDDocument.load(f);
    int pageNum = 0;
    for (PDPage page : doc.getPages()) {
        pageNum++;

        //            if (pageNum == 11) { //Degug test hack
        System.out.println("Parsing page " + pageNum);
        PDFTextStripperByArea stripper = new PDFTextStripperByArea();
        List<PDAnnotation> annotations = page.getAnnotations();
        //first setup text extraction regions
        for (int j = 0; j < annotations.size(); j++) {
            PDAnnotation annot = 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.getRotation();
                if (rotation == 0) {
                    PDRectangle pageSize = page.getMediaBox();
                    y = pageSize.getHeight() - y;
                } else if (rotation == 90) {
                    //do nothing
                }

                //Rectangle2D.Float awtRect = new Rectangle2D.Float(x, y, width, height);
                // Rounding here could be a problem!
                Rectangle2D.Double awtRect = new Rectangle2D.Double(x, y, width, height);
                stripper.addRegion("" + j, awtRect);
            }
        }

        stripper.extractRegions(page);

        for (int j = 0; j < annotations.size(); j++) {
            PDAnnotation annot = annotations.get(j);
            if (annot instanceof PDAnnotationLink) {
                PDAnnotationLink link = (PDAnnotationLink) annot;
                PDAction action = link.getAction();
                if (action == null) {
                    System.out.println(link.getContents());
                    System.out.println(annot.getClass().getName());
                    System.out.println(annot.getAnnotationName());
                    //System.out.println(annot.getNormalAppearanceStream().toString());
                    System.out.println(annot.getContents());
                    System.out.println(annot.getSubtype());
                } else {
                    String urlText = stripper.getTextForRegion("" + j);
                    if (action instanceof PDActionURI) {
                        PDActionURI uri = (PDActionURI) action;
                        String url;
                        url = uri.getURI();
                        if (url.contains(filter)) {
                            String[] partResult;
                            partResult = new String[3];
                            partResult[0] = "Page " + pageNum;
                            partResult[1] = "urlText " + urlText;
                            partResult[2] = "URL " + uri.getURI();
                            System.out.println(partResult[0]);
                            System.out.println(partResult[1]);
                            System.out.println(partResult[2]);
                            System.out.println("URL " + uri.getURI());
                            result.add(partResult);
                        } else {
                            System.out.println("URL " + uri.getURI());
                        }
                    } else {
                        System.out.println(action.getType());
                    }
                }
            } else {
                System.out.println(annot.getClass().getName());
                System.out.println(annot.getAnnotationName());
                System.out.println(annot.getContents());
                System.out.println(annot.getSubtype());
            }
        }

        //}
    }
    //       PDDocument doc = PDDocument.load(f);
    //        int pageNum = 0;
    //        for (PDPage page : doc.getPages()) {
    //            pageNum++;
    //            List<PDAnnotation> annotations = page.getAnnotations();
    //
    //            for (PDAnnotation annotation : annotations) {
    //                PDAnnotation annot = annotation;
    //                if (annot instanceof PDAnnotationLink) {
    //                    PDAnnotationLink link = (PDAnnotationLink) annot;
    //                    PDAction action = link.getAction();
    //                    if (action instanceof PDActionURI) {
    //                        PDActionURI uri = (PDActionURI) action;
    //                        String oldURI = uri.getURI();
    //                        String name = annot.getAnnotationName();
    //                        String contents = annot.getContents();
    //                        PDAppearanceStream a = annot.getNormalAppearanceStream();
    //                        //String newURI = "http://pdfbox.apache.org";
    //                        System.out.println(oldURI + " " + name + " " + contents);
    //                        //uri.setURI(newURI);
    //                    }
    //                }
    //            }
    //        }

    //        result = parseWithTika(fis);
    //XMPSchema schema;
    //schema = new XMPSchema();
    //List<String> XMPBagOrSeqList;
    //XMPBagOrSeqList = getXMPBagOrSeqList(XMPSchema schema, String name) {

    //        PDDocument tPDDocument;
    //        tPDDocument = PDDocument.load(f);
    //        COSDocument tCOSDocument;
    //        tCOSDocument = tPDDocument.getDocument();

    //        String header;
    //        header = tCOSDocument.getHeaderString();
    //        System.out.println(header);

    //        PDDocumentCatalog tPDDocumentCatalog;
    //        tPDDocumentCatalog = tPDDocument.getDocumentCatalog();
    //        PDDocumentNameDictionary tPDDocumentNameDictionary;
    //        tPDDocumentNameDictionary = tPDDocumentCatalog.getNames();

    //        COSDictionary tCOSDictionary;
    //        tCOSDictionary = tPDDocumentNameDictionary.getCOSDictionary();
    //tCOSDictionary.
    //        PDPageNode tPDPageNode;
    //        tPDPageNode = tPDDocumentCatalog.getPages();

    //        List<COSObject> tCOSObjects;
    //        tCOSObjects = tCOSDocument.getObjects();
    //        int n;
    //        n = tCOSObjects.size();
    //        System.out.println(n);
    //        COSObject aCOSObject;
    //        String s;
    //        for (int i = 0; i < n; i++) {
    //            aCOSObject = tCOSObjects.get(i);
    //            s = aCOSObject.toString();
    //            System.out.println(s);
    //        }

    //        XMPMetadata tXMPMetadata;
    //        tXMPMetadata = getXMPMetadata(tPDDocument);

    //        Document XMPDocument;
    //        XMPDocument = tXMPMetadata.getXMPDocument();
    //        Node n;
    //        n = XMPDocument.getFirstChild();
    //        parseNode(n);
    return result;
}

From source file:uk.ac.leeds.ccg.andyt.rdl.web.RDL_ParsePDF.java

/**
 * Converts PDF to a String a page at a time.
 *
 * @param f/*ww  w.  j  a  va 2s  . c  o m*/
 * @return
 * @throws IOException
 */
public static String parseToString(File f) throws IOException {
    String result;
    result = "";
    PDDocument doc = PDDocument.load(f);
    PDFTextStripperByArea stripper = new PDFTextStripperByArea();
    stripper.setSortByPosition(true);
    //Rectangle rect = new Rectangle(10, 280, 275, 60);
    //PDPage firstPage = doc.getPage(0);
    for (PDPage page : doc.getPages()) {
        PDRectangle aPDRectangle;
        aPDRectangle = page.getBBox();
        Rectangle2D.Double rect = new Rectangle2D.Double(aPDRectangle.getLowerLeftX(),
                aPDRectangle.getLowerLeftY(),
                //aPDRectangle.getUpperRightY(),
                aPDRectangle.getWidth(), aPDRectangle.getHeight());
        stripper.addRegion("class1", rect);
        stripper.extractRegions(page);
        System.out.println("<Text in the area:" + rect + ">");
        String text;
        text = stripper.getTextForRegion("class1");
        System.out.println(text);
        System.out.println("</Text in the area:" + rect + ">");
        result += text;
    }
    return result;
}

From source file:vortext.TextHighlight.java

License:Apache License

/**
 * Computes a float array of size eight with all the vertices of the
 * PDRectangle/*from  w  w  w.  j  a v  a2 s. com*/
 */
public float[] getQuads(final PDRectangle rect) {
    final float[] quads = new float[8];
    // top left
    quads[0] = rect.getLowerLeftX(); // x1
    quads[1] = rect.getUpperRightY(); // y1
    // bottom left
    quads[2] = rect.getUpperRightX(); // x2
    quads[3] = quads[1]; // y2
    // top right
    quads[4] = quads[0]; // x3
    quads[5] = rect.getLowerLeftY(); // y3
    // bottom right
    quads[6] = quads[2]; // x4
    quads[7] = quads[5]; // y5
    return quads;
}