在 XPS 文件中处理文本 | .NET
Contents
[
Hide
Show
]在 XPS 文档中添加文本
Aspose.Page for .NET 提供了 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, FontStyle.Regular, 300f, 450f, "Hello World!");
12glyphs.Fill = textFill;
13// Save resultant XPS document
14doc.Save(OutputDir + outputFileName);
对于 Linux、MacOS 和其他非 Windows 操作系统,我们提供 Aspose.Page.Drawing Nuget 包。它使用 Aspose.Drawing 后端,而不是 System.Drawing 系统库。
在上述代码片段和以下代码片段中,将使用 Aspose.Page.Drawing.FontStyle,而不是 System.Drawing.FontStyle。我们在 GitHub 上的代码示例包含所有必要的替换。
即使在非 Windows 操作系统下,System.Drawing.Common 系统库中也包含图形结构体类型,例如 System.Drawing.Point、System.Drawing.PointF、System.Drawing.Rectangle、System.Drawing.RectangleF 和 System.Drawing.Color,因此无需替换。
结果
如果您需要将文本方向从从左到右改为从右到左,例如希伯来语或阿拉伯语文本,请更改 BidiLevel 属性,该属性的默认值为“0”,即从左到右。
1glyphs.BidiLevel = 1;
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, FontStyle.Regular, 400f, 200f, "TEN. rof SPX.esopsA");
11
12//Change direction of text from left-to-right to right-to-left
13glyphs.BidiLevel = 1;
14
15glyphs.Fill = textFill;
16// Save resultant XPS document
17doc.Save(OutputDir + outputFileName);
结果
您可以从 GitHub下载示例和数据文件。