highlight text using pdfbox when it's location in the pdf is known

匿名 (未验证) 提交于 2019-12-03 01:10:02

问题:

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?

回答1:

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*/); 


回答2:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!