0

I'm looking for a way to add a formatted code to a pdf file. Now I use iText 5.5.10 to generating pdf. I need to recognize syntax and color it.

iText library can something like this, or can with another library? Maybe I must change the library to generating pdf?

1 Answers1

1

I had to flag your question as off topic because of

Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.

However, I still have a (partial) answer for you. What you are looking for, is called a Syntax Highlighter. A very popular and well-known one is GeSHi (General Syntax Highlighter), but that lives in the PHP world. You are looking for a "GeSHi for Java". I personally don't know any, but I suggest that you google for "java syntax highlighter library". One of the search results is JHighlight, with description "JHighlight is an embeddable pure Java syntax highlighting library that supports Java, HTML, XHTML, XML and LZX languages and outputs to XHTML." I haven't tried it myself. There are others, please continue your research.

Your question essentially boils down to a partial duplicate of Where can I find a syntax highlighting library for Java?

What you do then, is:

  • start with source code as plain text
  • use a syntax highlighter library to convert it to formatted (X)HTML
  • use iText to convert the (X)HTML to PDF

I strongly recommend that you do not use iText 5, but use iText 7 + pdfHTML add-on instead. The HTML-to-PDF functionality is much richer in iText 7, and iText 5 is end-of-life (only security updates for customers).

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101