处理 XPS 文件中的文本 | Java
向 XPS 文档添加文本
Aspose.Page for Java 提供了 XpsGlyphs 类,您可以使用它在 XPS 文档中添加文本。您需要指定 API 提供的任何画笔,以下示例使用了 XpsSolidColorBrush 并保存了 XpsDocument 类的对象。以下代码片段展示了在 XPS 文档中添加文本的完整功能:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
2// The path to the documents directory.
3String dataDir = Utils.getDataDir();
4// Create new XPS Document
5XpsDocument doc = new XpsDocument();
6//Create a brush
7XpsSolidColorBrush textFill = doc.createSolidColorBrush(Color.BLACK);
8//Add glyph to the document
9XpsGlyphs glyphs = doc.addGlyphs("Arial", 12, XpsFontStyle.Regular, 300f, 450f, "Hello World!");
10glyphs.setFill(textFill);
11// Save resultant XPS document
12doc.save(dataDir + "AddText_out.xps");
结果
使用编码字符串添加文本
Java 解决方案提供了 XpsGlyphs 类,您可以使用该类在 XPS 文档中使用编码字符串添加文本。您需要使用 XpsGlyphs 类的 setBidiLevel() 属性指定画笔。以下代码片段展示了使用编码字符串添加文本的完整功能:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
2// The path to the documents directory.
3String dataDir = Utils.getDataDir();
4// Create new XPS Document
5XpsDocument doc = new XpsDocument();
6// Add Text
7XpsSolidColorBrush textFill = doc.createSolidColorBrush(Color.BLACK);
8XpsGlyphs glyphs = doc.addGlyphs("Arial", 20, XpsFontStyle.Regular, 400f, 200f, "AVAJ rof SPX.esopsA");
9glyphs.setBidiLevel(1);
10glyphs.setFill(textFill);
11// Save resultant XPS document
12doc.save(dataDir + "AddEncodingText_out.xps");
结果
您可以从 GitHub下载示例和数据文件。