C# – Convert txt to xps

When I tried to display a text file on DocumentViewer I met a problem that the DocumentViewer component only supports Fixed Document such as XPS document. This problem put me before a mission to convert my text file into XPS document. I think it may be simple because Microsoft surely supports so that man can convert all types of text file into XPS. Microsoft has created this format, he can not put his developers into trouble but I am wrong. After searching with Google I found out that there is no easy way, for example with one line of code, to convert text file to XPS document so I must find out a walk-around to solve it.
Instead of converting text file to XPS document, I decide to create a new XPS document and insert the content of text file into it. To create XPS document, we should understand a little about its structure as I illustrate with very basic concept in figure bellow

As described in figure above, to create a XPS document, we will follow these steps:
– Create a package and XPS document
– Create document sequence of this package and add fixed document to to document sequence
– For each document, add pages and these pages contains our content.
– During creating pages, we must insert relevant resources, for example Fonts, Images, …
Following these steps I used following code snippet

1. Create a package and XPS document

using (Package pckXPS = Package.Open(strDestFile))
	XpsDocument xdDocument = new XpsDocument(pckXPS);

2. Create document sequence of this package and add fixed document to to document sequence

IXpsFixedDocumentSequenceWriter ifdswDocumentSequence = xdDocument.AddFixedDocumentSequence();
IXpsFixedDocumentWriter ifdwDocument = ifdswDocumentSequence.AddFixedDocument();

3. For each document, add pages and these pages contains our content

IXpsFixedPageWriter ifpwPage = ifdwDocument.AddFixedPage();
dictResources = AddResourcesToPage(ifpwPage);
	lstLineSet[nIndex], dictResources);

4. Add content to page and set font

List<XpsResource> xpsFonts = lstResources["XpsFont"];
// Element are indented for reading purposes only
writerXML.WriteAttributeString("Width", cnst_nPageWidth.ToString());
writerXML.WriteAttributeString("Height", cnst_nPageHeight.ToString());
writerXML.WriteAttributeString("xml:lang", "en-US");

for(int nIndex=0; nIndex < strLine.Length; nIndex++)
	writerXML.WriteAttributeString("Fill", "#ff000000");
		"FontUri", xpsFonts[0].Uri.ToString());
	writerXML.WriteAttributeString("FontRenderingEmSize", "18");
	writerXML.WriteAttributeString("OriginX", cnst_nStartXOffset.ToString());
	writerXML.WriteAttributeString("OriginY", (cnst_nStartYOffset + nIndex*cnst_nLineSpacing).ToString() );
	writerXML.WriteAttributeString("UnicodeString", strLine[nIndex]);

I can convert text file to XPS document with code above but there are a lot of bugs to be solved for example encoding and line overflowing as in figure below

The complete source code of this tool you can download here “Txt To XPS“. I will try to solve the bugs above, if you know how man can solve them then don’t hesitate to share it here.


  1. hi,

    i have an issue….when i convert a .lis file into XPS format…..it doesn’t support the format.
    Please help.

Leave a comment

Your email address will not be published. Required fields are marked *