List of usage examples for org.apache.pdfbox.pdmodel PDPage getAnnotations
public List<PDAnnotation> getAnnotations() throws IOException
From source file:airviewer.AbstractDocumentCommandWrapper.java
License:Apache License
/** * This method finds and returns the "last" (upper most) annotation that * contains the specified x and y coordinates * * @param pageIndex Must be pageIndex >= 0 && pageIndex < getPageCount() * @param x/*from ww w .j a v a 2s. c o m*/ * An X coordinate in the PDF coordinate system * @param y A Y coordinate in the PDF coordinate system * @return The annotation (if any) that was found at {x,y} on the page with * pageIndex or null otherwise. */ protected PDAnnotation getLastAnnotationOnPageAtPoint(int pageIndex, float x, float y) { assert 0 <= pageIndex && pageIndex < getPageCount(); try { PDPage page = wrappedDocument.getPage(pageIndex); List<PDAnnotation> annotations = page.getAnnotations(); return getLastAnnotationAtPoint(annotations, x, y); } catch (IOException ex) { Logger.getLogger(AbstractDocumentCommandWrapper.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:airviewer.AIRViewerModel.java
License:Apache License
/** * Some PDF editors (and hand written files) produce unnamed annotations, * but the Model relies on annotations having unique names. Call this method * to ensure that all annotations on the specified page in wrappedDocument * have unique names regardless of the presence or absence of pre-existing * names.//from ww w . j a va 2 s .c om * * @param pageIndex pageIndex >= 0 && pageIndex < numPages() * @return A list of uniquely named annotations. */ private List<PDAnnotation> getAllSanitizedAnnotationsOnPage(int pageIndex) { assert Integer.toString(pageIndex) != null; List<PDAnnotation> result = null; try { PDPage page = wrappedDocument.getPage(pageIndex); result = page.getAnnotations(); for (PDAnnotation a : result) { // Other programs neglect to provide a name, so provide one // to uniquely identify selectde annotations. a.setAnnotationName(new UID().toString()); } } catch (IOException ex) { Logger.getLogger(AbstractDocumentCommandWrapper.class.getName()).log(Level.SEVERE, null, ex); } return result; }
From source file:airviewer.BoxAnnotationMaker.java
License:Apache License
/** * * @param document/* w ww .ja va 2 s .co m*/ * @param arguments(lowerLeftX, lowerLeftY, width, height) * @return */ public static List<PDAnnotation> make(PDDocument document, ArrayList<String> arguments) { assert null != arguments && arguments.size() == 5; assert null != document; List<PDAnnotation> result; try { int pageNumber = parseInt(arguments.get(0)); float lowerLeftX = parseFloat(arguments.get(1)); float lowerLeftY = parseFloat(arguments.get(2)); float width = parseFloat(arguments.get(3)); float height = parseFloat(arguments.get(4)); String contents = ""; PDFont font = PDType1Font.HELVETICA_OBLIQUE; float fontSize = 16; // Or whatever font size you want. //float textWidth = font.getStringWidth(contents) * fontSize / 1000.0f; //float textHeight = 32; try { PDPage page = document.getPage(pageNumber); PDColor red = new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE); PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary(); borderThick.setWidth(72 / 12); // 12th inch PDRectangle position = new PDRectangle(); position.setLowerLeftX(lowerLeftX); position.setLowerLeftY(lowerLeftY); position.setUpperRightX(lowerLeftX + width); position.setUpperRightY(lowerLeftY + height); PDAnnotationSquareCircle aSquare = new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_SQUARE); aSquare.setAnnotationName(new UID().toString()); aSquare.setContents(contents); aSquare.setColor(red); // Outline in red, not setting a fill PDColor fillColor = new PDColor(new float[] { .8f, .8f, .8f }, PDDeviceRGB.INSTANCE); aSquare.setInteriorColor(fillColor); aSquare.setBorderStyle(borderThick); aSquare.setRectangle(position); result = new ArrayList<>(page.getAnnotations()); // copy page.getAnnotations().add(aSquare); // The following lines are needed for PDFRenderer to render // annotations. Preview and Acrobat don't seem to need these. if (null == aSquare.getAppearance()) { aSquare.setAppearance(new PDAppearanceDictionary()); PDAppearanceStream annotationAppearanceStream = new PDAppearanceStream(document); position.setLowerLeftX(lowerLeftX - borderThick.getWidth() * 0.5f); position.setLowerLeftY(lowerLeftY - borderThick.getWidth() * 0.5f); position.setUpperRightX(lowerLeftX + width + borderThick.getWidth() * 0.5f); position.setUpperRightY(lowerLeftY + height + borderThick.getWidth() * 0.5f); annotationAppearanceStream.setBBox(position); annotationAppearanceStream.setMatrix(new AffineTransform()); annotationAppearanceStream.setResources(page.getResources()); try (PDPageContentStream appearanceContent = new PDPageContentStream(document, annotationAppearanceStream)) { Matrix transform = new Matrix(); appearanceContent.transform(transform); appearanceContent.addRect(lowerLeftX, lowerLeftY, width, height); appearanceContent.setLineWidth(borderThick.getWidth()); appearanceContent.setNonStrokingColor(fillColor); appearanceContent.setStrokingColor(red); appearanceContent.fillAndStroke(); appearanceContent.beginText(); // Center text vertically, left justified appearanceContent.newLineAtOffset(lowerLeftX, lowerLeftY + height * 0.5f - fontSize * 0.5f); appearanceContent.setFont(font, fontSize); appearanceContent.setNonStrokingColor(red); appearanceContent.showText(contents); appearanceContent.endText(); } aSquare.getAppearance().setNormalAppearance(annotationAppearanceStream); } //System.out.println(page.getAnnotations().toString()); } catch (IOException ex) { Logger.getLogger(DocumentCommandWrapper.class.getName()).log(Level.SEVERE, null, ex); result = null; } } catch (NumberFormatException | NullPointerException ex) { System.err.println("\tNon number encountered where floating point number expected."); result = null; } return result; }
From source file:airviewer.EllipseAnnotationMaker.java
License:Apache License
/** * //from w ww . jav a 2 s . co m * @param document * @param arguments(pageNumber, lowerLeftX, lowerLeftY, width, height, contents) * @return */ public static List<PDAnnotation> make(PDDocument document, ArrayList<String> arguments) { assert null != arguments && arguments.size() == 6; assert null != document; List<PDAnnotation> result; try { int pageNumber = parseInt(arguments.get(0)); float lowerLeftX = parseFloat(arguments.get(1)); float lowerLeftY = parseFloat(arguments.get(2)); float width = parseFloat(arguments.get(3)); float height = parseFloat(arguments.get(4)); String contents = arguments.get(5); PDFont font = PDType1Font.HELVETICA_OBLIQUE; final float fontSize = 16.0f; // Or whatever font size you want. //final float lineSpacing = 4.0f; width = max(width, font.getStringWidth(contents) * fontSize / 1000.0f); //final float textHeight = fontSize + lineSpacing; try { PDPage page = document.getPage(pageNumber); PDColor red = new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE); PDColor black = new PDColor(new float[] { 0, 0, 0 }, PDDeviceRGB.INSTANCE); PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary(); borderThick.setWidth(72 / 12); // 12th inch PDRectangle position = new PDRectangle(); position.setLowerLeftX(lowerLeftX); position.setLowerLeftY(lowerLeftY); position.setUpperRightX(lowerLeftX + width); position.setUpperRightY(lowerLeftY + height); PDAnnotationSquareCircle aCircle = new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_CIRCLE); aCircle.setAnnotationName(new UID().toString()); aCircle.setContents(contents); PDColor fillColor = new PDColor(new float[] { .8f, .8f, .8f }, PDDeviceRGB.INSTANCE); aCircle.setInteriorColor(fillColor); aCircle.setColor(red); aCircle.setBorderStyle(borderThick); aCircle.setRectangle(position); result = new ArrayList<>(page.getAnnotations()); // Copy page.getAnnotations().add(aCircle); // The following lines are needed for PDFRenderer to render // annotations. Preview and Acrobat don't seem to need these. if (null == aCircle.getAppearance()) { aCircle.setAppearance(new PDAppearanceDictionary()); PDAppearanceStream annotationAppearanceStream = new PDAppearanceStream(document); position.setLowerLeftX(lowerLeftX - borderThick.getWidth() * 0.5f); position.setLowerLeftY(lowerLeftY - borderThick.getWidth() * 0.5f); position.setUpperRightX(lowerLeftX + width + borderThick.getWidth() * 0.5f); position.setUpperRightY(lowerLeftY + height + borderThick.getWidth() * 0.5f); annotationAppearanceStream.setBBox(position); annotationAppearanceStream.setMatrix(new AffineTransform()); annotationAppearanceStream.setResources(page.getResources()); try (PDPageContentStream appearanceContent = new PDPageContentStream(document, annotationAppearanceStream)) { Matrix transform = new Matrix(); appearanceContent.transform(transform); appearanceContent.moveTo(lowerLeftX, lowerLeftY + height * 0.5f); appearanceContent.curveTo(lowerLeftX, lowerLeftY + height * 0.75f, lowerLeftX + width * 0.25f, lowerLeftY + height, lowerLeftX + width * 0.5f, lowerLeftY + height); appearanceContent.curveTo(lowerLeftX + width * 0.75f, lowerLeftY + height, lowerLeftX + width, lowerLeftY + height * 0.75f, lowerLeftX + width, lowerLeftY + height * 0.5f); appearanceContent.curveTo(lowerLeftX + width, lowerLeftY + height * 0.25f, lowerLeftX + width * 0.75f, lowerLeftY, lowerLeftX + width * 0.5f, lowerLeftY); appearanceContent.curveTo(lowerLeftX + width * 0.25f, lowerLeftY, lowerLeftX, lowerLeftY + height * 0.25f, lowerLeftX, lowerLeftY + height * 0.5f); appearanceContent.setLineWidth(borderThick.getWidth()); appearanceContent.setNonStrokingColor(fillColor); appearanceContent.setStrokingColor(red); appearanceContent.fillAndStroke(); appearanceContent.moveTo(0, 0); appearanceContent.beginText(); appearanceContent.setNonStrokingColor(black); // Center text vertically, left justified appearanceContent.newLineAtOffset(lowerLeftX + borderThick.getWidth(), lowerLeftY + height * 0.5f - fontSize * 0.5f); appearanceContent.setFont(font, fontSize); appearanceContent.showText(contents); appearanceContent.endText(); } aCircle.getAppearance().setNormalAppearance(annotationAppearanceStream); } } catch (IOException ex) { Logger.getLogger(DocumentCommandWrapper.class.getName()).log(Level.SEVERE, null, ex); result = null; } } catch (NumberFormatException | NullPointerException ex) { System.err.println("Non number encountered where floating point number expected."); result = null; } catch (IOException ex) { Logger.getLogger(EllipseAnnotationMaker.class.getName()).log(Level.SEVERE, null, ex); result = null; } return result; }
From source file:airviewer.TextAnnotationMaker.java
License:Apache License
/** * //from w w w . j av a 2s . co m * @param document * @param arguments(pageNumber, lowerLeftX, lowerLeftY); String contents * @return */ public static List<PDAnnotation> make(PDDocument document, ArrayList<String> arguments) { assert null != arguments && arguments.size() == 4; assert null != document; List<PDAnnotation> result; try { int pageNumber = parseInt(arguments.get(0)); float lowerLeftX = parseFloat(arguments.get(1)); float lowerLeftY = parseFloat(arguments.get(2)); String contents = arguments.get(3); PDFont font = PDType1Font.HELVETICA_OBLIQUE; final float fontSize = 16.0f; // Or whatever font size you want. final float lineSpacing = 4.0f; float width = font.getStringWidth(contents) * fontSize / 1000.0f; // font.getStringWidth(contents) returns thousanths of PS point final float textHeight = fontSize + lineSpacing; try { PDPage page = document.getPage(pageNumber); PDColor red = new PDColor(new float[] { 1, 0, 0 }, PDDeviceRGB.INSTANCE); PDBorderStyleDictionary borderThick = new PDBorderStyleDictionary(); borderThick.setWidth(72 / 12); // 12th inch PDRectangle position = new PDRectangle(); position.setLowerLeftX(lowerLeftX); position.setLowerLeftY(lowerLeftY); position.setUpperRightX(lowerLeftX + width); position.setUpperRightY(lowerLeftY + textHeight); PDAnnotationSquareCircle aSquare = new PDAnnotationSquareCircle( PDAnnotationSquareCircle.SUB_TYPE_SQUARE); aSquare.setAnnotationName(new UID().toString()); aSquare.setContents(contents); PDColor fillColor = new PDColor(new float[] { .8f, .8f, .8f }, PDDeviceRGB.INSTANCE); aSquare.setInteriorColor(fillColor); aSquare.setRectangle(position); result = new ArrayList<>(page.getAnnotations()); // copy page.getAnnotations().add(aSquare); // The following lines are needed for PDFRenderer to render // annotations. Preview and Acrobat don't seem to need these. if (null == aSquare.getAppearance()) { aSquare.setAppearance(new PDAppearanceDictionary()); PDAppearanceStream annotationAppearanceStream = new PDAppearanceStream(document); position.setLowerLeftX(lowerLeftX - borderThick.getWidth() * 0.5f); position.setLowerLeftY(lowerLeftY - borderThick.getWidth() * 0.5f); position.setUpperRightX(lowerLeftX + width + borderThick.getWidth() * 0.5f); position.setUpperRightY(lowerLeftY + textHeight + borderThick.getWidth() * 0.5f); annotationAppearanceStream.setBBox(position); annotationAppearanceStream.setMatrix(new AffineTransform()); annotationAppearanceStream.setResources(page.getResources()); try (PDPageContentStream appearanceContent = new PDPageContentStream(document, annotationAppearanceStream)) { Matrix transform = new Matrix(); appearanceContent.transform(transform); appearanceContent.addRect(lowerLeftX, lowerLeftY, width, textHeight); appearanceContent.setNonStrokingColor(fillColor); appearanceContent.fill(); appearanceContent.beginText(); // Center text vertically, left justified appearanceContent.newLineAtOffset(lowerLeftX, lowerLeftY + textHeight * 0.5f - fontSize * 0.5f); appearanceContent.setFont(font, fontSize); appearanceContent.setNonStrokingColor(red); appearanceContent.showText(contents); appearanceContent.endText(); } aSquare.getAppearance().setNormalAppearance(annotationAppearanceStream); } //System.out.println(page.getAnnotations().toString()); } catch (IOException ex) { Logger.getLogger(DocumentCommandWrapper.class.getName()).log(Level.SEVERE, null, ex); result = null; } } catch (NumberFormatException | NullPointerException ex) { System.err.println("Non number encountered where floating point number expected."); result = null; } catch (IOException ex) { Logger.getLogger(TextAnnotationMaker.class.getName()).log(Level.SEVERE, null, ex); result = null; } return result; }
From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PDFAsVisualSignatureBuilder.java
License:EUPL
public void createSignature(PDSignatureField pdSignatureField, PDPage page, String signatureName) throws IOException { PDSignature pdSignature = new PDSignature(); pdSignatureField.setSignature(pdSignature); pdSignatureField.getWidget().setPage(page); page.getAnnotations().add(pdSignatureField.getWidget()); pdSignature.setName(signatureName);//from ww w.java 2s .c o m pdSignature.setByteRange(new int[] { 0, 0, 0, 0 }); pdSignature.setContents(new byte[4096]); getStructure().setPdSignature(pdSignature); logger.debug("PDSignature has been created"); }
From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.PDFAsVisualSignatureBuilder.java
License:EUPL
public void createSignature(PDSignatureField pdSignatureField, PDPage page, String signatureName) throws IOException { PDSignature pdSignature = new PDSignature(); pdSignatureField.setSignature(pdSignature); pdSignatureField.getWidget().setPage(page); page.getAnnotations().add(pdSignatureField.getWidget()); pdSignature.setName(signatureName);//from w w w. j a v a2s. c o m pdSignature.setByteRange(new int[] { 0, 0, 0, 0 }); pdSignature.setContents(new byte[4096]); getStructure().setPdSignature(pdSignature); logger.debug("PDSignatur has been created"); }
From source file:at.knowcenter.wag.egov.egiz.pdf.PDFUtilities.java
License:EUPL
public static float calculatePageLength(PDPage page, float effectivePageHeight, boolean legacy32, boolean legacy40) throws PDFIOException { try {/*from w w w . j a v a 2 s. co m*/ PDFPage my_page = new PDFPage(effectivePageHeight, legacy32, legacy40); PDResources resources = page.findResources(); if (page.getContents() != null) { COSStream stream = page.getContents().getStream(); // List<PDThreadBead> articles = page.getThreadBeads(); // my_page.processMyPage(page); my_page.processStream(page, resources, stream); } if (!legacy32) { if (page.getAnnotations() != null) { Iterator<PDAnnotation> annotationsIt = page.getAnnotations().iterator(); while (annotationsIt.hasNext()) { PDAnnotation annotation = annotationsIt.next(); if (!annotation.isInvisible()) { my_page.processAnnotation(annotation); } } } } return my_page.getMaxPageLength(); } catch (IOException e) { throw new PDFIOException("error.pdf.stamp.11", e); } }
From source file:chiliad.parser.pdf.extractor.vectorgraphics.VectorGraphicsExtractor.java
License:Apache License
@Override public MPage extract(PDPage pageToExtract, MPage pageContent) { try {/* w ww . ja v a 2 s . c om*/ if (pageToExtract.getContents() == null) { throw new ExtractorException("Contents is null."); } pageSize = pageToExtract.findMediaBox().createDimension(); graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); // initialize the used stroke with CAP_BUTT instead of CAP_SQUARE graphics.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER)); // Only if there is some content, we have to process it. // Otherwise we are done here and we will produce an empty page PDResources resources = pageToExtract.findResources(); processStream(pageToExtract, resources, pageToExtract.getContents().getStream()); List<PDAnnotation> annotations = pageToExtract.getAnnotations(); for (PDAnnotation annotation : annotations) { PDAnnotation annot = (PDAnnotation) annotation; PDRectangle rect = annot.getRectangle(); String appearanceName = annot.getAppearanceStream(); PDAppearanceDictionary appearDictionary = annot.getAppearance(); if (appearDictionary != null) { if (appearanceName == null) { appearanceName = "default"; } Map<String, PDAppearanceStream> appearanceMap = appearDictionary.getNormalAppearance(); if (appearanceMap != null) { PDAppearanceStream appearance = (PDAppearanceStream) appearanceMap.get(appearanceName); if (appearance != null) { Point2D point = new Point2D.Float(rect.getLowerLeftX(), rect.getLowerLeftY()); Matrix matrix = appearance.getMatrix(); if (matrix != null) { // transform the rectangle using the given matrix AffineTransform at = matrix.createAffineTransform(); at.transform(point, point); } graphics.translate((int) point.getX(), -(int) point.getY()); processSubStream(pageToExtract, appearance.getResources(), appearance.getStream()); graphics.translate(-(int) point.getX(), (int) point.getY()); } } } } return handleResult(graphics, pageContent); } catch (IOException ex) { throw new ExtractorException("Failed to extract vector graphics.", ex); } }
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 w w .j a va 2 s. c om*/ * 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; }