Trabajar con texto en un archivo XPS | .NET
Agregar texto en un documento XPS
Aspose.Page para .NET ofrece la clase XpsGlyphs, con la que puede agregar texto en documentos XPS. Debe especificar cualquier pincel proporcionado por la API.
El siguiente ejemplo utiliza XpsSolidColorBrush y guarda el objeto de la clase XpsDocument. El siguiente fragmento de código muestra la funcionalidad completa para agregar texto en un documento XPS:
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);
Para Linux, MacOS y otros sistemas operativos distintos de Windows, ofrecemos utilizar nuestro paquete Nuget Aspose.Page.Drawing. Utiliza el backend Aspose.Drawing en lugar de la biblioteca del sistema System.Drawing.
En los fragmentos de código anteriores y siguientes se utilizará Aspose.Page.Drawing.FontStyle en lugar de System.Drawing.FontStyle. Nuestros ejemplos de código en GitHub contienen todas las sustituciones necesarias.
Los tipos de estructuras de gráficos, como System.Drawing.Point, System.Drawing.PointF, System.Drawing.Rectangle, System.Drawing.RectangleF y System.Drawing.Color se incluyen en la biblioteca del sistema System.Drawing.Common incluso en sistemas no Sistemas operativos Windows, por lo que no se sustituye.
El resultado
Si necesita cambiar la dirección de izquierda a derecha a derecha a izquierda, como en los textos hebreos o árabes, cambie la propiedad BidiLevel, cuyo valor predeterminado es “0”, es decir, de izquierda a derecha.
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);
El resultado
Puede descargar ejemplos y archivos de datos desde GitHub.