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:
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_text()
3# Create a new XPS Document
4doc = XpsDocument()
5# Create a brush
6text_fill = doc.create_solid_color_brush(aspose.pydrawing.Color.black)
7# Add a glyph to the document
8glyphs = doc.add_glyphs("Arial", 12, aspose.pydrawing.FontStyle.REGULAR, 300, 450, "Hello World!")
9glyphs.fill = text_fill
10# Save the resultant XPS document
11doc.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:
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_text()
3# Create a new XPS Document
4doc = XpsDocument()
5# Add Text
6text_fill = doc.create_solid_color_brush(aspose.pydrawing.Color.black)
7glyphs = doc.add_glyphs("Arial", 20, aspose.pydrawing.FontStyle.REGULAR, 400, 200, "TEN. rof SPX.esopsA")
8glyphs.bidi_level = 1
9glyphs.fill = text_fill
10# Save the resultant XPS document
11doc.save(data_dir + "AddTextRTL_out.xps")
The result
You can download examples and data files from GitHub.