Operaciones entre paquetes dentro del paquete XPS

Manipule páginas, colores y glifos dentro del paquete XPS mediante C++

 

La API de Aspose.Page para C++ ofrece una biblioteca independiente para trabajar con archivos XPS, lo que le permite tratarlos como un formato distinto. Esta biblioteca cuenta con una variedad de características útiles, que incluyen fusionar documentos XPS, convertirlos a otros formatos y manipular gráficos en su interior.

Un aspecto clave de los archivos XPS es su capacidad para contener varios archivos dentro de un solo documento. Como resultado, la biblioteca Aspose.Page XPS proporciona funcionalidades para administrar estos archivos internos y sus páginas. Estas operaciones, conocidas como "operaciones entre paquetes (cross-package operations)", implican la manipulación de contenido en diferentes documentos XPS.

Esta sección profundizará en ejemplos específicos de operaciones entre paquetes, como administrar páginas dentro de un solo documento XPS y agregar texto (glifos) con colores específicos.

Pero para probar la funcionalidad primero necesita obtener la solución:

  • Abra el administrador de paquetes NuGet, busque Aspose.Page e instálelo. También puede utilizar el siguiente comando desde la Consola del Administrador de Paquetes (Package Manager Console).

Pasos para manipular páginas dentro del paquete XPS C++.

  1. Establezca la ruta al directorio de documentos.
  2. Cree un archivo XPS utilizando la Clase XpsDocument .
  3. Para insertar una página activa de un documento al principio de otro documento, utilice el método InsertPage() .
  4. Para insertar una página activa de un documento al final de otro documento, utilice el método AddPage() .
  5. Para eliminar una página vacía, utilice el método RemovePage() .
  6. Para eliminar una página de un documento a otro documento, utilice los métodos InsertPage() y SelectActivePage() .
  7. Guarde los documentos XPS modificados utilizando el método XPsDocument.Save .
Manipular páginas
    // 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");

Pasos para agregar un clon de glifo dentro del paquete XPS C++.

  1. Establezca la ruta al directorio de documentos.
  2. Abra un flujo del archivo XPS.
  3. Cree un archivo XPS utilizando la Clase XpsDocument.
  4. Agregue glifos al documento utilizando el método AddGlyphs() .
  5. Cree el segundo archivo XPS utilizando la Clase XpsDocument.
  6. Para clonar el glifo del primer archivo al segundo archivo, utilice los métodos Add() y Clone() .
  7. Guarde ambos documentos XPS mediante el método XPsDocument.Save().
Agregar clon de glifo y cambiar de 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");

Pasos para agregar un glifo relleno de imagen en C++.

  1. Establezca la ruta al directorio de documentos.
  2. Abra un flujo del archivo XPS.
  3. Cree un archivo XPS utilizando la Clase XpsDocument.
  4. Agregue glifos al documento utilizando el método AddGlyphs().
  5. Para rellenar los glifos con un pincel de imagen (image brush), utilice el método CreateImageBrush() .
  6. Cree el segundo archivo XPS utilizando la Clase XpsDocument.
  7. Agregue glifos con la fuente del primer documento al segundo documento utilizando el método AddGlyphs().
  8. Cree un pincel de imagen a partir del relleno del primer documento y rellene los glifos en el segundo documento utilizando el método CreateImageBrush().
  9. Guarde ambos documentos XPS mediante el método XPsDocument.Save().
Agregar glifo relleno de imagen e imagen externa
    // 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 Qué es XPS Formato de archivo

XPS (XML Paper Specification) es el equivalente de Microsoft a PDF. Se basa en XML/HTML, mantiene el diseño en distintas plataformas y es independiente del sistema operativo.