Working with Text in XPS file | Python
Here you will find the code that explains to you how to work with text in XPS files using Python.
Add Text to XPS Documents
Aspose.Page for Python via .NET provides the XpsGlyphs Class, enabling you to insert text into XPS documents. To do so, you must specify a brush offered by the API. In the example below, we utilize XpsSolidColorBrush and then save the object of XpsDocument class. The following code snippet demonstrates the complete functionality to add text to an XPS document:
1from aspose.page.xps import *
2import aspose.pydrawing
3from util import Util
4###############################################
5###### Class and Method declaration here ######
6###############################################
7
8# The path to the documents directory.
9data_dir = Util.get_data_dir_working_with_text()
10# Create a new XPS Document
11doc = XpsDocument()
12# Create a brush
13text_fill = doc.create_solid_color_brush(aspose.pydrawing.Color.black)
14# Add a glyph to the document
15glyphs = doc.add_glyphs("Arial", 12, aspose.pydrawing.FontStyle.REGULAR, 300, 450, "Hello World!")
16glyphs.fill = text_fill
17# Save the resultant XPS document
18doc.save(data_dir + "AddText_out.xps")
The result
Add Text Using an Encoding String
Aspose.Page for Python solution provides the XpsGlyphs Class, enabling you to add text to an XPS document using an encoding string. You can specify a brush using the set_bidi_level() property of the XpsGlyphs class. The following code snippet demonstrates the complete functionality for adding text using an encoding string:
1from aspose.page.xps import *
2import aspose.pydrawing
3from util import Util
4###############################################
5###### Class and Method declaration here ######
6###############################################
7
8# The path to the documents directory.
9data_dir = Util.get_data_dir_working_with_text()
10# Create a new XPS Document
11doc = XpsDocument()
12# Add Text
13text_fill = doc.create_solid_color_brush(aspose.pydrawing.Color.black)
14glyphs = doc.add_glyphs("Arial", 20, aspose.pydrawing.FontStyle.REGULAR, 400, 200, "TEN. rof SPX.esopsA")
15glyphs.bidi_level = 1
16glyphs.fill = text_fill
17# Save the resultant XPS document
18doc.save(data_dir + "AddTextRTL_out.xps")
The result
You can download examples and data files from GitHub.