使用 XPS 图形实用程序 | .NET

如何在 XPS 中轻松创建简单形状

XPS 规范定义了可用于组成任何复杂形状的图形基元元素。Aspose.Page for .NET 提供了封装这些元素的类。然而,当您需要创建即使是相对简单的形状(例如扇形或线段,或内接或外接于圆的正 N 边形)时,使用它们可能会有些繁琐。即使是绘制圆形或椭圆形,也并非像它应该的那样简单。因此,Aspose.Page 额外提供了一组实用方法,这些方法很可能会在执行此类任务时节省您的时间。

在下面的示例中,我们将使用所有可用的形状实用工具。请注意,它们都返回一个 XpsPathGeometry 对象,该对象可用于构建 XPS 路径。并且,您可以为这些路径指定外观属性,例如画笔、描边图案、不透明度等。

 1// Using shape utilities for adding shapes to XPS document.
 2
 3string outputFileName = "UseShapeUtilsXPS_out.xps";
 4
 5// Create new XPS Document
 6using (XpsDocument doc = new XpsDocument())
 7{
 8    // Set first page's size.
 9    doc.Page.Width = 650f;
10    doc.Page.Height = 240f;
11
12    // Draw a circle with center (120, 120) and radius 100.
13    XpsPath path = doc.CreatePath(doc.Utils.CreateCircle(new PointF(120f, 120f), 100f));
14    path.Fill = doc.CreateSolidColorBrush(Color.Green);
15    doc.Add(path);
16
17    // Inscribe a regular pentagon in the circle.
18    path = doc.CreatePath(doc.Utils.CreateRegularInscribedNGon(5, new PointF(120f, 120f), 100f));
19    path.Fill = doc.CreateSolidColorBrush(Color.Red);
20    doc.Add(path);
21
22    // Circumscribe a regular hexagon around the circle.
23    path = doc.CreatePath(doc.Utils.CreateRegularCircumscribedNGon(6, new PointF(120f, 120f), 100f));
24    path.Stroke = doc.CreateSolidColorBrush(Color.Magenta);
25    path.StrokeThickness = 3f;
26    doc.Add(path);
27
28    // Draw a sector of the circle centered at (340, 120), starting at -45 degrees and ending at +45 degrees.
29    path = doc.CreatePath(doc.Utils.CreatePieSlice(new PointF(340f, 120f), 100f, -45f, 45f));
30    path.Stroke = doc.CreateSolidColorBrush(Color.Red);
31    path.StrokeThickness = 5f;
32    doc.Add(path);
33
34    // Draw a segment of the circle centered at (340, 120), starting at -45 degrees and ending at +45 degrees.
35    path = doc.CreatePath(doc.Utils.CreateCircularSegment(new PointF(340f, 120f), 100f, -45f, 45f));
36    path.Fill = doc.CreateSolidColorBrush(Color.Black);
37    doc.Add(path);
38
39    // Draw a rectangle with the top left vertex (530, 20), width 100 units and height 200 units.
40    path = doc.CreatePath(doc.Utils.CreateRectangle(new RectangleF(530f, 20f, 100f, 200f)));
41    path.Stroke = doc.CreateSolidColorBrush(Color.Red);
42    doc.Add(path);
43
44    // Draw an ellipse with center (580, 120) and radii 50 and 100.
45    path = doc.CreatePath(doc.Utils.CreateEllipse(new PointF(580f, 120f), 50f, 100f));
46    path.Fill = doc.CreateSolidColorBrush(Color.Yellow);
47    doc.Add(path);
48
49    doc.Save(OutputDir + outputFileName);
50}

我们首先创建一个新的 XPS 文档,然后调整第一页的大小。我们在页面上放置的第一个形状是一个圆形(由其圆心和半径指定),并用纯绿色填充。接下来,我们在该圆形内内接一个未填充的红色正五边形。然后是一个外接正六边形,并用洋红色描边。

在右侧,我们首先在 -45 度到 +45 度之间绘制一个红色扇形(或“饼形切片”),然后在扇形上方使用相同的参数绘制一个黑色圆段。

绘图的最后一部分由一个红色矩形(由左上角顶点和尺寸指定)和一个黄色椭圆(由圆心和半径指定)组成,它们内接于矩形内。在这里,我们“手动”控制刻画。

保存的 XPS 文件中应该显示的内容如下:

XPS 形状实用程序

如何轻松在 XPS 页面上添加图像

使用 XPS 规范定义的图元,在页面上添加图像包含两个步骤:

不过,幸运的是,Aspose.Page API for .NET 的 XPS 图形实用程序中有一个便捷的方法,可以为您完成(几乎)所有工作。它还提供各种适配模式。让我们看下面的示例:

 1// Using image utilities for adding images to XPS document.
 2
 3string outputFileName = "UseImageUtilsXPS_out.xps";
 4
 5// Create new XPS Document
 6using (XpsDocument doc = new XpsDocument())
 7{
 8    // Set first page's size.
 9    doc.Page.Width = 540f;
10    doc.Page.Height = 220f;
11
12    // Draw the image box.
13    RectangleF imageBox = new RectangleF(10f, 10f, 200f, 200f);
14    XpsPath path = doc.AddPath(doc.Utils.CreateRectangle(imageBox));
15    path.Stroke = doc.CreateSolidColorBrush(Color.Black);
16    // Add an image to fit width.
17    path = doc.Utils.CreateImage(DataDir + "R08LN_NN.jpg", imageBox, ImageMode.FitToWidth);
18    // Prevent tiling.
19    ((XpsImageBrush)path.Fill).TileMode = XpsTileMode.None;
20    doc.Add(path);
21
22    // Add an image to fit width.
23    doc.Add(doc.Utils.CreateImage(DataDir + "R08LN_NN.jpg", new RectangleF(220f, 10f, 200f, 100f), ImageMode.FitToHeight));
24
25    // Add an image to fit width.
26    doc.Add(doc.Utils.CreateImage(DataDir + "R08LN_NN.jpg", new RectangleF(430f, 10f, 100f, 200f), ImageMode.FitToBox));
27
28    // Save resultant XPS document 
29    doc.Save(OutputDir + outputFileName);
30}

再次,我们新建一个 XPS 文档并调整第一页的大小。此时值得注意的是,默认情况下,用作画笔的图像将以与 XpsTileMode.Tile 模式相同的方式平铺在矩形区域上。但是,在示例的第一部分,我们演示了如何避免这种平铺。

因此,我们首先希望图像出现在 (10, 10) 位置,并适应宽 200 个单位、高 200 个单位的矩形框的宽度。为了更清楚地看到结果,我们首先绘制矩形框本身。接下来,我们创建图像(请注意,它实际上是一个填充路径)。最后,获取路径的填充并将其转换为 XpsImageBrush 后,我们将 TileMode 属性设置为 XpsTileMode.None

在右侧,我们放置相同的图像,并使其与图像框的高度相适应。注意平铺。

最后,我们再次将相同的图像放置在右侧,并使其与图像框的高度和宽度相适应,这会使图像变形。

保存的 XPS 文件中的结果如下:

XPS 图像实用程序

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.