XPS内のキャンバス(canvases)操作

C++ APIを使用してXPSファイル内のキャンバスをクリップ(clip)および変換(transform)する

 

XPSファイルにおいて、キャンバス(canvas)はページ上の矩形の領域であり、テキスト、画像、図形などの視覚的なコンテンツを描画することができます。これらの要素のコンテナとして機能し、それらを整理および重ね合わせるためのフレームワークを提供します。キャンバスはドキュメントのさまざまな部分、あるいは複数のドキュメントで再利用でき、XPSドキュメント内のコンテンツを構造化・整理するための柔軟な方法を提供します。XPSファイルのキャンバスの主な特徴:

  • キャンバスは他のキャンバス内にネスト(nested)でき、複雑なレイアウト(layouts)や要素のグループ化(grouping)を可能にする階層構造を作成します。
  • キャンバスは、移動(translation)、回転(rotation)、拡大縮小(scaling)などのさまざまな操作を使用して変換できます。これにより、コンテンツの柔軟な配置やサイズ変更が可能になります。
  • キャンバスは特定の領域にクリップ(clip)でき、コンテンツの表示領域を制限します。これは複雑な形状や効果を作成するのに役立ちます。
  • キャンバスには、テキスト実行(text runs)、段落(paragraphs)、テキストボックス(text boxes)など、さまざまな視覚要素を含めることができます。画像、幾何学的な形状、その他のキャンバスを含めることもできます。

ドキュメント内のキャンバスを管理する機能は、Aspose.Page for C++が提供する機能の1つです。このソリューションは、さまざまなページ記述言語、特にXPSと連動します。

XPSファイルのキャンバスを変換(transform)するには、以下のガイドをご確認ください:

  1. XpsDocumentクラスを使用してXPSファイルを作成します。
  2. *AddCanvas()*メソッドを使用して、すべてのページ要素に共通するメインキャンバスを作成します。
  3. *CreateMatrix()*メソッドを使用して、メインキャンバスで左と上のオフセットを作成します。
  4. *CreatePathGeometry()*メソッドで四角形のパスジオメトリ(rectangle path geometry)を作成します。
  5. XpsBrushクラスを使用して四角形の塗りつぶし(fill)を作成します。
  6. キャンバス2に四角形を作成して塗りつぶすには、XpsPathクラスを使用します。
  7. キャンバス3を移動させて前の四角形の下に新しい四角形を配置するには、*CreateMatrix()*メソッドを使用します。
  8. このキャンバスをページの右側に移動するには、*Translate()*メソッドを使用します。
  9. キャンバス4を拡大縮小(scale)するには、*Scale()*メソッドを呼び出します。
  10. キャンバス5をある点を中心に45度回転させるには、*RotateAround()*メソッドが便利です。
  11. *XpsDocument.Save()*メソッドを使用して変更したXPSドキュメントを保存します。
キャンバスの変換(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");
次のコードスニペットは、Aspose.Page for C++ APIソリューション内でXPSファイルのキャンバスをクリップする方法を示しています。

XPSファイルのキャンバスをクリップ(clip)するには、以下のガイドをご確認ください:

  1. XpsDocumentクラスを使用してXPSファイルを作成または開きます。
  2. *AddCanvas()*メソッドを使用して、すべてのページ要素に共通するメインキャンバスを作成します。
  3. *CreateMatrix()*メソッドを使用して、メインキャンバスで左と上のオフセットを作成します。
  4. *CreatePathGeometry()*メソッドで四角形のパスジオメトリを作成します。
  5. XpsBrushクラスを使用して四角形の塗りつぶしを作成します。
  6. メインキャンバスにクリップ付きの別のキャンバスを追加するには、*AddCanvas()*メソッドを再度呼び出します。
  7. XpsPathGeometryクラスを使用して、クリップ用の円ジオメトリを作成します。
  8. このキャンバスに四角形を作成して塗りつぶすには、XpsPathクラスを使用します。
  9. AddCanvas()メソッドで別のキャンバスを追加し、そのキャンバスに四角形を作成してXpsPathGeometryクラスでストローク(stroke)を施します。
  10. *XpsDocument.Save()*メソッドを使用して変更したXPSドキュメントを保存します。
キャンバスのクリップ(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 XPS ファイル形式とは

XPS (XML Paper Specification) は、Microsoft が提供する PDF の代替フォーマットです。XML/HTML ベースで、プラットフォームに依存せずレイアウトを保持します。