Create and modify XPS files

C# .NET API solution to create and change XPS files.

 

Aspose.Page for .NET API solution lets you manipulate XPS files along with other Page Description Language format files. The rich functionality allows adding different shapes to documents, merging a few files into a single one, or conversion of them to a better format. Here will be described how to create new XPS files and modify(add a signature to) the already existing ones. The code snippets are added for better understandability.

To manipulate XPS files, we need:

  • Aspose.Page for .NET API which is a feature-rich, powerful and easy-to-use document manipulation and conversion API for the C# platform.

  • Open the NuGet package manager, and search for Aspose.Page and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


    PM> Install-Package Aspose.Page

Steps to create an XPS file with C# .NET.

  1. Set the path to the documents directory.
  2. Create an XPS file using the XpsDocument Class .
  3. If needed, add glyphs to the document using the AddGlyphs() Method.
  4. Save the changed XPS document using the XPsDocument.Save() Method.

C# Code to make an XPS file

    using Aspose.Page.XPS;
    using Aspose.Page.XPS.XpsModel;
    using System.Drawing;
    // The path to the documents directory.
    string dir = RunExamples.GetDataDir_WorkingWithDocument();
    // Create a new XPS Document
    XpsDocument xDocs = new XpsDocument();

    // Add the glyph to the document
    var glyphs = xDocs.AddGlyphs("Arial", 12, FontStyle.Regular, 300f, 450f, "Hello World!");

    glyphs.Fill = xDocs.CreateSolidColorBrush(Color.Black);

    // Save the result
    xDocs.Save(dir + "output.xps");
The next code snippet shows how to modify XPS files within the Aspose.Page for .NET Api Solution.

Steps to modify an XPS file with C# .NET.

  1. Set the path to the documents directory.
  2. Open a stream of the XPS file.
  3. Create an XPS file using the XpsDocument Class.
  4. To create a fill of the signature text use the CreateSolidColorBrush() Method.
  5. To define pages where the signature will be set, use the PageNumbers Property.
  6. Adjust the signature with the SelectActivePage and AddGlyphs() Methods.
  7. Save the changed XPS document by means of the XPsDocument.Save() Method.

C# Code to edit an XPS file

    // The path to the documents directory.
    string dir = RunExamples.GetDataDir_WorkingWithDocument();
    // Open a stream of XPS file
    using (FileStream xpsStream = File.Open(dir + "input1.xps", FileMode.Open, FileAccess.Read))
    {
        // Create an XPS document from stream
        XpsDocument document = new XpsDocument(xpsStream, new XpsLoadOptions());

        // Create the fill of the signature text
        XpsSolidColorBrush textFill = document.CreateSolidColorBrush(Color.BlueViolet);

        // Define pages where the signature will be set
        int[] pageNumbers = new int[] {1, 2, 3};

        // For every defined page set signature "Confirmed" at coordinates x=650 and y=950
        for (int i = 0; i < pageNumbers.Length; i++)
        {
            // Define an active page
            document.SelectActivePage(pageNumbers[i]);

            // Create a glyphs object
            XpsGlyphs glyphs = document.AddGlyphs("Arial", 24, FontStyle.Bold, 650, 900, "Confirmed");

            // Define the fill for glyphs
            glyphs.Fill = textFill;
        }

        // Save the changed XPS document
        document.Save(dir + "input1_out.xps");
    }



FAQ

1. How can I edit an XPS file?

To modify XPS files with this API Solution first set the path to the file and then use the entities of the XpsDocument Class to implement the changes.

2. How to create an XPS file?

To make a new XPS file using Aspose.Page you need to set the path to the document and then use the XpsDocument constructor of the XpsDocument Class.

3. How do I convert XPS to DOCX?

To get DOCX file from an XPS one, use our free cross-platform Converter

XPS What is XPS File Format

XPS format is similar to PDF format. Both are page description language (PDL) formats. EPS is based on HTML and not on PostScript language. The .eps file is capable to contain a markup of the document's structure along with the information on how the document would look like. There are also added instructions on how to print and render the document. The feature of the format is that it fixes the document's description which means that it will look the same no matter who and from what operational system opens it.