Cross-package operations within XPS Package

Manipulate pages, colors and glyphs within XPS Package via C#

 

Aspose.Page API Solution for .NET among other formats includes the XPS Package as a separate library to work with XPS files. Its rich functionality contains many useful and popular features like merging files, conversion, working with graphics, etc.

XPS can hold in one document several files. So any XPS Package should have the functionality to manipulate those files and their pages inside the document and between different XPS documents. Such manipulations are called cross-package operations. They should be explained separately.

Here you will find examples of such cross-package operations as page manipulations, and adding glyphs and colors.

Steps to manipulate pages within XPS Package C#.

  1. Set the path to the documents directory.
  2. Create an XPS file using the XpsDocument Class .
  3. To insert an active page from one document to the beginning of another document use the InsertPage() Method.
  4. To insert an active page from one document to the end of another document use the AddPage() Method.
  5. To remove an empty page use the RemovePage() Method.
  6. To remove a page from one document to another document use the InsertPage() and SelectActivePage() Methods.
  7. Save the changed XPS documents using the XPsDocument.Save .

C# Code for cross-package manipulations with pages

    using Aspose.Page.XPS;
    using Aspose.Page.XPS.XpsModel;
    using System.Drawing;
    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithCrossPackageOperations();

    // Create the first XPS Document
    XpsDocument doc1 = new XpsDocument(dataDir + "input1.xps");

    // Create the second XPS Document
    XpsDocument doc2 = new XpsDocument(dataDir + "input2.xps");

    // Create the third XPS Document
    XpsDocument doc3 = new XpsDocument(dataDir + "input3.xps");

    // Create the fourth XPS Document
    XpsDocument doc4 = new XpsDocument();

    // Insert the active page (1 in this case) from the second document to the beginning of the fourth document
    doc4.InsertPage(1, doc2.Page, false);

    // Insert the active page (1 in this case) from the third document to the end of the fourth document
    doc4.AddPage(doc3.Page, false);

    // Remove page 2 from the fourth document. This is an empty page that was created when the document had been created.
    doc4.RemovePageAt(2);

    // Insert page 3 from the first document to the second postion of the fourth document
    doc4.InsertPage(2, doc1.SelectActivePage(3), false);

    // Save the fourth XPS document
    doc4.Save(dataDir + "out.xps");

Steps to add a glyph clone within XPS Package C#.

  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. Add glyphs to the document using the AddGlyphs() Method.
  5. Create the second XPS file using the XpsDocument Class.
  6. To clone the glyph from the first file to the second file, use the Add() and Clone() Methods.
  7. Save both XPS documents by means of the XPsDocument.Save() Method.

C# Code to copy a glyth within XPS Package

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithCrossPackageOperations();

    // Create the first XPS Document
    XpsDocument doc1 = new XpsDocument();

    // Add glyphs to the first document
    XpsGlyphs glyphs = doc1.AddGlyphs("Times New Roman", 200, FontStyle.Bold, 50, 250, "Test");

    // Fill glyphs in the first document with one color
    glyphs.Fill = doc1.CreateSolidColorBrush(Color.Green);

    // Create the second XPS Document
    XpsDocument doc2 = new XpsDocument();

    // Add glyphs cloned from the one's from the first document
    glyphs = doc2.Add(glyphs.Clone());

    // Fill glyphs in the second document with another color
    ((XpsSolidColorBrush)glyphs.Fill).Color = doc2.CreateColor(Color.Red);

    // Save the first XPS document
    doc1.Save(dataDir + "out1.xps");

    // Save the second XPS document
    doc2.Save(dataDir + "out2.xps");

Steps to add an image-filled Glyph C#.

  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. Add glyphs to the document using the AddGlyphs() Method.
  5. To fill the glyphs with an image brush use the CreateImageBrush() Method.
  6. Create the second XPS file using the XpsDocument Class.
  7. Add glyphs with the font from the first document to the second document using the AddGlyphs() Method.
  8. Create an image brush from the fill of the first document and fill glyphs in the second document using the CreateImageBrush() Method.
  9. Save both XPS documents by means of the XPsDocument.Save() Method

C# Code to create an image-filled Glyph within XPS Package

    // The path to the documents directory.
    string dataDir = RunExamples.GetDataDir_WorkingWithCrossPackageOperations();

    // Create the first XPS Document
    XpsDocument doc1 = new XpsDocument();

    // Add glyphs to the first document
    XpsGlyphs glyphs1 = doc1.AddGlyphs("Times New Roman", 200, FontStyle.Bold, 50, 250, "Test");

    // Fill the glyphs with an image brush
    glyphs1.Fill = doc1.CreateImageBrush(dataDir + "R08SY_NN.tif", new RectangleF(0f, 0f, 128f, 192f),
        new RectangleF(0f, 0f, 64f, 96f));
    ((XpsImageBrush)glyphs1.Fill).TileMode = XpsTileMode.Tile;

    // Create the second XPS Document
    XpsDocument doc2 = new XpsDocument();

    // Add glyphs with the font from the first document to the second document
    XpsGlyphs glyphs2 = doc2.AddGlyphs(glyphs1.Font, 200, 50, 250, "Test");

    // Create an image brush from the fill of the the first document and fill glyphs in the second document
    glyphs2.Fill = doc2.CreateImageBrush(((XpsImageBrush)glyphs1.Fill).Image, new RectangleF(0f, 0f, 128f, 192f),
        new RectangleF(0f, 0f, 128f, 192f));
    ((XpsImageBrush)glyphs2.Fill).TileMode = XpsTileMode.Tile;

    // Save the first XPS document
    doc1.Save(dataDir + "out1.xps");

    // Save the second XPS document
    doc2.Save(dataDir + "out2.xps");



FAQ

1. What is the XPS Package?

XPS Package is a separate library to manage XPS files. Use it to create your own converters, readers or other apps to edit XPS.

2. How can I get an XPS Package?

XPS Package is included in Aspose.Page Solution.

3. What cross-package operations are available?

Using Aspose XPS Package you can transfer pages from one document to another, clone objects like glyphs, styles or settings.

4. How to manipulate pages between XPS documents?

To transfer files with this XPS Package use InsertPage(), AddPage(), RemovePage(), and SelectActivePage() Methods of the XpsDocument Class.

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.