使用画布
剪辑和转换 XPS 文件的画布
管理文档中的画布是 Aspose.Page for .NET 提供的功能之一。这是一个使用不同页面描述语言的解决方案,特别是 XPS XPS。在下面的示例中,您将了解如何:
在 XPS 文件中创建画布。
在主画布中进行左侧和顶部偏移。
将带有转换转换的新画布添加到主画布。
向主画布添加一个围绕点变换旋转的新画布。
剪辑画布。在 XPS 文件中使用画布进行更多操作。
要转换 XPS 文件的画布,请遵循以下指南:
- 使用 XpsDocument 类 创建一个 XPS 文件。
- 使用 AddCanvas() 方法创建所有页面元素通用的主画布。
- 使用 CreateMatrix() 方法在主画布中进行左侧和顶部偏移。
- 使用 CreatePathGeometry() 方法创建矩形路径几何。
- 通过 XpsBrush 类为矩形创建填充。
- 要在画布 2 中创建一个矩形并填充它,请使用 XpsPath 类。
- 要将画布 3 转换为在前一个矩形下方放置一个新矩形,请使用 CreateMatrix() 方法。
- 要将此画布翻译到页面右侧,请使用 Translate() 方法。
- 要缩放画布 4,请调用 Scale() 方法。
- 要围绕 45 度的点旋转画布 5,请使用 RotateAround() 方法派上用场。
- 使用 XPsDocument.Save() 方法保存更改后的 XPS 文档。
XPS 文件画布转换 C# 代码
using Aspose.Page.Xps;
using Aspose.Page.Xps.XpsModel;
using System.Drawing; // The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCanvas();
// Create a new XPS Document
XpsDocument doc = new XpsDocument();
// Create main canvas, common for all page elemnts
XpsCanvas canvas1 = doc.AddCanvas();
// Make left and top offsets in the main canvas
canvas1.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 20, 10);
// Create the rectangle path geometry
XpsPathGeometry rectGeom = doc.CreatePathGeometry("M 0,0 L 200,0 200,100 0,100 Z");
// Create a fill for rectangles
XpsBrush fill = doc.CreateSolidColorBrush(doc.CreateColor(12, 15, 159));
// Add new canvas without any transformations to the main canvas
XpsCanvas canvas2 = canvas1.AddCanvas();
// Create rectangle in this canvas and fill it
XpsPath rect = canvas2.AddPath(rectGeom);
rect.Fill = fill;
// Add new canvas with translate transformation to the main canvas
XpsCanvas canvas3 = canvas1.AddCanvas();
// Translate this canvas to position new rectangle below the previous rectnagle
canvas3.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 0, 200);
// Translate this canvas to the right side of the page
canvas3.RenderTransform.Translate(500, 0);
// Create the rectangle in this canvas and fill it
rect = canvas3.AddPath(rectGeom);
rect.Fill = fill;
// Add new canvas with the double scale transformation to the main canvas
XpsCanvas canvas4 = canvas1.AddCanvas();
// Translate this canvas to position new rectangle below the previous rectnagle
canvas4.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 0, 400);
// Scale this canvas
canvas4.RenderTransform.Scale(2, 2);
// Create a rectangle in this canvas and fill it
rect = canvas4.AddPath(rectGeom);
rect.Fill = fill;
// Add new canvas with rotation around a point transformation to the main canvas
XpsCanvas canvas5 = canvas1.AddCanvas();
// Translate this canvas to position new rectangle below the previous rectnagle
canvas5.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 0, 800);
// Rotate this canvas aroud a point on 45 degrees
canvas5.RenderTransform.RotateAround(45, new PointF(100, 50));
rect = canvas5.AddPath(rectGeom);
rect.Fill = fill;
// Save the resultant XPS document
doc.Save(dataDir + "output1.xps");要剪辑 XPS 文件的画布,请遵循以下指南:
- 使用 XpsDocument 类 创建或打开 XPS 文件。
- 使用 AddCanvas() 方法创建所有页面元素通用的主画布。
- 使用 CreateMatrix() 方法在主画布中进行左侧和顶部偏移。
- 使用 CreatePathGeometry() 方法创建矩形路径几何。
- 通过 XpsBrush 类为矩形创建填充。
- 要将带有剪辑的另一个画布添加到主画布,请再次调用 AddCanvas() 方法。
- 使用 XpsPathGeometry 类为剪辑创建圆形几何图形。
- 要在此画布中创建一个矩形并填充它,请使用 XpsPath 类。
- 使用 AddCanvas() 方法添加另一个画布,然后在此画布中创建一个矩形并使用 XpsPathGeometry 类对其进行描边。
- 通过 XPsDocument.Save() 方法保存更改后的 XPS 文档。
XPS 文件画布裁剪 C# 代码
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithCanvas();
// Create a new XPS Document
XpsDocument doc = new XpsDocument();
// Create main canvas, common for all page elemnts
XpsCanvas canvas1 = doc.AddCanvas();
// Make left and top offsets in the main canvas
canvas1.RenderTransform = doc.CreateMatrix(1, 0, 0, 1, 20, 10);
// Create the rectangle path geometry
XpsPathGeometry rectGeom = doc.CreatePathGeometry("M 0,0 L 500,0 500,300 0,300 Z");
// Create a fill for rectangles
XpsBrush fill = doc.CreateSolidColorBrush(doc.CreateColor(12, 15, 159));
// Add another canvas with the clip to the main canvas
XpsCanvas canvas2 = canvas1.AddCanvas();
// Create circle geometry for the clip
XpsPathGeometry clipGeom = doc.CreatePathGeometry("M250,250 A100,100 0 1 1 250,50 100,100 0 1 1 250,250");
canvas2.Clip = clipGeom;
// Create rectangle in this canvas and fill it
XpsPath rect = canvas2.AddPath(rectGeom);
rect.Fill = fill;
// Add the second canvas with stroked rectangle to the main canvas
XpsCanvas canvas3 = canvas1.AddCanvas();
// Create rectangle in this canvas and stroke it
rect = canvas3.AddPath(rectGeom);
rect.Stroke = fill;
rect.StrokeThickness = 2;
// Save resultant XPS document
doc.Save(dataDir + "output2.xps");常问问题
1. 如何将画布添加到 XPS 文件中?
设置文档目录的路径。要添加画布,请使用 AddCanvas() 方法。
2. 如何操作 XPS 文件中的画布偏移?
设置文档目录的路径并添加画布。使用 CreateMatrix() 方法进行偏移。
3. 支持对 XPS 文件中的画布进行哪些操作?
您可以创建、剪辑和添加画布以及操纵其偏移。
XPS 什么是XPS文件格式
XPS(XML Paper Specification)是 Microsoft 的 PDF 替代方案,基于 XML/HTML,跨平台保持布局一致,且不依赖操作系统。