在 XPS 中使用画布 (canvases)
使用 C++ API 裁剪 (clip) 和变换 (transform) XPS 文件中的画布
在 XPS 文件中,画布 (canvas) 是页面上的矩形区域,其中可以绘制文本、图像和形状等视觉内容。它充当这些元素的容器,并提供用于组织和分层 (layering) 它们的框架。画布可以在文档的不同部分甚至多个文档中重复使用,从而提供了一种在 XPS 文档中灵活构建和组织内容的方法。XPS 文件中画布的主要特性:
- 画布可以嵌套 (nested) 在其他画布中,从而创建允许复杂布局 (layouts) 和元素分组的层次结构。
- 画布可以使用各种操作进行变换 (transform),例如平移 (translation)、旋转 (rotation) 和缩放 (scaling)。这可以灵活定位内容并调整内容大小。
- 画布可以裁剪 (clip) 到特定区域,限制其内容的可见区域。这对于创建复杂的形状和效果很有用。
- 画布可以包含不同的视觉元素,包括文本运行 (text runs)、段落 (paragraphs) 和文本框 (text boxes)。它们还可以包含图像、几何形状和其他画布。
管理文档中的画布是 Aspose.Page for C++ 提供的功能之一。该解决方案可与不同的页面描述语言配合使用,尤其是 XPS。
要变换 (transform) XPS 文件的画布,请按照以下指南操作:
- 使用 XpsDocument 类创建 XPS 文件。
- 使用 AddCanvas() 方法创建所有页面元素通用的主画布。
- 使用 CreateMatrix() 方法在主画布中创建左偏移和上偏移。
- 使用 CreatePathGeometry() 方法创建矩形路径几何图形 (rectangle path geometry)。
- 使用 XpsBrush 类为矩形创建填充 (fill)。
- 要在画布 2 中创建矩形并填充它,请使用 XpsPath 类。
- 要平移画布 3 以将新矩形定位在前一个矩形下方,请使用 CreateMatrix() 方法。
- 要将此画布平移到页面右侧,请使用 Translate() 方法。
- 要缩放 (scale) 画布 4,请调用 Scale() 方法。
- 要将画布 5 围绕某点旋转 (rotate) 45 度;RotateAround() 方法非常有用。
- 使用 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");要裁剪 (clip) XPS 文件的画布,请按照以下指南操作:
- 使用 XpsDocument 类创建或打开 XPS 文件。
- 使用 AddCanvas() 方法创建所有页面元素通用的主画布。
- 使用 CreateMatrix() 方法在主画布中创建左偏移和上偏移。
- 使用 CreatePathGeometry() 方法创建矩形路径几何图形。
- 使用 XpsBrush 类为矩形创建填充。
- 要向主画布添加带有剪辑 (clip) 的另一个画布,请再次调用 AddCanvas() 方法。
- 使用 XpsPathGeometry 类为剪辑创建圆形几何图形。
- 要在此画布中创建矩形并填充,请使用 XpsPath 类。
- 使用 AddCanvas() 方法添加另一个画布,然后在此画布中创建一个矩形,并使用 XpsPathGeometry 类描绘其轮廓 (stroke)。
- 使用 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 What is XPS File Format
XPS(XML Paper Specification)是 Microsoft 的 PDF 替代方案,基于 XML/HTML,跨平台保持布局一致,且不依赖操作系统。