Міжпакетні операції в пакеті XPS

Керуйте сторінками, кольорами та гліфами в пакеті XPS за допомогою C++

 

API Aspose.Page для C++ пропонує окрему бібліотеку для роботи з файлами XPS, що дозволяє розглядати їх як окремий формат. Ця бібліотека має різноманітні корисні функції, включаючи об’єднання документів XPS, конвертацію їх в інші формати та маніпулювання графікою всередині них.

Одним із ключових аспектів файлів XPS є їхня здатність містити кілька файлів у межах одного документа. У результаті бібліотека Aspose.Page XPS надає функції для управління цими внутрішніми файлами та їхніми сторінками. Ці операції, відомі як "міжпакетні операції (cross-package operations)", передбачають маніпулювання вмістом у різних документах XPS.

У цьому розділі розглядаються конкретні приклади міжпакетних операцій, як-от керування сторінками в одному документі XPS та додавання тексту (гліфів) певного кольору.

Але щоб спробувати функціональність, спершу потрібно отримати рішення:

  • Відкрийте диспетчер пакетів NuGet, знайдіть Aspose.Page та встановіть його. Ви також можете використати наступну команду в консолі диспетчера пакетів (Package Manager Console).

Кроки для роботи (керування) зі сторінками в пакеті XPS C++.

  1. Установіть шлях до каталогу документів.
  2. Створіть файл XPS за допомогою класу XpsDocument Class .
  3. Щоб вставити активну сторінку з одного документа на початок іншого документа, скористайтеся методом InsertPage() .
  4. Щоб вставити активную сторінку з одного документа в кінець іншого документа, скористайтеся методом AddPage() .
  5. Щоб видалити порожню сторінку, використовуйте метод RemovePage() .
  6. Щоб видалити (перемістити) сторінку з одного документа в інший документ, використовуйте методи InsertPage() і SelectActivePage() .
  7. Збережіть змінені документи XPS за допомогою методу XPsDocument.Save .
Маніпулювання сторінками
    // 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");

Кроки для додавання клону гліфа в пакет XPS C++.

  1. Установіть шлях до каталогу документів.
  2. Відкрийте потік (stream) файлу XPS.
  3. Створіть файл XPS за допомогою XpsDocument Class.
  4. Додайте гліфи до документа за допомогою методу AddGlyphs() .
  5. Створіть другий файл XPS за допомогою XpsDocument Class.
  6. Щоб клонувати гліф із першого файлу в другий файл, використовуйте методи Add() і Clone() .
  7. Збережіть обидва документи XPS за допомогою методу XPsDocument.Save().
Додати клон гліфа та змінити колір
    // 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");

Кроки для додавання гліфа, заповненого зображенням, у C++.

  1. Установіть шлях до каталогу документів.
  2. Відкрийте потік файлу XPS.
  3. Створіть файл XPS за допомогою XpsDocument Class.
  4. Додайте гліфи до документа за допомогою методу AddGlyphs().
  5. Щоб заповнити гліфи пензлем зображення (image brush), використовуйте метод CreateImageBrush() .
  6. Створіть другий файл XPS за допомогою XpsDocument Class.
  7. Додайте гліфи зі шрифтом із першого документа до другого документа за допомогою методу AddGlyphs().
  8. Створіть пензель зображення із заливки першого документа та залийте гліфи в другому документі за допомогою методу CreateImageBrush().
  9. Збережіть обидва документи XPS за допомогою методу XPsDocument.Save().
Додати гліф, заповнений зображенням, і стороннє зображення
    // 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) — альтернатива Microsoft до PDF. Заснований на XML/HTML, зберігає макет на різних платформах і не залежить від операційної системи.