List of usage examples for org.apache.pdfbox.pdmodel.interactive.annotation PDAnnotation getContents
public String getContents()
From source file:airviewer.AnnotationGenerator.java
/** * * @param a/*from ww w . j ava 2 s . c o m*/ * @throws IOException */ public static void resizeAnnotationToContent(PDAnnotation a) throws IOException { assert null != a; final String contents = a.getContents(); final boolean hasContents = null != contents && 0 < contents.length(); if (hasContents) { float borderWidth = 0; if (null != a.getColor() && a instanceof PDAnnotationMarkup) { final PDBorderStyleDictionary borderStyle = ((PDAnnotationMarkup) a).getBorderStyle(); if (null != borderStyle) { borderWidth = Math.abs(borderStyle.getWidth()); } } final float margin = MARGIN_SIZE_PDF_POINTS; final float fontSize = FONT_SIZE_PDF_POINTS; final float textHeight = fontSize + LINE_SPACE_SIZE_PDF_POINTS; PDRectangle position = a.getRectangle(); final float lowerLeftX = position.getLowerLeftX(); final float lowerLeftY = position.getLowerLeftY(); float width = FONT.getStringWidth(contents) * fontSize / SIZE_UNITS_PER_PDF_POINT; float height = textHeight; // Reserve enough width for border and margin at start and end width += (borderWidth + MARGIN_SIZE_PDF_POINTS) * 2.0f; height += (borderWidth + MARGIN_SIZE_PDF_POINTS) * 2.0f; // Replace the annotations existing rectangle a.setRectangle(new PDRectangle(lowerLeftX, lowerLeftY, width, height)); } }
From source file:airviewer.AnnotationGenerator.java
/** * * @param a//from w w w.j ava 2 s . c o m * @param d * @param p * @param shouldResize * @return */ public static PDAppearanceStream generateSquareAppearance(PDAnnotation a, PDDocument d, PDPage p, boolean shouldResize) { assert null != a; assert null != d; assert null != p; PDAppearanceStream annotationAppearanceStream = null; try { if (shouldResize) { resizeAnnotationToContent(a); } final String contents = a.getContents(); final boolean hasContents = null != contents && 0 < contents.length(); float borderWidth = 0; if (a instanceof PDAnnotationMarkup) { final PDBorderStyleDictionary borderStyle = ((PDAnnotationMarkup) a).getBorderStyle(); if (null != a.getColor() && null != borderStyle) { borderWidth = Math.abs(borderStyle.getWidth()); } } final float fontSize = FONT_SIZE_PDF_POINTS; final float textHeight = fontSize; final float margin = MARGIN_SIZE_PDF_POINTS; PDRectangle position = a.getRectangle(); final float lowerLeftX = position.getLowerLeftX(); final float lowerLeftY = position.getLowerLeftY(); float width = position.getWidth(); float height = position.getHeight(); annotationAppearanceStream = new PDAppearanceStream(d); annotationAppearanceStream.setBBox(position); annotationAppearanceStream.setMatrix(new AffineTransform()); annotationAppearanceStream.setResources(p.getResources()); try (PDPageContentStream appearanceContent = new PDPageContentStream(d, annotationAppearanceStream)) { appearanceContent.transform(new Matrix()); // Identity transform // Rect is inset by half border width to prevent border leaking // outside bounding box final float insetLowerLeftX = lowerLeftX + borderWidth * 0.5f; final float insetLowerLeftY = lowerLeftY + borderWidth * 0.5f; final float insetWidth = width - borderWidth; final float insetheight = height - borderWidth; appearanceContent.addRect(insetLowerLeftX, insetLowerLeftY, insetWidth, insetheight); appearanceContent.setLineWidth(borderWidth); appearanceContent.setNonStrokingColor(GRAY); if (null != a.getColor() && 0 < borderWidth) { appearanceContent.setStrokingColor(a.getColor()); appearanceContent.fillAndStroke(); } else { appearanceContent.fill(); } if (hasContents) { appearanceContent.moveTo(0, 0); appearanceContent.beginText(); // Center vertically, left justified inside border with margin appearanceContent.newLineAtOffset(lowerLeftX + borderWidth + margin, lowerLeftY + (height + LINE_SPACE_SIZE_PDF_POINTS) * 0.5f - textHeight * 0.5f); appearanceContent.setFont(FONT, fontSize); if (null != a.getColor()) { appearanceContent.setNonStrokingColor(a.getColor()); // Sets color of text } else { appearanceContent.setNonStrokingColor(BLACK); // Sets color of text } appearanceContent.showText(a.getContents()); appearanceContent.endText(); } } } catch (IOException ex) { Logger.getLogger(AnnotationGenerator.class.getName()).log(Level.SEVERE, null, ex); } return annotationAppearanceStream; }
From source file:airviewer.AnnotationGenerator.java
/** * * @param a/* w w w. ja va2s .co m*/ * @param d * @param p * @param shouldResize * @return */ public static PDAppearanceStream generateCircleAppearance(PDAnnotation a, PDDocument d, PDPage p, boolean shouldResize) { assert null != a; assert null != d; assert null != p; PDAppearanceStream annotationAppearanceStream = null; try { if (shouldResize) { resizeAnnotationToContent(a); } final String contents = a.getContents(); final boolean hasContents = null != contents && 0 < contents.length(); float borderWidth = 0; if (a instanceof PDAnnotationMarkup) { final PDBorderStyleDictionary borderStyle = ((PDAnnotationMarkup) a).getBorderStyle(); if (null != a.getColor() && null != borderStyle) { borderWidth = Math.abs(borderStyle.getWidth()); } } final float fontSize = FONT_SIZE_PDF_POINTS; final float textHeight = fontSize; final float margin = MARGIN_SIZE_PDF_POINTS; PDRectangle position = a.getRectangle(); final float lowerLeftX = position.getLowerLeftX(); final float lowerLeftY = position.getLowerLeftY(); float width = position.getWidth(); float height = position.getHeight(); annotationAppearanceStream = new PDAppearanceStream(d); annotationAppearanceStream.setBBox(position); annotationAppearanceStream.setMatrix(new AffineTransform()); annotationAppearanceStream.setResources(p.getResources()); try (PDPageContentStream appearanceContent = new PDPageContentStream(d, annotationAppearanceStream)) { appearanceContent.transform(new Matrix()); // Identity transform // Rect is inset by half border width to prevent border leaking // outside bounding box final float insetLowerLeftX = lowerLeftX + borderWidth * 0.5f; final float insetLowerLeftY = lowerLeftY + borderWidth * 0.5f; final float insetWidth = width - borderWidth; final float insetheight = height - borderWidth; if (null != a.getColor()) { appearanceContent.setLineWidth(borderWidth); appearanceContent.moveTo(insetLowerLeftX, insetLowerLeftY + insetheight * 0.5f); appearanceContent.curveTo(insetLowerLeftX, insetLowerLeftY + insetheight * 0.75f, insetLowerLeftX + insetWidth * 0.25f, insetLowerLeftY + insetheight, insetLowerLeftX + insetWidth * 0.5f, insetLowerLeftY + insetheight); appearanceContent.curveTo(insetLowerLeftX + insetWidth * 0.75f, insetLowerLeftY + insetheight, insetLowerLeftX + insetWidth, insetLowerLeftY + insetheight * 0.75f, insetLowerLeftX + insetWidth, insetLowerLeftY + insetheight * 0.5f); appearanceContent.curveTo(insetLowerLeftX + insetWidth, insetLowerLeftY + insetheight * 0.25f, insetLowerLeftX + insetWidth * 0.75f, insetLowerLeftY, insetLowerLeftX + insetWidth * 0.5f, insetLowerLeftY); appearanceContent.curveTo(insetLowerLeftX + insetWidth * 0.25f, insetLowerLeftY, insetLowerLeftX, insetLowerLeftY + insetheight * 0.25f, insetLowerLeftX, insetLowerLeftY + insetheight * 0.5f); } appearanceContent.setNonStrokingColor(GRAY); if (null != a.getColor() && 0 < borderWidth) { appearanceContent.setStrokingColor(a.getColor()); appearanceContent.fillAndStroke(); } else { appearanceContent.fill(); } if (hasContents) { appearanceContent.moveTo(0, 0); appearanceContent.beginText(); // Center text vertically, left justified inside border with margin appearanceContent.newLineAtOffset(lowerLeftX + borderWidth + margin, lowerLeftY + (height + LINE_SPACE_SIZE_PDF_POINTS) * 0.5f - textHeight * 0.5f); appearanceContent.setFont(FONT, fontSize); appearanceContent.setNonStrokingColor(BLACK); // Sets color of text appearanceContent.showText(a.getContents()); appearanceContent.endText(); } } } catch (IOException ex) { Logger.getLogger(AnnotationGenerator.class.getName()).log(Level.SEVERE, null, ex); } return annotationAppearanceStream; }
From source file:net.bookinaction.ExtractAnnotations.java
License:Apache License
public void doJob(String job, Float[] pA) throws IOException { PDDocument document = null;/* w ww . j a v a 2 s . co m*/ Stamper s = new Stamper(); // utility class final String job_file = job + ".pdf"; final String dic_file = job + "-dict.txt"; final String new_job = job + "-new.pdf"; PrintWriter writer = new PrintWriter(dic_file); ImageLocationListener imageLocationsListener = new ImageLocationListener(); AnnotationMaker annotMaker = new AnnotationMaker(); try { document = PDDocument.load(new File(job_file)); int pageNum = 0; for (PDPage page : document.getPages()) { pageNum++; PDRectangle cropBox = page.getCropBox(); List<PDAnnotation> annotations = page.getAnnotations(); // extract image locations List<Rectangle2D> imageRects = new ArrayList<Rectangle2D>(); imageLocationsListener.setImageRects(imageRects); imageLocationsListener.processPage(page); int im = 0; for (Rectangle2D pdImageRect : imageRects) { s.recordImage(writer, pageNum, "[im" + im + "]", (Rectangle2D.Float) pdImageRect); annotations.add(annotMaker.squareAnnotation(Color.YELLOW, (Rectangle2D.Float) pdImageRect, "[im" + im + "]")); im++; } PDFTextStripperByArea stripper = new PDFTextStripperByArea(); int j = 0; List<PDAnnotation> viableAnnots = new ArrayList(); for (PDAnnotation annot : annotations) { if (annot instanceof PDAnnotationTextMarkup || annot instanceof PDAnnotationLink) { stripper.addRegion(Integer.toString(j++), s.getAwtRect( s.adjustedRect(annot.getRectangle(), pA[0], pA[1], pA[2], pA[3]), cropBox)); viableAnnots.add(annot); } else if (annot instanceof PDAnnotationPopup || annot instanceof PDAnnotationText) { viableAnnots.add(annot); } } stripper.extractRegions(page); List<PDRectangle> rects = new ArrayList<PDRectangle>(); List<String> comments = new ArrayList<String>(); List<String> highlightTexts = new ArrayList<String>(); j = 0; for (PDAnnotation viableAnnot : viableAnnots) { if (viableAnnot instanceof PDAnnotationTextMarkup) { String highlightText = stripper.getTextForRegion(Integer.toString(j++)); String withoutCR = highlightText.replace((char) 0x0A, '^'); String comment = viableAnnot.getContents(); String colorString = String.format("%06x", viableAnnot.getColor().toRGB()); PDRectangle aRect = s.adjustedRect(viableAnnot.getRectangle(), pA[4], pA[5], pA[6], pA[7]); rects.add(aRect); comments.add(comment); highlightTexts.add(highlightText); s.recordTextMarkup(writer, pageNum, comment, withoutCR, aRect, colorString); } else if (viableAnnot instanceof PDAnnotationText) { String comment = viableAnnot.getContents(); String colorString = String.format("%06x", viableAnnot.getColor().toRGB()); for (Rectangle2D pdImageRect : imageRects) { if (pdImageRect.contains(viableAnnot.getRectangle().getLowerLeftX(), viableAnnot.getRectangle().getLowerLeftY())) { s.recordTextMarkup(writer, pageNum, comment, "", (Rectangle2D.Float) pdImageRect, colorString); annotations.add(annotMaker.squareAnnotation(Color.GREEN, (Rectangle2D.Float) pdImageRect, comment)); } ; } } } PDPageContentStream canvas = new PDPageContentStream(document, page, true, true, true); int i = 0; for (PDRectangle pdRect : rects) { String comment = comments.get(i); String highlightText = highlightTexts.get(i); //annotations.add(linkAnnotation(pdRect, comment, highlightText)); //annotations.add(annotationSquareCircle(pdRect, BLUE)); s.showBox(canvas, new Rectangle2D.Float(pdRect.getLowerLeftX(), pdRect.getUpperRightY(), pdRect.getWidth(), pdRect.getHeight()), cropBox, Color.BLUE); i++; } canvas.close(); } writer.close(); document.save(new_job); } finally { if (document != null) { document.close(); } } }
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/*from w ww.ja v a 2 s . 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; }