I have a simple C# .net web application. In that I'm working with XPS files. I have used the following code
private void button1_Click(object sender, EventArgs e)
{
try
{
string xpsFile = "D:\\Completed-Form.xps";
xpsToBmp(xpsFile);
MessageBox.Show("Done");
}
catch (Exception ex)
{
MessageBox.Show (ex.Message);
}
}
static public void xpsToBmp(string xpsFile)
{
XpsDocument xps = new XpsDocument(xpsFile, System.IO.FileAccess.Read);
FixedDocumentSequence sequence = xps.GetFixedDocumentSequence();
for (int pageCount = 0; pageCount < sequence.DocumentPaginator.PageCount; ++pageCount)
{
DocumentPage page = sequence.DocumentPaginator.GetPage(pageCount);
RenderTargetBitmap toBitmap = new RenderTargetBitmap((int)page.Size.Width,(int)page.Size.Height,96,96,System.Windows.Media.PixelFormats.Default);
toBitmap.Render(page.Visual);
BitmapEncoder bmpEncoder = new BmpBitmapEncoder();
bmpEncoder.Frames.Add(BitmapFrame.Create(toBitmap));
FileStream fStream = new FileStream("D:\\xpstobmp" + pageCount + ".bmp", FileMode.Create, FileAccess.Write);
bmpEncoder.Save(fStream);
fStream.Close();
}
}
When I debug the code, an error showing as XamlParserException
occurred
'The invocation of the constructor on type 'System.Windows.Documents.DocumentReference' that matches the specified binding constraints threw an exception.' Line number '2' and line position '20'.
in the following line of code:
FixedDocumentSequence sequence = xps.GetFixedDocumentSequence();
I have downloaded a sample XPS file from http://msdn.microsoft.com/en-us/library/windows/hardware/gg463422.aspx (I got 160MB zip file from there. When I unzip it there were a number of folders and files having .xps extension. I don't know how to use these files) and used in the above code. I'm very new to this file concept. I don't know how to resolve this error and how the .xps files are used. Also I have little knowledge about bitmap files.
XPS files or XML Paper Specification files are Microsoft's direct competitor to Adobe's PDF format. XPS Viewer is a tool used to view XPS files, similar to Adobe Reader. While XPS might not be as popular as PDF, some Windows users still work with it.
The main difference between XPS and PDF is that XPS files can be viewed using an XPS Viewer while PDF files can be viewed using Adobe Reader. XPS and PDF are two file formats. XPS is a Microsoft version of PDF. Mac computers and mobile devices do not have built-in XPS viewers.
A key advantage of both XPS and PDF formats is that the fonts used in the document are embedded in the file so that the document will display and print properly on other computers and printers.
It was invented by Microsoft as a replacement for PDF files. XPS files can be used to archive and share files you routinely use for your business, such as contracts, receipts, memos, invoices or Web pages. The files are created by Microsoft XPS Document Writer, but can be opened by Microsoft XPS Viewer.
Even i faced the same problem while running as a Windows Application.
The solution to that is :
The calling thread must be in STA mode. Most project created by Visual Studio is set to MTA by default.
What you can do is to run your code inside STA thread.
I tried in: Visual Studio 2010, Windows XP Srv Pack 3 64 Bit, And .Net Framework 4.0
Good Luck...
Accept this as Answer if it solves your issue
Your code is working, I just tested on my environment (VS 2010, Windows 7 64bit).
As input file I used a google page printed with the built-in Microsoft XPS Document Writer.
So the issue is with the XPS document you are testing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With