处理 XPS 文件中的文本 | Java

向 XPS 文档添加文本

Aspose.Page for Java 提供了 XpsGlyphs 类,您可以使用它在 XPS 文档中添加文本。您需要指定 API 提供的任何画笔,以下示例使用了 XpsSolidColorBrush 并保存了 XpsDocument 类的对象。以下代码片段展示了在 XPS 文档中添加文本的完整功能:

 1// Add text to XPS document.
 2
 3// Create new XPS Document
 4XpsDocument doc = new XpsDocument();
 5
 6String outputFileName = "AddText_out.xps";
 7
 8//Create a brush 
 9XpsSolidColorBrush textFill = doc.createSolidColorBrush(Color.BLACK);
10//Add glyph to the document
11XpsGlyphs glyphs = doc.addGlyphs("Arial", 12, XpsFontStyle.Regular, 300f, 450f, "Hello World!");
12glyphs.setFill(textFill);
13// Save resultant XPS document
14doc.save(getOutputDir() + outputFileName);
Example-AddTextXPS.java hosted with ❤ by GitHub

请参阅 .NETC++ 中 XPS 文档中文本的处理方法。


结果

添加文本

使用编码字符串添加文本

Java 解决方案提供了 XpsGlyphs 类,您可以使用该类在 XPS 文档中使用编码字符串添加文本。您需要使用 XpsGlyphs 类的 setBidiLevel() 属性指定画笔。以下代码片段展示了使用编码字符串添加文本的完整功能:

 1// Change direction of text from left-to-right to right-to-left, as in Hebrew or Arabic texts, in XPS document.
 2
 3// Create new XPS Document
 4XpsDocument doc = new XpsDocument();
 5
 6String outputFileName = "AddTextRTL_out.xps";
 7
 8// Add Text
 9XpsSolidColorBrush textFill = doc.createSolidColorBrush(Color.BLACK);
10XpsGlyphs glyphs = doc.addGlyphs("Arial", 20, XpsFontStyle.Regular, 400f, 200f, "TEN. rof SPX.esopsA");
11
12//Change direction of text from left-to-right to right-to-left
13glyphs.setBidiLevel(1);
14
15glyphs.setFill(textFill);
16// Save resultant XPS document
17doc.save(getOutputDir() + outputFileName);

请参阅 .NETC++ 中 XPS 文档文本的处理方法。


结果

从右到左添加文本

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

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.