Getsuyodev, Programming is mainly used when constructing an application. Programming requires knowledge of application domains, algorithms, and programming language expertise. Programming can be developed in different logic based on developer knowledge.

How to convert XPS file to Bitmap



XPS stands for XML Paper Specification and is basically an electronic representation of digital documents based on XML. An XpsDocument contains a FixedDocumentSequence that comprises one or more FixedDocument elements. They are also easy to share because you can view them on any computer where an XPS viewer is installed, even if the computer does not have the same programs that you used to create 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.

  • windowsbase.dll
  • ReachFramework.dll
  • PresentationFramework.dll
  • Milk
  • PresentationCore.dll
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();
            }
        }
    }
}

Comments are closed.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More