在 XPS 文件中使用可视化画笔 | Java

使用可视化画笔添加网格

Aspose.Page for Java 提供了 XpsVisualBrush 类,您可以使用它在 XPS 文档上添加网格。您需要指定 XpsPathGeometry 并将 XpsCanvas 添加到 XpsDocument 类的对象中。以下代码片段展示了在 XPS 文档上添加网格的完整功能:

 1// Add grid with visual brush to XPS document.
 2
 3XpsDocument doc = new XpsDocument();
 4
 5String outputFileName = "AddGrid_out.xps";
 6
 7// Geometry for magenta grid VisualBrush
 8XpsPathGeometry pathGeometry = doc.createPathGeometry();
 9Point2D.Float[] points = new Point2D.Float[] { 
10    new Point2D.Float(240f, 5f), 
11    new Point2D.Float(240f, 310f), 
12    new Point2D.Float(0f, 310f) 
13};
14pathGeometry.addSegment(doc.createPolyLineSegment(points));
15pathGeometry.getPathFigures().get(0).setStartPoint(new Point2D.Float(0f, 5f));
16
17// Canvas for magenta grid VisualBrush
18XpsCanvas visualCanvas = doc.createCanvas();
19
20XpsPath visualPath = visualCanvas.addPath(
21    doc.createPathGeometry("M 0,4 L 4,4 4,0 6,0 6,4 10,4 10,6 6,6 6,10 4,10 4,6 0,6 Z"));
22visualPath.setFill(doc.createSolidColorBrush(doc.createColor(1f, .61f, 0.1f, 0.61f)));
23
24XpsPath gridPath = doc.createPath(pathGeometry);
25//Create Visual Brush, it is specified by some XPS fragment (vector graphics and glyphs)
26gridPath.setFill(doc.createVisualBrush(visualCanvas,
27    new Rectangle2D.Float(0f, 0f, 10f, 10f), new Rectangle2D.Float(0f, 0f, 10f, 10f)));
28((XpsVisualBrush)gridPath.getFill()).setTileMode(XpsTileMode.Tile);
29
30// New canvas
31XpsCanvas canvas = doc.addCanvas();
32canvas.setRenderTransform(doc.createMatrix(1f, 0f, 0f, 1f, 268f, 70f));
33// Add grid
34canvas.addPath(pathGeometry);
35// Red transparent rectangle in the middle top
36XpsPath path = canvas.addPath(doc.createPathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
37path = canvas.addPath(doc.createPathGeometry("M 10,10 L 228,10 228,100 10,100"));
38path.setFill(doc.createSolidColorBrush(doc.createColor(1.0f, 0.0f, 0.0f)));
39path.setOpacity(0.7f);
40// Save resultant XPS document
41doc.save(getOutputDir() + outputFileName);
Example-AddGridXPS.java hosted with ❤ by GitHub

请参阅 .NETC++ 中 XPS 文档中可视化画笔的使用方法。


结果如下

添加网格

您可以从 GitHub下载示例和数据文件。

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.