What is XPS files and how it is being used
Asked Answered
E

2

6

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.

Enviable answered 2/4, 2013 at 7:28 Comment(3)
If you're writing an application to handle a file format, you should probably know the format cold, inside and out.Legionnaire
This is for self learning Im searching for xps and bitmapEnviable
When I run the above code an error showing as XamlParserException occured ('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 line of code FixedDocumentSequence sequence = xps.GetFixedDocumentSequence();Enviable
S
4

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

Sharasharai answered 12/4, 2013 at 4:20 Comment(1)
That's a stupid error message, but yay for the solution!Blindworm
O
1

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.

Overwhelm answered 3/4, 2013 at 16:53 Comment(1)
Most probably you are right. Im very new to the xps file concept. I have downloaded sample XPS file from 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 dont know how to use thes files.Enviable

© 2022 - 2024 — McMap. All rights reserved.