I am trying to create a java project that simply prints a project source code, be it java, php, c++ and others. I can create the PDF just fine with iText, but now I need some kind of highlighting the java code I read the same way a code editor like sublime highlights. I discovered pdfbox: a library for creating/manipulating PDF files, but I can't find how to highlight code text(like sublime does) by using this library. Any help?
Asked
Active
Viewed 494 times
-1
-
A library for creating&manipulating pdf files can't do syntax highlighting. You need to combine multiple libraries to solve your issue. Or use existing tools like LaTex to do it https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings – zapl Sep 21 '15 at 19:52
-
1Your question isn't really a PDF question; the real problem is "which color for what component of the text". Only if you have solved that you can start with writing to PDF. Then the only "problem" is how to print a text in a certain color, but that is easy regardless which lib you are using. – Tilman Hausherr Sep 21 '15 at 20:11
-
so... Any hints for the "which color for what component of the text" problem? Tilman? – Fabio Phillip Rocha Marques Sep 21 '15 at 20:43
-
cool, zapl! Which libraries do you reccomend for combining? – Fabio Phillip Rocha Marques Sep 21 '15 at 20:44
1 Answers
0
Copying from another SO question : 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*/);

Community
- 1
- 1

roymustang86
- 8,054
- 22
- 70
- 101
-
yes, I am looking more for a way to highlight methods, classes etc. without me having to go line through line of code highlighting it manually – Fabio Phillip Rocha Marques Sep 21 '15 at 20:25