XPSファイル内のテキスト操作 | Java
Contents
[
Hide
Show
]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);結果

エンコーディング文字列を使用してテキストを追加する
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);結果

サンプルとデータ ファイルは GitHub からダウンロードできます。