none
How to convert xps documents to other formats, for example bmp ?

    Question

  • Hi, I have one question to ask.
    How to convert xps documents to other formats, for example bmp format?
    Thanks!
    Friday, November 11, 2005 7:23 AM

Answers

  • XPS is an xml markup format that represents a page's content in WPF's (Windows Presentation Foundation) vector format.  The code below shows how to get a page's visual, and save it as a .bmp file.  You will end up with 1 bitmap per page.

    If you're looking to convert XPS Documents into another vector or markup based format, then your best bet is to follow the XPS Spec and transform the XPS markup into your own format.  You can find the XPS Spec book on: http://www.microsoft.com/whdc/xps/downloads.mspx


    // Save XPS Document page(s) to .bmp
    // Current as of Sept '05 WinFX CTP

    using System.IO;
    using System.Windows.Documents;
    using System.Windows.Xps.Packaging;
    using System.Windows.Media.Imaging;


    namespace GenerateXpsBitmaps
    {
        static public class XpsBitmapHelper
        {
            static public void SaveXpsPageToBitmap(string xpsFileName, int[] pages)
            {
                XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
                FixedDocumentSequence docSeq = xpsDoc.GetPackageRoot();

                // You can get the total page count from docSeq.PageCount

                foreach (int pageNum in pages)
                {
                    DocumentPage docPage = docSeq.GetPage(pageNum);
                    BitmapImage bitmap = new BitmapImage();
                    RenderTargetBitmap renderTarget =
                        new RenderTargetBitmap( (int) docPage.Size.Width,
                                                (int) docPage.Size.Height,
                                                96, // WPF (Avalon) units are 96dpi based
                                                96,
                                                System.Windows.Media.PixelFormats.Bgra32);

                    renderTarget.Render(docPage.Visual);

                    BitmapEncoder encoder = new BmpBitmapEncoder();  // Choose type here ie: JpegBitmapEncoder, etc
                    encoder.Frames.Add(BitmapFrame.Create(renderTarget));

                    FileStream pageOutStream = new FileStream(xpsDoc + ".Page" + pageNum + ".bmp", FileMode.Create, FileAccess.Write);
                    encoder.Save(pageOutStream);
                    pageOutStream.Close();
                }
            }
        }
    }

    Tuesday, November 15, 2005 7:16 PM

All replies

  • XPS is an xml markup format that represents a page's content in WPF's (Windows Presentation Foundation) vector format.  The code below shows how to get a page's visual, and save it as a .bmp file.  You will end up with 1 bitmap per page.

    If you're looking to convert XPS Documents into another vector or markup based format, then your best bet is to follow the XPS Spec and transform the XPS markup into your own format.  You can find the XPS Spec book on: http://www.microsoft.com/whdc/xps/downloads.mspx


    // Save XPS Document page(s) to .bmp
    // Current as of Sept '05 WinFX CTP

    using System.IO;
    using System.Windows.Documents;
    using System.Windows.Xps.Packaging;
    using System.Windows.Media.Imaging;


    namespace GenerateXpsBitmaps
    {
        static public class XpsBitmapHelper
        {
            static public void SaveXpsPageToBitmap(string xpsFileName, int[] pages)
            {
                XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
                FixedDocumentSequence docSeq = xpsDoc.GetPackageRoot();

                // You can get the total page count from docSeq.PageCount

                foreach (int pageNum in pages)
                {
                    DocumentPage docPage = docSeq.GetPage(pageNum);
                    BitmapImage bitmap = new BitmapImage();
                    RenderTargetBitmap renderTarget =
                        new RenderTargetBitmap( (int) docPage.Size.Width,
                                                (int) docPage.Size.Height,
                                                96, // WPF (Avalon) units are 96dpi based
                                                96,
                                                System.Windows.Media.PixelFormats.Bgra32);

                    renderTarget.Render(docPage.Visual);

                    BitmapEncoder encoder = new BmpBitmapEncoder();  // Choose type here ie: JpegBitmapEncoder, etc
                    encoder.Frames.Add(BitmapFrame.Create(renderTarget));

                    FileStream pageOutStream = new FileStream(xpsDoc + ".Page" + pageNum + ".bmp", FileMode.Create, FileAccess.Write);
                    encoder.Save(pageOutStream);
                    pageOutStream.Close();
                }
            }
        }
    }

    Tuesday, November 15, 2005 7:16 PM
  • thank you, Dennis Quintela
    You offered a simple way to converting xps documents to image formats, for exmaple bmp, tif, gif and so on. Can the useful way convet xps to RTF file format?  the XPS Spec is very complicated for me.
    Thanks!
    Wednesday, November 16, 2005 1:31 AM
  • At this point, there is no simple way to convert an XPS document to RTF.
    Tuesday, December 06, 2005 11:25 PM
  • This only works if xps dosen't contains {Binding} expressions.
    Wednesday, December 07, 2005 7:41 AM
  • I found your code to be very helpful. However I am finding that the Canvas.Resources attributes in the xps documents are not being rendered in the bitmap outputs.

    I've altered the the suggested code slightly to match the updated specifications:

    -> FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();

    -> DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageNum);

    -> RenderTargetBitmap renderTarget =new RenderTargetBitmap((int)docPage.Size.Width,(int)docPage.Size.Height,96, 96,PixelFormats.Pbgra32);

    I hope my alterations haven't caused the problem...do you have any ideas on how I could resolve the canvas.resources problem?

    Thanks

    Friday, October 13, 2006 1:45 AM
  • Do you have any ideas, how I could overcome the {binding} expressions problem.
    My XPS document contains {binding} expressions.
    Wednesday, October 18, 2006 12:48 AM
  • I have discovered that there is another way to extract bitmaps: to use FixedPage instead of the DocumentPage proposed:

    FixedDocumentSequence seq = (new XpsDocument(args[0], FileAccess.Read)).GetFixedDocumentSequence();

    int i = 0;
    foreach (DocumentReference DocRef in seq.References) {
    FixedDocument doc = DocRef.GetDocument(false);

    foreach (PageContent content in doc.Pages) {
    FixedPage page = content.GetPageRoot(false);
    writeln("Saving page " + i);
    savePage(page, new Size(page.Width, page.Height), new PngBitmapEncoder(), args[0] + "[" + i + "]"+ ".png");
    i++;
    }
    }



    Where the original method was:

    DocumentPaginator paginator = (new XpsDocument(args[0], FileAccess.Read)).GetFixedDocumentSequence().DocumentPaginator;
    paginator.ComputePageCount();
    writeln("Extracting " + paginator.PageCount + " pages");
    for (int i = 0; i < paginator.PageCount ; i++) {
    DocumentPage page = paginator.GetPage(i);
    writeln("Saving page " + i);
    BitmapFixedPageExtractor.savePage(page.Visual, page.Size, new PngBitmapEncoder(), args[0] + "[" + i + "]"+ ".png");
    }



    and the visual save method is plain

    public static void savePage(Visual visual, Size size, BitmapEncoder encoder, string filename) {
    RenderTargetBitmap renderTarget =
    new RenderTargetBitmap( (int) size.Width,
    (int) size.Height,
    96, // WPF (Avalon) units are 96dpi based
    96,
    System.Windows.Media.PixelFormats./*Bgra32*/Pbgra32);

    renderTarget.Render(visual);

    encoder.Frames.Add(BitmapFrame.Create(renderTarget));
    using (FileStream pageOutStream = new FileStream(filename, FileMode.Create, FileAccess.Write)) {
    encoder.Save(pageOutStream);
    pageOutStream.Close();
    }
    }




    1. The question is what is the difference (which one is better)?

    2. Another question is should I call ComputerPagesCount() before accessing PageCount property of fixed document paginator? I see that some users don't do that.
    Monday, May 28, 2007 8:36 PM
  • I tried both of the methods used above.  The original works fine, however the FixedPage example only produces images for the FIRST page.  Subsequent pages were blank.

    Wednesday, July 04, 2007 9:06 AM
  •  

    is that first method availabe in c or c++?

     

    Peter

    Tuesday, September 25, 2007 10:40 PM
  • I'm too looking for a way to convert xps to bitmap, and this code looks good, but does not even compile on my system. I always get these two errors:

     

    error CS1061: 'System.Windows.Xps.Packaging.XpsDocument' does not contain a definition for 'GetPackageRoot' and no extension method 'GetPackageRoot' accepting a first argument of type 'System.Windows.Xps.Packaging.XpsDocument' could be found (are you missing a using directive or an assembly reference?)

    error CS1061: 'System.Windows.Documents.FixedDocumentSequence' does not contain a definition for 'GetPage' and no extension method 'GetPage' accepting a first argument of type 'System.Windows.Documents.FixedDocumentSequence' could be found (are you missing a using directive or an assembly reference?)

     

    Have these method names changed? I can't think (or find) any reference I might be missing Sad

     

    Thanks,
    Sam

    Sunday, February 17, 2008 6:44 PM
  •  

    Hi Dennis,

     

    Thanks for the info. When I tried your code, I get a  .bmp file with just a black background. I don't see any text. Would you happen to have an idea why this is so?

     

    Regards.

    Sunday, April 27, 2008 11:14 PM
  • Hi Guys,

    Does any one no how to convert from Bitmap to XPS?

    Thanks in advanced

    James
    Wednesday, August 06, 2008 7:49 AM
  • Sam Jost, 


    It seems like the newer version of .NET Framework has changed a bit. I have rewritten Dennis Quintela's code as follows:


    1 using System;  
    2 using System.IO;  
    3 using System.IO.Packaging;  
    4 using System.Windows.Documents;  
    5 using System.Windows.Xps.Packaging;  
    6 using System.Windows.Media.Imaging;  
    7 using System.Collections.Generic;  
    8  
    9 namespace xps2bmp  
    10 {  
    11     class Program   
    12     {  
    13         [STAThread]  
    14         static void Main(string[] args)  
    15         {  
    16             try   
    17             {  
    18                 SaveXpsPageToBitmap(args[0]);  
    19             }  
    20             catch (Exception ex)  
    21             {  
    22                 Console.WriteLine(ex.Message);  
    23             }  
    24         }  
    25    
    26         static public void SaveXpsPageToBitmap(string xpsFileName)  
    27         {  
    28             XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);  
    29             FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();  
    30    
    31             // You can get the total page count from docSeq.PageCount   
    32             for (int pageNum = 0; pageNum < docSeq.DocumentPaginator.PageCount; ++pageNum)  
    33             {  
    34                 DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageNum);  
    35                 BitmapImage bitmap = new BitmapImage();  
    36                 RenderTargetBitmap renderTarget =  
    37                     new RenderTargetBitmap((int)docPage.Size.Width,  
    38                                             (int)docPage.Size.Height,  
    39                                             96, // WPF (Avalon) units are 96dpi based   
    40                                             96,  
    41                                             System.Windows.Media.PixelFormats.Default);  
    42    
    43                 renderTarget.Render(docPage.Visual);  
    44    
    45                 BitmapEncoder encoder = new BmpBitmapEncoder();  // Choose type here ie: JpegBitmapEncoder, etc  
    46                 encoder.Frames.Add(BitmapFrame.Create(renderTarget));  
    47    
    48                 FileStream pageOutStream = new FileStream(xpsFileName + ".Page" + pageNum + ".bmp", FileMode.Create, FileAccess.Write);  
    49                 encoder.Save(pageOutStream);  
    50                 pageOutStream.Close();  
    51             }  
    52         }  
    53     }  
    54 }  
    55  


    Try using this. It should work.


    Regards,

    --
    Victor

    Monday, August 25, 2008 2:18 PM
  • What references do I need to add to link this program?
    siegfried heintze
    Monday, December 15, 2008 6:12 AM
  • The following seems to work:

    csc /out:xps_to_bmp.exe /checked  /d:noprompt  /debug xps_to_bmp.cs /r:ReachFramework.dll /r:System.dll /r:System.Windows.Forms.dll /r:System.Drawing.dll /r:System.Xml.dll /debug:full /r:WindowsBase.Dll /r:PresentationCore.Dll /r:PresentationFramework.Dll


    siegfried heintze
    Monday, December 15, 2008 7:57 AM
  • I hope you guys don't get mad at me for doing this:   I took the code and made a small project out of it.   I have posted the project on codeplex.   http://www.codeplex.com/XPS2Image

    Personally, I would love it if some other people got involved.  We could make a cool little utility out of it.

    Matt
    Tuesday, January 13, 2009 4:40 PM
  • Where exactly do I use this code? What program would I open to do this? I have an XPS file that I am trying to change to a .bmp so I can edit it to hidden into a sound file. I am using Coagula in junction with Sonic Visualizer. All I need to know how to do is change this damn xps file to .bmp, I know you wrote how to already but I havent a clue as to where to input this code or how to modify it for my file...please help
    Tuesday, October 27, 2009 12:30 AM


  • You can use PDFTron XPSConvert (http://www.pdftron.com/xpsconvert/index.html)

    to convert XPS to PDF, JPEG, TIF, PNG, bmp etc.

    For programmatic conversion use PDFNet SDK (http://www.pdftron.com/pdfnet/).

    Aki

    Thursday, February 18, 2010 12:36 AM
  • I get an error in the FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence(); statement.

    I´m trying to convert files generated from Word or from ofiice´s printer.

    Have you check this error?

    Any Idea of what would happen?

    thanks

    Saturday, July 10, 2010 8:35 PM
  • this Function need to get called from an STAtread

    so if you cant make your main thread sta you have to make a new thread with appartmentstate sta and  call the function from there

    the best is you start the whole xpstoimage function in the thread

    Tuesday, August 24, 2010 7:09 PM
  • Are there any C++ code available for the conversion..

    Pls help...

    Friday, November 26, 2010 4:17 AM
  • Matt,

    I've done some work on the solution above and blogged about it at http://craigwatson1962.wordpress.com/2010/11/26/convert-an-xps-to-jpeg-png-tiff-or-bmp-in-a4-pages/

    Features added include:

    • cleaning up some of the memory mangement,
    • added tiling the image into A4 or A3 pages
    • writing multiple A4 or A3 graphics files, or
    • single multiple page tiff file. 
    Friday, November 26, 2010 8:42 PM
  • Matt,

    I've done some work on the solution above and blogged about it at http://craigwatson1962.wordpress.com/2010/11/26/convert-an-xps-to-jpeg-png-tiff-or-bmp-in-a4-pages/

    Features added include:

    • cleaning up some of the memory mangement,
    • added tiling the image into A4 or A3 pages
    • writing multiple A4 or A3 graphics files, or
    • single multiple page tiff file. 


    The code from this blog post can be found in an example codeplex project - http://xpsconverter.codeplex.com/

    it features one click running

     

    David

    Monday, July 11, 2011 10:59 AM
  • @Victor Uhh... No. I got this error:

    Thursday, October 25, 2012 12:05 PM