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// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
2// The path to the documents directory.
3string dataDir = RunExamples.GetDataDir_WorkingWithText();
4// Create new XPS Document
5XpsDocument doc = new XpsDocument();
6//Create a brush
7XpsSolidColorBrush textFill = doc.CreateSolidColorBrush(Color.Black);
8//Add glyph to the document
9XpsGlyphs glyphs = doc.AddGlyphs("Arial", 12, FontStyle.Regular, 300f, 450f, "Hello World!");
10glyphs.Fill = textFill;
11// Save resultant XPS document
12doc.Save(dataDir + "AddText_out.xps");
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// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET
2// The path to the documents directory.
3string dataDir = RunExamples.GetDataDir_WorkingWithText();
4// Create new XPS Document
5XpsDocument doc = new XpsDocument();
6// Add Text
7XpsSolidColorBrush textFill = doc.CreateSolidColorBrush(Color.Black);
8XpsGlyphs glyphs = doc.AddGlyphs("Arial", 20, FontStyle.Regular, 400f, 200f, "TEN. rof SPX.esopsA");
9glyphs.BidiLevel = 1;
10glyphs.Fill = textFill;
11// Save resultant XPS document
12doc.Save(dataDir + "AddText_out.xps");
The result
You can download examples and data files from GitHub.