Does pdfbox provide some utility to highlight the text when I have it's co-ordinates?
Bounds of the text is known.
I know there are other libraries that provide the same functionality like pdfclown etc. But does pdfbox provide something like that?
Does pdfbox provide some utility to highlight the text when I have it's co-ordinates?
Bounds of the text is known.
I know there are other libraries that provide the same functionality like pdfclown etc. But does pdfbox provide something like that?
well i found this out. it is simple.
PDDocument doc = PDDocument.load(/*path to the file*/); PDPage page = (PDPage)doc.getDocumentCatalog.getAllPages.get(i); List annots = page.getAnnotations; PDAnnotationTextMarkup markup = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.Su....); markup.setRectangle(/*your PDRectangle*/); markup.setQuads(/*float array of size eight with all the vertices of the PDRectangle in anticlockwise order*/); annots.add(markup); doc.save(/*path to the output file*/);
This works for pdfbox 2.0.7
PDDocument document = /* get doc */ /* numeration is index-based. Starts from 0 */ List annotations = document.getPage(yourPageNumber - 1).getAnnotations(); PDAnnotationTextMarkup highlight = new PDAnnotationTextMarkup(PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT); highlight.setRectangle(PDRectangle.A4); // quadPoints is array of x,y coordinates in Z-like order (top-left, top-right, bottom-left,bottom-right) // of the area to be highlighted highlight.setQuadPoints(quadPoints); PDColor yellow = new PDColor(new float[]{1, 1, 204 / 255F}, PDDeviceRGB.INSTANCE); highlight.setColor(yellow); annotations.add(highlight);
Note: such annotation will be displayed if you save doc in file, but it will not appear in image created from page since there is no AppearanceStream created for this annotation. I solved it with code drafts from PDFBOX-3353