在 XPS 文件中处理图像 | Java
Contents
[
Hide
Show
]在 XPS 文档中添加图像
Aspose.Page for Java 提供了 XpsPath 类,您可以使用它在 XPS 文档中添加图像。您需要创建 Matrix 和 ImageBrush 并保存 XpsDocument。以下代码片段展示了在 XPS 文档中添加图像的完整功能:
1// Add image to XPS document.
2
3// Create new XPS Document
4XpsDocument doc = new XpsDocument();
5
6String outputFileName = "AddImage_outXPS.xps";
7
8// Add Image
9XpsPath path = doc.addPath(doc.createPathGeometry("M 30,20 l 258.24,0 0,56.64 -258.24,0 Z"));
10//Creating a matrix is optional, it can be used for proper positioning
11path.setRenderTransform(doc.createMatrix(0.7f, 0f, 0f, 0.7f, 0f, 20f));
12//Create Image Brush
13path.setFill((XpsImageBrush)doc.createImageBrush(getDataDir() + "QL_logo_color.tif", new Rectangle2D.Float(0f, 0f, 258.24f, 56.64f), new Rectangle2D.Float(50f, 20f, 193.68f, 42.48f)));
14// Save resultant XPS document
15doc.save(getOutputDir() + outputFileName);运行此代码的结果如下:

添加平铺图像
Aspose.Page for Java 提供了 XpsPath 类,您可以使用它在 XPS 文档中添加图像。您需要创建 Matrix 和 ImageBrush,然后设置 setTileMode(XpsTileMode.Tile) 模式并保存 XpsDocument。以下代码片段展示了在 XPS 文档中添加平铺图像的完整功能:
1// Paint rectangle with tiled image (texture pattern) in XPS document.
2
3// Create new XPS Document
4XpsDocument doc = new XpsDocument();
5
6String outputFileName = "AddTextureTilingPattern_outXPS.xps";
7
8// Tile image
9// ImageBrush filled rectangle in the right top bellow
10XpsPath path = doc.addPath(doc.createPathGeometry("M 10,160 L 228,160 228,305 10,305"));
11path.setFill((XpsImageBrush)doc.createImageBrush(getDataDir() + "R08LN_NN.jpg", new Rectangle2D.Float(0f, 0f, 128f, 96f), new Rectangle2D.Float(0f, 0f, 64f, 48f)));
12((XpsImageBrush)path.getFill()).setTileMode(com.aspose.xps.XpsTileMode.Tile);
13path.getFill().setOpacity(0.5f);
14// Save resultant XPS document
15doc.save(getOutputDir() + outputFileName);运行此代码的结果如下:

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