Cross-package operations within XPS Package

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

 

Aspose.Page API for C++ offers a separate library for working with XPS files, allowing you to treat them as a distinct format. This library boasts a variety of useful features, including merging XPS documents, converting them to other formats, and manipulating graphics within them.

One key aspect of XPS files is their ability to contain multiple files within a single document. As a result, the Aspose.Page XPS library provides functionalities for managing these internal files and their pages. These operations, known as "cross-package operations," involve manipulating content across different XPS documents.

This section will delve into specific examples of cross-package operations, such as managing pages within a single XPS document and adding text (glyphs) with specific colors.

But to try the functionality you first need to get the solution:

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

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 Method.
Manipulate Pages
    // The path to the documents directory.
    System::String dataDir = RunExamples::GetDataDir_WorkingWithCrossPackageOperations();
    
    // Create the first XPS Document
    System::SharedPtr<XpsDocument> doc1 = System::MakeObject<XpsDocument>(dataDir + u"input1.xps");
    
    // Create the second XPS Document
    System::SharedPtr<XpsDocument> doc2 = System::MakeObject<XpsDocument>(dataDir + u"input2.xps");
    
    // Create the third XPS Document
    System::SharedPtr<XpsDocument> doc3 = System::MakeObject<XpsDocument>(dataDir + u"input3.xps");
    
    // Create the fourth XPS Document
    System::SharedPtr<XpsDocument> doc4 = System::MakeObject<XpsDocument>();
    
    // Insert active page (1 in this case) from the second document to the beginning of the fourth document
    doc4->InsertPage(1, doc2->get_Page(), false);
    
    // Insert active page (1 in this case) from the third document to the end of the fourth document
    doc4->AddPage(doc3->get_Page(), false);
    
    // Remove page 2 from the fourth document. This is an empty page that was created when document has 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 + u"output/" + u"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.
Add Glyph Clone And Change Color
    // The path to the documents directory.
    System::String dataDir = RunExamples::GetDataDir_WorkingWithCrossPackageOperations();
    
    // Create the first XPS Document
    System::SharedPtr<XpsDocument> doc1 = System::MakeObject<XpsDocument>();
    
    // Add glyphs to the first document
    System::SharedPtr<XpsGlyphs> glyphs = doc1->AddGlyphs(u"Times New Roman", 200.0f, System::Drawing::FontStyle::Bold, 50.0f, 250.0f, u"Test");
    
    // Fill glyphs in the first document with one color
    glyphs->set_Fill(doc1->CreateSolidColorBrush(System::Drawing::Color::get_Green()));
    
    // Create the second XPS Document
    System::SharedPtr<XpsDocument> doc2 = System::MakeObject<XpsDocument>();
    
    // Add glyphs cloned from the one's from the first document
    glyphs = doc2->Add<System::SharedPtr<XpsGlyphs>>(glyphs->Clone());
    
    // Fill glyphs in the second document with another color
    (System::ExplicitCast<Aspose::Page::XPS::XpsModel::XpsSolidColorBrush>(glyphs->get_Fill()))->set_Color(doc2->CreateColor(System::Drawing::Color::get_Red()));
    
    // Save the first XPS document
    doc1->Save(dataDir + u"output/" + u"out1.xps");
    
    // Save the second XPS document
    doc2->Save(dataDir + u"output/" + u"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.
Add Image Filled Glyph And Foreign Image
    // The path to the documents directory.
    System::String dataDir = RunExamples::GetDataDir_WorkingWithCrossPackageOperations();
    
    // Create the first XPS Document
    System::SharedPtr<XpsDocument> doc1 = System::MakeObject<XpsDocument>();
    
    // Add glyphs to the first document
    System::SharedPtr<XpsGlyphs> glyphs1 = doc1->AddGlyphs(u"Times New Roman", 200.0f, System::Drawing::FontStyle::Bold, 50.0f, 250.0f, u"Test");
    
    // Fill the glyphs with an image brush
    glyphs1->set_Fill(doc1->CreateImageBrush(dataDir + u"R08SY_NN.tif", System::Drawing::RectangleF(0.f, 0.f, 128.f, 192.f), System::Drawing::RectangleF(0.f, 0.f, 64.f, 96.f)));
    (System::ExplicitCast<Aspose::Page::XPS::XpsModel::XpsImageBrush>(glyphs1->get_Fill()))->set_TileMode(Aspose::Page::XPS::XpsModel::XpsTileMode::Tile);
    
    // Create the second XPS Document
    System::SharedPtr<XpsDocument> doc2 = System::MakeObject<XpsDocument>();
    
    // Add glyphs with the font from the first document to the second document
    System::SharedPtr<XpsGlyphs> glyphs2 = doc2->AddGlyphs(glyphs1->get_Font(), 200.0f, 50.0f, 250.0f, u"Test");
    
    // Create an image brush from the fill of the the first document and fill glyphs in the second document
    glyphs2->set_Fill(doc2->CreateImageBrush((System::ExplicitCast<Aspose::Page::XPS::XpsModel::XpsImageBrush>(glyphs1->get_Fill()))->get_Image(), System::Drawing::RectangleF(0.f, 0.f, 128.f, 192.f), System::Drawing::RectangleF(0.f, 0.f, 128.f, 192.f)));
    (System::ExplicitCast<Aspose::Page::XPS::XpsModel::XpsImageBrush>(glyphs2->get_Fill()))->set_TileMode(Aspose::Page::XPS::XpsModel::XpsTileMode::Tile);
    
    // Save the first XPS document
    doc1->Save(dataDir + u"output/" + u"out1.xps");
    
    // Save the second XPS document
    doc2->Save(dataDir + u"output/" + u"out2.xps");

XPS What is XPS File Format

XPS (XML Paper Specification) is Microsoft’s open‑source alternative to PDF. It uses XML/HTML markup to describe page layout, fonts, and images, ensuring consistent rendering across Windows platforms and other operating systems.