XPSファイル内のテキスト操作 | .NET
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以外のOSでは、 Aspose.Page.Drawing NuGetパッケージをご利用いただけます。このパッケージは、System.Drawingシステムライブラリではなく、Aspose.Drawingバックエンドを使用します。
上記および以下のコードスニペットでは、System.Drawing.FontStyleではなく、Aspose.Page.Drawing.FontStyleが使用されます。GitHubのコード例には、必要な置換がすべて含まれています。
System.Drawing.Point、System.Drawing.PointF、System.Drawing.Rectangle、System.Drawing.RectangleF、System.Drawing.Colorなどのグラフィックス構造体型は、Windows以外のOSでもSystem.Drawing.Commonシステムライブラリに含まれているため、置換されません。
結果
ヘブライ語やアラビア語のテキストのように、左から右へのテキストを右から左へのテキストに変更する必要がある場合は、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 からダウンロードできます。