Trabalhar com telas (canvases) dentro do XPS

Recortar (clip) e transformar (transform) telas em arquivos XPS usando a API C++

 

Nos arquivos XPS, uma tela (canvas) é uma área retangular em uma página onde conteúdo visual, como texto, imagens e formas, pode ser desenhado. Ela funciona como um contêiner para esses elementos e fornece uma estrutura para organizá-los e agrupá-los em camadas (layering). As telas podem ser reutilizadas em diferentes partes do documento ou até mesmo em vários documentos, o que oferece uma forma flexível de estruturar e organizar o conteúdo em um documento XPS. Principais características das telas nos arquivos XPS:

  • As telas podem ser aninhadas (nested) dentro de outras telas, criando uma estrutura hierárquica que permite layouts (layouts) complexos e agrupamento de elementos.
  • As telas podem ser transformadas usando várias operações, como translação (translation), rotação (rotation) e dimensionamento (scaling). Isso permite o posicionamento flexível e o redimensionamento do conteúdo.
  • As telas podem ser recortadas (clip) em regiões específicas, limitando a área visível de seu conteúdo. Isso é útil para criar formas e efeitos intrincados.
  • As telas podem conter diferentes elementos visuais, incluindo execuções de texto (text runs), parágrafos e caixas de texto (text boxes). Elas também podem conter imagens, formas geométricas e outras telas.

Gerenciar telas em documentos é um dos recursos oferecidos pelo Aspose.Page para C++. Esta solução funciona com diferentes linguagens de descrição de página, XPS em particular.

Para transformar (transform) as telas de um arquivo XPS, siga o guia abaixo:

  1. Crie um arquivo XPS usando a classe XpsDocument.
  2. Crie a tela principal, comum a todos os elementos da página, usando o método AddCanvas().
  3. Defina deslocamentos à esquerda e ao topo na tela principal usando o método CreateMatrix().
  4. Crie a geometria de caminho retangular (rectangle path geometry) com o método CreatePathGeometry().
  5. Crie um preenchimento para os retângulos usando a classe XpsBrush.
  6. Para criar um retângulo na tela 2 e preenchê-lo, use a classe XpsPath.
  7. Para mover a tela 3 a fim de posicionar um novo retângulo abaixo do retângulo anterior, use o método CreateMatrix().
  8. Para mover esta tela para o lado direito da página, use o método Translate().
  9. Para dimensionar (scale) a tela 4, chame o método Scale().
  10. Para girar (rotate) a tela 5 em torno de um ponto em 45 graus; o método RotateAround() é muito útil.
  11. Salve o documento XPS alterado usando o método XpsDocument.Save().
A transformação da tela (canvas transformation)
    // The path to the documents directory.
    System::String dataDir = RunExamples::GetDataDir_WorkingWithCanvas();
    
    // Create new XPS Document
    System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
    
    // Create main canvas, common for all page elemnts
    System::SharedPtr<XpsCanvas> canvas1 = doc->AddCanvas();
    
    // Make left and top offsets in the main canvas
    canvas1->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 20.0f, 10.0f));
    
    // Create rectangle path geometry
    System::SharedPtr<XpsPathGeometry> rectGeom = doc->CreatePathGeometry(u"M 0,0 L 200,0 200,100 0,100 Z");
    
    // Create a fill for rectangles
    System::SharedPtr<XpsBrush> fill = doc->CreateSolidColorBrush(doc->CreateColor(12, 15, 159));
    
    // Add new canvas without any transformations to the main canvas
    System::SharedPtr<XpsCanvas> canvas2 = canvas1->AddCanvas();
    // Create rectangle in this canvas and fill it
    System::SharedPtr<XpsPath> rect = canvas2->AddPath(rectGeom);
    rect->set_Fill(fill);
    
    // Add new canvas with translate transformation to the main canvas
    System::SharedPtr<XpsCanvas> canvas3 = canvas1->AddCanvas();
    // Translate this canvas to position new rectangle below previous rectnagle
    canvas3->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 200.0f));
    // Translate this canvas to right side of page
    canvas3->get_RenderTransform()->Translate(500.0f, 0.0f);
    // Create rectangle in this canvas and fill it
    rect = canvas3->AddPath(rectGeom);
    rect->set_Fill(fill);
    
    // Add new canvas with double scale transformation to the main canvas
    System::SharedPtr<XpsCanvas> canvas4 = canvas1->AddCanvas();
    // Translate this canvas to position new rectangle below previous rectnagle
    canvas4->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 400.0f));
    // Scale this canvas
    canvas4->get_RenderTransform()->Scale(2.0f, 2.0f);
    // Create rectangle in this canvas and fill it
    rect = canvas4->AddPath(rectGeom);
    rect->set_Fill(fill);
    
    // Add new canvas with rotation around a point transformation to the main canvas
    System::SharedPtr<XpsCanvas> canvas5 = canvas1->AddCanvas();
    // Translate this canvas to position new rectangle below previous rectnagle
    canvas5->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 800.0f));
    // Rotate this canvas aroud a point on 45 degrees
    
    canvas5->get_RenderTransform()->RotateAround(45.0f, System::Drawing::PointF(100.0f, 50.0f));
    rect = canvas5->AddPath(rectGeom);
    rect->set_Fill(fill);
    
    // Save resultant XPS document
    doc->Save(dataDir + u"output/" + u"output1.xps");
O trecho de código a seguir mostra como recortar telas de arquivos XPS dentro da solução API Aspose.Page para C++.

Para recortar (clip) as telas de um arquivo XPS, siga o guia abaixo:

  1. Crie ou abra um arquivo XPS usando a classe XpsDocument.
  2. Crie a tela principal, comum a todos os elementos da página, usando o método AddCanvas().
  3. Defina deslocamentos à esquerda e ao topo na tela principal usando o método CreateMatrix().
  4. Crie a geometria de caminho retangular com o método CreatePathGeometry().
  5. Crie um preenchimento para os retângulos usando a classe XpsBrush.
  6. Para adicionar outra tela com recorte (clip) à tela principal, chame o método AddCanvas() novamente.
  7. Crie a geometria do círculo para o recorte usando a classe XpsPathGeometry.
  8. Para criar um retângulo nesta tela e preenchê-lo, use a classe XpsPath.
  9. Adicione outra tela com o método AddCanvas(), crie um retângulo nesta tela e aplique o traçado (stroke) com a classe XpsPathGeometry.
  10. Salve o documento XPS alterado usando o método XpsDocument.Save().
O recorte da tela (canvas clipping)
    // The path to the documents directory.
    System::String dataDir = RunExamples::GetDataDir_WorkingWithCanvas();
    
    // Create new XPS Document
    System::SharedPtr<XpsDocument> doc = System::MakeObject<XpsDocument>();
    
    // Create main canvas, common for all page elemnts
    System::SharedPtr<XpsCanvas> canvas1 = doc->AddCanvas();
    
    // Make left and top offsets in the main canvas
    canvas1->set_RenderTransform(doc->CreateMatrix(1.0f, 0.0f, 0.0f, 1.0f, 20.0f, 10.0f));
    
    // Create rectangle path geometry
    System::SharedPtr<XpsPathGeometry> rectGeom = doc->CreatePathGeometry(u"M 0,0 L 500,0 500,300 0,300 Z");
    
    // Create a fill for rectangles
    System::SharedPtr<XpsBrush> fill = doc->CreateSolidColorBrush(doc->CreateColor(12, 15, 159));
    
    // Add another canvas with clip to the main canvas
    System::SharedPtr<XpsCanvas> canvas2 = canvas1->AddCanvas();
    
    // Create circle geometry for clip
    System::SharedPtr<XpsPathGeometry> clipGeom = doc->CreatePathGeometry(u"M250,250 A100,100 0 1 1 250,50 100,100 0 1 1 250,250");
    canvas2->set_Clip(clipGeom);
    
    // Create rectangle in this canvas and fill it
    System::SharedPtr<XpsPath> rect = canvas2->AddPath(rectGeom);
    rect->set_Fill(fill);
    
    // Add the second canvas with stroked rectangle to the main canvas
    System::SharedPtr<XpsCanvas> canvas3 = canvas1->AddCanvas();
    
    // Create rectangle in this canvas and stroke it
    rect = canvas3->AddPath(rectGeom);
    rect->set_Stroke(fill);
    rect->set_StrokeThickness(2.0f);
    
    // Save resultant XPS document
    doc->Save(dataDir + u"output/" + u"output2.xps");

XPS O que é XPS Formato de Arquivo

XPS (XML Paper Specification) é a alternativa da Microsoft ao PDF. Baseado em XML/HTML, preserva o layout em diferentes plataformas e é independente do sistema operacional.