Why won't Convert.ToPDF converrt my xml document as plain text?

Q:

I’m trying to convert a text file, containing XML, to PDF on Windows. But I’m getting an error. Why?

A:

Convert.ToPDF’s txt-to-PDF conversion is, on Windows, performed using Office interop. If you use the Convert add-on to convert the text file to PDF, you will need to make sure Word displays the document as desired.

In the case of one such document, Office appears to give an error when opening it:

The document sbml_xml_as_txt.txt cannot be opened because there are problems with the contents.

Details:
A text/xml declaration may occur only at the very beginning of input.

Location: Line: 1, Column: 40

It seems that Office is detecting that the file is XML, and thus refuses to open it as text.

If you’d like to continue using Office to layout the text, you would need to find some way to configure Office to open this file without complaint.

An alternative would be to give the file a new extension (".plaintext") which is associated with a program able to print text documents (such as a text editor).

There are, of course, many other ways to use the PDFNet SDK to add text to a PDF page.

To start with, you may want to take a look at this sample:

http://www.pdftron.com/pdfnet/samplecode/data/Text2PDF.zip

It shows how to write lines of text, starting a new line at a specific width.

To fill text to an exact width, the following KB article might be helpful:

https://groups.google.com/d/msg/pdfnet-sdk/MdEMEkr6wxs/qv7ezDu2QSkJ

To scale and place arbitrary text on a PDF page, you might want to look at the stamper sample:

http://www.pdftron.com/pdfnet/samplecode.html#Stamper