XPS 内でキャンバスを操作する

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

 

XPS ファイルでは、キャンバスは、テキスト、画像、図形などのビジュアル コンテンツを描画できるページ上の長方形の領域です。これはこれらの要素のコンテナとして機能し、要素を整理して階層化するためのフレームワークを提供します。   キャンバスはドキュメントのさまざまな部分で、または複数のドキュメントでさえ再利用でき、XPS ドキュメント内のコンテンツを構造化および整理する柔軟な方法を提供します。 XPS ファイルのキャンバスの主な特徴:

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

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

XPS ファイルのキャンバスを変換するには、次のガイドに従ってください。

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

XPS ファイルのキャンバスをクリップするには、次のガイドに従ってください。

  1. XpsDocument クラス を使用して XPS ファイルを作成または開きます。
  2. AddCanvas() メソッドを使用して、すべてのページ要素に共通のメイン キャンバスを作成します。
  3. CreateMatrix() メソッドを使用して、メイン キャンバスの左と上のオフセットを作成します。
  4. CreatePathGeometry() メソッドを使用して、長方形のパス ジオメトリを作成します。
  5. XpsBrush クラスを使用して、長方形の塗りつぶしを作成します。
  6. クリップを含む別のキャンバスをメイン キャンバスに追加するには、AddCanvas() メソッドを再度呼び出します。
  7. XpsPathGeometry クラスを使用して、クリップの円ジオメトリを作成します。
  8. このキャンバスに四角形を作成して塗りつぶすには、 XpsPath クラスを使用します。
  9. AddCanvas() メソッドを使用して別のキャンバスを追加し、このキャンバスに四角形を作成し、XpsPathGeometry クラスでストロークします。
  10. XPsDocument.Save() メソッドを使用して、変更された XPS ドキュメントを保存します。
キャンバスのクリッピング
// 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 形式は PDF 形式に似ています。どちらもページ記述言語 (PDL) 形式です。 EPS は PostScript 言語ではなく、HTML に基づいています。 .eps ファイルには、ドキュメントの構造のマークアップと、ドキュメントがどのように見えるかに関する情報を含めることができます。また、ドキュメントを印刷およびレンダリングする方法についての説明も追加されています。この形式の特徴は、ドキュメントの説明を修正することです。つまり、誰が、どのオペレーティング システムからドキュメントを開いたとしても、同じように表示されます。