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); AddTextToPackageContent(xdDocument); xdDocument.Close(); }
2. Create document sequence of this package and add fixed document to to document sequence
IXpsFixedDocumentSequenceWriter ifdswDocumentSequence = xdDocument.AddFixedDocumentSequence(); IXpsFixedDocumentWriter ifdwDocument = ifdswDocumentSequence.AddFixedDocument(); AddTextToDocumentContent(ifdwDocument); ifdwDocument.Commit(); ifdswDocumentSequence.Commit();
3. For each document, add pages and these pages contains our content
IXpsFixedPageWriter ifpwPage = ifdwDocument.AddFixedPage(); dictResources = AddResourcesToPage(ifpwPage); WriteTextPageContent(ifpwPage.XmlWriter, lstLineSet[nIndex], dictResources); ifpwPage.Commit();
4. Add content to page and set font
List<XpsResource> xpsFonts = lstResources["XpsFont"]; // Element are indented for reading purposes only writerXML.WriteStartElement("FixedPage"); writerXML.WriteAttributeString("Width", cnst_nPageWidth.ToString()); writerXML.WriteAttributeString("Height", cnst_nPageHeight.ToString()); writerXML.WriteAttributeString("xmlns", "http://schemas.microsoft.com/xps/2005/06"); writerXML.WriteAttributeString("xml:lang", "en-US"); for(int nIndex=0; nIndex < strLine.Length; nIndex++) { writerXML.WriteStartElement("Glyphs"); writerXML.WriteAttributeString("Fill", "#ff000000"); writerXML.WriteAttributeString( "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]); writerXML.WriteEndElement(); } writerXML.WriteEndElement();
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.
hi,
i have an issue….when i convert a .lis file into XPS format…..it doesn’t support the format.
Please help.
Hi,
Can you please share the source code of this text to xps and link above says page not found.
Thank You