List of usage examples for org.apache.pdfbox.pdmodel.interactive.annotation PDAnnotationTextMarkup PDAnnotationTextMarkup
protected PDAnnotationTextMarkup(COSDictionary field)
From source file:com.infoimage.infotrac.pdfbox.PDFTextAnnotator.java
License:Apache License
/** * Highlights a pattern within the PDF with the default color * Returns the list of added annotations for further modification * Note: it will process every page, but cannot process patterns that span multiple pages * Note: it will not work for top-bottom text (such as Chinese) * * @param pdf//from w ww . j a va 2 s.c o m * PDDocument * @param pattern * Pattern (regex) * @throws Exception */ public List<PDAnnotationTextMarkup> highlight(PDDocument pdf, Pattern pattern) throws Exception { if (textCache == null) { throw new Exception("TextCache was not initilized, please run initialize on the document first"); } List<PDPage> pages = pdf.getDocumentCatalog().getAllPages(); ArrayList<PDAnnotationTextMarkup> highligts = new ArrayList<PDAnnotationTextMarkup>(); for (int pageIndex = getStartPage() - 1; pageIndex < getEndPage() && pageIndex < pages.size(); pageIndex++) { PDPage page = pages.get(pageIndex); List<PDAnnotation> annotations = page.getAnnotations(); List<Match> matches = this.textCache.getTextPositions(pageIndex + 1, pattern); for (Match match : matches) { List<PDRectangle> textBoundingBoxes = getTextBoundingBoxes(match.positions); PDAnnotationTextMarkup markup = new PDAnnotationTextMarkup( PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT); if (textBoundingBoxes.size() > 0) { markup.setRectangle(textBoundingBoxes.get(0)); float[] quads = new float[8 * textBoundingBoxes.size()]; int cursor = 0; for (PDRectangle rect : textBoundingBoxes) { float[] tmp = computeQuads(rect); for (int i = 0; i < tmp.length; i++) { quads[cursor + i] = tmp[i]; } cursor = cursor + 8; } markup.setQuadPoints(quads); markup.setConstantOpacity((float) 0.8); markup.setColour(getDefaultColor()); markup.setPrinted(true); markup.setContents(match.str); annotations.add(markup); highligts.add(markup); } } } return highligts; }
From source file:de.berber.kindle.annotator.lib.Marking.java
License:Apache License
@Override protected PDAnnotation toPDAnnotation(final PDDocumentOutline documentOutline, final PDPage page) { LOG.info("Creating marking " + leftXPositionFactor + "/" + lowerYPositionFactor + " -> " + rightXPositionFactor + "/" + upperYPositionFactor); // create highlighted area final PDGamma pdColor = getColor(); // final PDFont font = PDType1Font.HELVETICA_BOLD; // float textHeight = font.getFontHeight("Hg".getBytes(), 0, 2); final PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup( PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT); txtMark.setColour(pdColor);/* w w w . j av a2 s. c o m*/ txtMark.setConstantOpacity(opacity); if (comment != null) { // set comment if available txtMark.setContents(comment.getText()); } // Set the rectangle containing the markup final PDRectangle cropBox = page.getTrimBox(); final PDRectangle position = new PDRectangle(); position.setLowerLeftX((float) (cropBox.getLowerLeftX() + leftXPositionFactor * (cropBox.getUpperRightX() - cropBox.getLowerLeftX()))); position.setUpperRightX((float) (cropBox.getLowerLeftX() + rightXPositionFactor * (cropBox.getUpperRightX() - cropBox.getLowerLeftX()))); position.setLowerLeftY((float) (cropBox.getUpperRightY() - (lowerYPositionFactor + ((upperYPositionFactor - lowerYPositionFactor == 0.0) ? 0.025 : 0.00)) * (cropBox.getUpperRightY() - cropBox.getLowerLeftY()))); position.setUpperRightY((float) (cropBox.getUpperRightY() - (upperYPositionFactor) * (cropBox.getUpperRightY() - cropBox.getLowerLeftY()))); txtMark.setRectangle(position); // work out the points forming the four corners of the annotations // set out in anti clockwise form (Completely wraps the text) // OK, the below doesn't match that description. // It's what acrobat 7 does and displays properly! float[] quads = new float[8]; quads[0] = position.getLowerLeftX(); // x1 quads[1] = position.getUpperRightY(); // y1 quads[2] = position.getUpperRightX(); // x2 quads[3] = position.getUpperRightY(); // y2 quads[4] = position.getLowerLeftX(); // x3 quads[5] = position.getLowerLeftY(); // y3 quads[6] = position.getUpperRightX(); // x4 quads[7] = position.getLowerLeftY(); // y5 txtMark.setQuadPoints(quads); return txtMark; }
From source file:helper.pdfpreprocessing.pdf.TextHighlight.java
License:Apache License
private boolean markupMatch(Color color, PDPageContentStream contentStream, Match markingMatch, int height, boolean withId, PDPage page, String comment, boolean commentOnly) throws IOException { final List<PDRectangle> textBoundingBoxes = getTextBoundingBoxes(markingMatch.positions); if (textBoundingBoxes.size() > 0) { contentStream.setNonStrokingColor(color); for (PDRectangle textBoundingBox : textBoundingBoxes) { if (comment.isEmpty()) { contentStream.addRect(textBoundingBox.getLowerLeftX(), textBoundingBox.getLowerLeftY(), Math .max(Math.abs(textBoundingBox.getUpperRightX() - textBoundingBox.getLowerLeftX()), 10), height);//from w w w.ja v a 2s. co m contentStream.fill(); } if (withId) { PDFont font = PDType1Font.HELVETICA; contentStream.beginText(); contentStream.setFont(font, 5); contentStream.newLineAtOffset(textBoundingBox.getUpperRightX(), textBoundingBox.getUpperRightY()); contentStream.showText(markingMatch.str); contentStream.endText(); } if (!comment.isEmpty() && !commentOnly) { PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup( PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT); PDRectangle position = new PDRectangle(); position.setLowerLeftX(textBoundingBox.getLowerLeftX()); position.setLowerLeftY(textBoundingBox.getLowerLeftY()); position.setUpperRightX(textBoundingBox.getLowerLeftX() + Math .max(Math.abs(textBoundingBox.getUpperRightX() - textBoundingBox.getLowerLeftX()), 10)); position.setUpperRightY(textBoundingBox.getLowerLeftY() + 10); txtMark.setRectangle(position); float[] quads = new float[8]; quads[0] = position.getLowerLeftX(); // x1 quads[1] = position.getUpperRightY() - 2; // y1 quads[2] = position.getUpperRightX(); // x2 quads[3] = quads[1]; // y2 quads[4] = quads[0]; // x3 quads[5] = position.getLowerLeftY() - 2; // y3 quads[6] = quads[2]; // x4 quads[7] = quads[5]; // y5 txtMark.setQuadPoints(quads); txtMark.setConstantOpacity((float) 0.5); txtMark.setContents("Missing Assumption/s (" + markingMatch.str + "):\n" + comment); float[] colorArray = new float[] { 0, 0, 0 }; colorArray = color.getColorComponents(colorArray); PDColor hColor = new PDColor(colorArray, PDDeviceRGB.INSTANCE); txtMark.setColor(hColor); txtMark.setCreationDate(Calendar.getInstance()); txtMark.setTitlePopup("Assumption Error"); page.getAnnotations().add(txtMark); } else if (!comment.isEmpty() && commentOnly) { for (int i = 0; i < page.getAnnotations().size(); i++) { String extractedComment = page.getAnnotations().get(i).getContents(); if (extractedComment != null) { String commentID = extractedComment.substring(extractedComment.indexOf("(") + 1, extractedComment.indexOf(")")); if (markingMatch.str.equals(commentID) && extractedComment.contains(comment)) { page.getAnnotations().get(i).setContents(extractedComment + "\n" + comment); } } } } } return true; } return false; }
From source file:hightlighting.PDFTextAnnotator.java
License:Apache License
/** * Highlights a pattern within the PDF with the default color * Returns the list of added annotations for further modification * Note: it will process every page, but cannot process patterns that span multiple pages * Note: it will not work for top-bottom text (such as Chinese) * /* w w w. j a va 2s . com*/ * @param pdf * PDDocument * @param pattern * Pattern (regex) * @throws Exception */ public List<PDAnnotationTextMarkup> highlight(PDDocument pdf, Pattern pattern) throws Exception { if (textCache == null) { throw new Exception("TextCache was not initilized, please run initialize on the document first"); } List<PDPage> pages = pdf.getDocumentCatalog().getAllPages(); ArrayList<PDAnnotationTextMarkup> highligts = new ArrayList<PDAnnotationTextMarkup>(); for (int pageIndex = getStartPage() - 1; pageIndex < getEndPage() && pageIndex < pages.size(); pageIndex++) { PDPage page = pages.get(pageIndex); List<PDAnnotation> annotations = page.getAnnotations(); List<Match> matches = this.textCache.getTextPositions(pageIndex + 1, pattern); for (Match match : matches) { List<PDRectangle> textBoundingBoxes = getTextBoundingBoxes(match.positions); if (textBoundingBoxes.size() > 0) { float[] quads = new float[8]; int cursor = 0; for (PDRectangle rect : textBoundingBoxes) { PDAnnotationTextMarkup markup = new PDAnnotationTextMarkup( PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT); markup.setRectangle(rect); float[] tmp = computeQuads(rect); for (int i = 0; i < tmp.length; i++) { quads[cursor + i] = tmp[i]; } //cursor = cursor + 8; markup.setQuadPoints(quads); markup.setConstantOpacity((float) 0.8); markup.setColour(getDefaultColor()); markup.setPrinted(true); markup.setContents(match.str); annotations.add(markup); highligts.add(markup); } } } } return highligts; }
From source file:net.bookinaction.utils.AnnotationMaker.java
License:Apache License
public PDAnnotationTextMarkup textMarkupAnnotation(PDColor color, Rectangle2D.Float position, String comment) { // Now add the markup annotation, a highlight to PDFBox text PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT); txtMark.setColor(color);// w w w . java 2 s . c o m txtMark.setConstantOpacity((float) 0.2); // 20% transparent // Set the rectangle containing the markup txtMark.setRectangle(new PDRectangle(position.x, position.y, position.width, position.height)); // work out the points forming the four corners of the annotations // set out in anti clockwise form (Completely wraps the text) // OK, the below doesn't match that description. // It's what acrobat 7 does and displays properly! float[] quads = new float[8]; quads[0] = position.x; // x1 quads[1] = position.y + position.height; // y1 quads[2] = position.x + position.width; // x2 quads[3] = quads[1]; // y2 quads[4] = quads[0]; // x3 quads[5] = position.y; // y3 quads[6] = quads[2]; // x4 quads[7] = quads[5]; // y5 txtMark.setQuadPoints(quads); txtMark.setContents(comment); return txtMark; }
From source file:net.padaf.preflight.annotation.FreeTextAnnotationValidator.java
License:Apache License
public FreeTextAnnotationValidator(DocumentHandler handler, COSDictionary annotDictionary) { super(handler, annotDictionary); this.pdFreeText = new PDAnnotationTextMarkup(annotDictionary); this.pdAnnot = this.pdFreeText; }
From source file:net.padaf.preflight.annotation.MarkupAnnotationValidator.java
License:Apache License
public MarkupAnnotationValidator(DocumentHandler handler, COSDictionary annotDictionary) { super(handler, annotDictionary); this.pdMarkup = new PDAnnotationTextMarkup(annotDictionary); this.pdAnnot = this.pdMarkup; }
From source file:org.data2semantics.annotate.D2S_SampleAnnotation.java
License:Apache License
/** * This will create a doucument showing various annotations. * /*from w w w. j ava 2s . c om*/ * @param args * The command line arguments. * * @throws Exception * If there is an error parsing the document. */ public static void main(String[] args) throws Exception { PDDocument document = new PDDocument(); try { PDPage page = new PDPage(); document.addPage(page); List annotations = page.getAnnotations(); // Setup some basic reusable objects/constants // Annotations themselves can only be used once! float inch = 72; PDGamma colourRed = new PDGamma(); colourRed.setR(1); PDGamma colourBlue = new PDGamma(); colourBlue.setB(1); PDGamma colourBlack = new PDGamma(); PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary(); borderThick.setWidth(inch / 12); // 12th inch PDBorderStyleDictionary borderThin = new PDBorderStyleDictionary(); borderThin.setWidth(inch / 72); // 1 point PDBorderStyleDictionary borderULine = new PDBorderStyleDictionary(); borderULine.setStyle(PDBorderStyleDictionary.STYLE_UNDERLINE); borderULine.setWidth(inch / 72); // 1 point float pw = page.getMediaBox().getUpperRightX(); float ph = page.getMediaBox().getUpperRightY(); // First add some text, two lines we'll add some annotations to this // later PDFont font = PDType1Font.HELVETICA_BOLD; PDPageContentStream contentStream = new PDPageContentStream(document, page); contentStream.beginText(); contentStream.setFont(font, 18); contentStream.moveTextPositionByAmount(inch, ph - inch - 18); contentStream.drawString("PDFBox"); contentStream.moveTextPositionByAmount(0, -(inch / 2)); contentStream.drawString("Click Here"); contentStream.endText(); contentStream.close(); // Now add the markup annotation, a highlight to PDFBox text PDAnnotationTextMarkup txtMark = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT); txtMark.setColour(colourBlue); txtMark.setConstantOpacity((float) 0.2); // Make the highlight 20% // transparent // Set the rectangle containing the markup float textWidth = (font.getStringWidth("PDFBox") / 1000) * 18; PDRectangle position = new PDRectangle(); position.setLowerLeftX(inch); position.setLowerLeftY(ph - inch - 18); position.setUpperRightX(72 + textWidth); position.setUpperRightY(ph - inch); txtMark.setRectangle(position); // work out the points forming the four corners of the annotations // set out in anti clockwise form (Completely wraps the text) // OK, the below doesn't match that description. // It's what acrobat 7 does and displays properly! float[] quads = new float[8]; quads[0] = position.getLowerLeftX(); // x1 quads[1] = position.getUpperRightY() - 2; // y1 quads[2] = position.getUpperRightX(); // x2 quads[3] = quads[1]; // y2 quads[4] = quads[0]; // x3 quads[5] = position.getLowerLeftY() - 2; // y3 quads[6] = quads[2]; // x4 quads[7] = quads[5]; // y5 txtMark.setQuadPoints(quads); txtMark.setContents("Highlighted since it's important"); annotations.add(txtMark); // Now add the link annotation, so the clickme works PDAnnotationLink txtLink = new PDAnnotationLink(); txtLink.setBorderStyle(borderULine); // Set the rectangle containing the link textWidth = (font.getStringWidth("Click Here") / 1000) * 18; position = new PDRectangle(); position.setLowerLeftX(inch); position.setLowerLeftY(ph - (float) (1.5 * inch) - 20); // down a // couple of // points position.setUpperRightX(72 + textWidth); position.setUpperRightY(ph - (float) (1.5 * inch)); txtLink.setRectangle(position); // add an action PDActionURI action = new PDActionURI(); action.setURI("http://www.pdfbox.org"); txtLink.setAction(action); annotations.add(txtLink); // Now draw a few more annotations PDAnnotationSquareCircle aCircle = new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_CIRCLE); aCircle.setContents("Circle Annotation"); aCircle.setInteriorColour(colourRed); // Fill in circle in red aCircle.setColour(colourBlue); // The border itself will be blue aCircle.setBorderStyle(borderThin); // Place the annotation on the page, we'll make this 1" round // 3" down, 1" in on the page position = new PDRectangle(); position.setLowerLeftX(inch); position.setLowerLeftY(ph - (3 * inch) - inch); // 1" height, 3" // down position.setUpperRightX(2 * inch); // 1" in, 1" width position.setUpperRightY(ph - (3 * inch)); // 3" down aCircle.setRectangle(position); // add to the annotations on the page annotations.add(aCircle); // Now a square annotation PDAnnotationSquareCircle aSquare = new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_SQUARE); aSquare.setContents("Square Annotation"); aSquare.setColour(colourRed); // Outline in red, not setting a fill aSquare.setBorderStyle(borderThick); // Place the annotation on the page, we'll make this 1" (72points) // square // 3.5" down, 1" in from the right on the page position = new PDRectangle(); // Reuse the variable, but note it's a // new object! position.setLowerLeftX(pw - (2 * inch)); // 1" in from right, 1" // wide position.setLowerLeftY(ph - (float) (3.5 * inch) - inch); // 1" height, 3.5" // down position.setUpperRightX(pw - inch); // 1" in from right position.setUpperRightY(ph - (float) (3.5 * inch)); // 3.5" down aSquare.setRectangle(position); // add to the annotations on the page annotations.add(aSquare); // Now we want to draw a line between the two, one end with an open // arrow PDAnnotationLine aLine = new PDAnnotationLine(); aLine.setEndPointEndingStyle(PDAnnotationLine.LE_OPEN_ARROW); aLine.setContents("Circle->Square"); aLine.setCaption(true); // Make the contents a caption on the line // Set the rectangle containing the line position = new PDRectangle(); // Reuse the variable, but note it's a // new object! position.setLowerLeftX(2 * inch); // 1" in + width of circle position.setLowerLeftY(ph - (float) (3.5 * inch) - inch); // 1" height, 3.5" // down position.setUpperRightX(pw - inch - inch); // 1" in from right, and // width of square position.setUpperRightY(ph - (3 * inch)); // 3" down (top of circle) aLine.setRectangle(position); // Now set the line position itself float[] linepos = new float[4]; linepos[0] = 2 * inch; // x1 = rhs of circle linepos[1] = ph - (float) (3.5 * inch); // y1 halfway down circle linepos[2] = pw - (2 * inch); // x2 = lhs of square linepos[3] = ph - (4 * inch); // y2 halfway down square aLine.setLine(linepos); aLine.setBorderStyle(borderThick); aLine.setColour(colourBlack); // add to the annotations on the page annotations.add(aLine); // Finally all done document.save("testAnnotation.pdf"); } finally { document.close(); } }
From source file:vortext.TextHighlight.java
License:Apache License
@SuppressWarnings("unchecked") public List<PDAnnotationTextMarkup> highlight(final Pattern pattern, final String subType) throws IOException { if (textAggregate == null || document == null) { throw new IllegalArgumentException("TextAggregate was not initilized"); }/*from www . ja va 2 s . com*/ final List<PDPage> pages = document.getDocumentCatalog().getAllPages(); final ArrayList<PDAnnotationTextMarkup> newAnnotations = new ArrayList<PDAnnotationTextMarkup>(); for (int pageIndex = getStartPage() - 1; pageIndex < getEndPage() && pageIndex < pages.size(); pageIndex++) { final PDPage page = pages.get(pageIndex); final List<PDAnnotation> annotations = page.getAnnotations(); final List<Match> matches = textAggregate.match(pageIndex + 1, pattern); for (final Match match : matches) { final List<PDRectangle> textBoundingBoxes = getTextBoundingBoxes(match.positions); if (textBoundingBoxes.size() > 0) { final PDAnnotationTextMarkup annotation = new PDAnnotationTextMarkup(subType); annotation.setRectangle(textBoundingBoxes.get(0)); final float[] quads = this.getQuads(textBoundingBoxes); annotation.setQuadPoints(quads); annotation.setContents(match.str); annotations.add(annotation); newAnnotations.add(annotation); } } } return newAnnotations; }