How to convert XPS file to Bitmap

The acronym XPS stands for XML Paper Specification, which serves as a digital document format that is structured based on XML. An XpsDocument, in essence, encapsulates a FixedDocumentSequence consisting of one or more FixedDocument elements. One of the notable advantages of XPS files is their ease of sharing, as they can be viewed on any computer equipped with an XPS viewer. This allows for seamless accessibility and viewing of XPS documents, even on systems that may lack the specific programs used in creating the original documents.

The following C# program convert and xps document to a bitmap image. Create a new C# project and add a Button to Form and add the following references to your project.

Go to Project->Add References and select these files from .Net tab.

  1. windowsbase.dll
  2. ReachFramework.dll
  3. PresentationFramework.dll
  4. Milk
  5. PresentationCore.dll
Full Source C#
using System; using System.IO; using System.IO.Packaging; using System.Windows.Documents; using System.Windows.Xps.Packaging; using System.Windows.Media.Imaging; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { string xpsFile = "c:\\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("c:\\xpstobmp" + pageCount + ".bmp", FileMode.Create, FileAccess.Write); bmpEncoder.Save(fStream); fStream.Close(); } } } }