Working with Text in XPS file | .NET
Add Text in XPS Document
Aspose.Page for .NET offers XpsGlyphs class, with which you can add text on XPS documents. You need to specify any brush provided by the API.
The example below 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, FontStyle.Regular, 300f, 450f, "Hello World!");
12glyphs.Fill = textFill;
13// Save resultant XPS document
14doc.Save(OutputDir + outputFileName);
For Linux, MacOS and other non-Windows operation systems we offer to use our Aspose.Page.Drawing Nuget package. It uses Aspose.Drawing backend instead of System.Drawing system library.
In the above and the following code snippets Aspose.Page.Drawing.FontStyle will be used instead of System.Drawing.FontStyle. Our code examples on GitHub contain all the necessary substitutions.
The graphics struct types, such as System.Drawing.Point, System.Drawing.PointF, System.Drawing.Rectangle, System.Drawing.RectangleF and System.Drawing.Color are included in System.Drawing.Common system library even under non-Windows OSs, therefore it isn’t substituted.
The result
If you need change direction form left-to-right to right-to-left, as in Hebrew or Arabic texts, change BidiLevel property, which default value is “0”, that is left-to-right.
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);
The result
You can download examples and data files from GitHub.