Working with Text in XPS file | Java

Add Text to XPS Document

Aspose.Page for Java offers XpsGlyphs Class, with which you can add text on XPS document. You need to specify any brush offered by the API, below example uses XpsSolidColorBrush and saves the object of XpsDocument class. The following code snippet shows complete functionality to add text on an XPS document:

 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

See working with text in XPS documents in .NET and C++.


The result

Add Text

Add Text Using Encoding String

The solution for Java offers XpsGlyphs Class, with which you can add text using Encoding String on an XPS document. You need to specify a brush using setBidiLevel() property of XpsGlyphs class. The following code snippet shows complete functionality to add text using encoding string:

 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);

See working with text in XPS documents in .NET and C++.


The result

Add Text Right-To-Left

You can download examples and data files from GitHub.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.