0

I am trying to save xml file as PDF as it is. In other words, I am trying to create PDF file that shows content of XML like a screenshot (like raw screenshot). My client somehow needs it like this. I couldn't really find the same question on stackoverflow. Is there anyway I can do this using iText or some other library?

Thank you!

  • Bit too broad. You would need to get the string representation of the XML file, and use a PDF creation library to create a text PDF document – Charlieface Aug 18 '21 at 20:18
  • You could [format the XML](https://stackoverflow.com/q/1123718/3744182) then save it to PDF and preserve formatting by using a monospaced font e.g. as shown in [Adding text to a PDF file from a text file](https://stackoverflow.com/q/38782784/3744182). – dbc Aug 18 '21 at 20:23
  • Because .NET does not have a built in PDF rendering capability, it would probably be best to first try one out, and submit a question if difficulties arise with the specific library. Getting the contents of an xml file will be pretty easy, but to answer how would depend on requirements. for example, does it need to be formatted? – DannyMeister Aug 18 '21 at 20:24

2 Answers2

0

If you load the xml in a browser you can easily save to searchable PDF enter image description here

In a shell call (replace msedge with chrome if necessary)

"path to\msedge.exe" --headless --disable-gpu --print-to-pdf="out path\xml.pdf" --enable-logging "file://path to A\file.xml"

Enable logging helps as it can take a long time to process without any visual progress.

[0818/231038.640:INFO:headless_shell.cc(648)] Written to file ...\xml.pdf.

You can also add --print-to-pdf-no-header. Also if adding some style consider --run-all-compositor-stages-before-draw but I have no idea if that works for xml.

ForGet Image --screenshot as a 40 Page high XML as JPEG does NOT translate well to PDF. I tried :-)

If you want that as an image PDF then Re-Print the PDF to PDF using a command line viewer such as this since it is ONLY Print As Image output :-) also note it can in addition read the XML in Black and White (NO linting).

enter image description here

But have not tested how well it does XML2PDF via command line print

SumatraPDF -print-to "My Print to PDF" "path to\filename.pdf" (or xml in mono)

Note "My Print to PDF" is a promptless port you need to configure as required.

K J
  • 8,045
  • 3
  • 14
  • 36
0

First extract your text from XML file:

XmlDocument doc = new XmlDocument();
doc.Load("c:\\temp.xml");
string myText;
foreach(XmlNode node in doc.DocumentElement.ChildNodes){
   myText= node.InnerText; //or loop through its children as well
}

Then create a PDF file and pass this text into it

in this case here I use PDFFlow library to create pdf documents

var DocumentBuilder.New()  
  .AddSection().AddParagraphToSection(myText).ToDocument()  
   .Build("Result.PDF");