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


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


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.

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:


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:


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