Print xml in pdf using itext

会有一股神秘感。 提交于 2019-12-13 07:45:32

问题


I want to print xml in pdf using itext in java, as well formatted and displayed in color and indention as well like shown in notepad++,

any api or suggestion regarding this?


回答1:


I have converted XHTML to pdf, via iText, using flying saucer for the rendering (previously xhtml renderer).

http://code.google.com/p/flying-saucer/

You can format using CSS, though I do remember it's slightly temperamental, however you can tweak it to get what you want, and end up with something nicely formatted.

I wasn't sure whet you meant regarding Notepad++ - I don't have PDF support there, just opens as Binary file contents, unless there is a PDF plugin you use?

::Answer updated after comments below.

Thanks for the comment, I understand the question much better now. I thought you wanted to output the data in the XML in the PDF, now I understand you want to see the raw XML itself in the PDF, formatted as you'd see XML formatted in Notepad, colours and all.

XML is a markup language designed to describe data, so you want to get this into a language that can descibe the presentation and style as well as the data. I'd suggest

1) Convert the XML to XHTML - so all the XML (tags, attributes) is your content, and you have classes describing each type (for example, attribute names, attribute values, starter tag, end tag). I don't know if you can use an XSLT library to transform it this way, oterwise you can write something yourself in Java, walking through the DOM and output it in the way you want. This way you can

2) Create CSS to style your classes as you want - e.g. have all attribute names as text color "red"

3) Use iText and flying saucer as above to convert the XHTML and CSS into PDF using Java, as described in original answer



来源:https://stackoverflow.com/questions/7634950/print-xml-in-pdf-using-itext

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