如何使用pdfbox编写带语法高亮的java代码

时间:2015-09-21 19:33:31

标签: java pdf pdfbox

我正在尝试创建一个 java 项目,只需打印项目源代码,无论是java,php,c ++等。 我可以使用iText创建PDF,但现在我需要某种突出显示java代码,我读取的方式与sublime代码编辑器相同。我发现了pdfbox:一个用于创建/操作PDF文件的库,但是我无法通过使用这个库来找到如何突出显示代码文本(如sublime)。有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

从另一个SO问题复制:highlight text using pdfbox when it's location in the pdf is known

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