在 XPS 文件中处理文本 | .NET
Contents
[
Hide
Show
]在 XPS 文档中添加文本
Aspose.Page for .NET 提供了 XpsGlyphs 类,您可以使用它在 XPS 文档中添加文本。您需要指定 API 提供的画笔。
以下示例使用了 XpsSolidColorBrush 并保存了 XpsDocument 类的对象。以下代码片段展示了在 XPS 文档中添加文本的完整功能:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
2// The path to the documents directory.
3string dataDir = RunExamples.GetDataDir_WorkingWithText();
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, FontStyle.Regular, 300f, 450f, "Hello World!");
10glyphs.Fill = textFill;
11// Save resultant XPS document
12doc.Save(dataDir + "AddText_out.xps");
对于 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// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
2// The path to the documents directory.
3string dataDir = RunExamples.GetDataDir_WorkingWithText();
4// Create new XPS Document
5XpsDocument doc = new XpsDocument();
6// Add Text
7XpsSolidColorBrush textFill = doc.CreateSolidColorBrush(Color.Black);
8XpsGlyphs glyphs = doc.AddGlyphs("Arial", 20, FontStyle.Regular, 400f, 200f, "TEN. rof SPX.esopsA");
9glyphs.BidiLevel = 1;
10glyphs.Fill = textFill;
11// Save resultant XPS document
12doc.Save(dataDir + "AddText_out.xps");
结果
您可以从 GitHub下载示例和数据文件。