'C# - Save XML file as PDF as Raw Image (not converting)

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!



Solution 1:[1]

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"); 

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohammed El-Sharnouby
Solution 2