在 PostScript 中处理文本 | .NET
在 PS 文档中添加文本
本文将探讨如何在 PS 文档中添加文本。
显然,文本应该使用某种字体。字体可以存储在系统文件夹中,在这种情况下,我们称之为系统字体;也可以存储在自定义文件夹中,自定义文件夹是用于存放特定用途字体的文件夹。通常,这些字体在系统文件夹中不存在。我们称此类字体为自定义字体。 Aspose.Page for .NET 库提供了使用系统和自定义字体的方法。
要使用系统字体,我们只需使用原生 .NET 的 System.Drawing.Font 方法填充或勾勒文本轮廓即可。方法调用中的数字是文本左上角的 x 和 y 坐标。
1document.FillText(str, new System.Drawing.Font("Times New Roman", fontSize, FontStyle.Bold), 50, 100);
要使用自定义字体,首先在 PsSaveOptions 中添加 custom fonts 文件夹,然后获取 Aspose.Page.Font.DrFont。最后使用此 DrFont 对象填充或勾勒文本轮廓。
1options.AdditionalFontsFolders = new string[] { FONTS_FOLDER };
2Aspose.Page.Font.DrFont drFont = ExternalFontCache.FetchDrFont("Palatino Linotype", fontSize, FontStyle.Regular);
3document.FillText(str, drFont, 50, 100);
另一种选择是使用也属于字体的文本。用于在 PostScript 文档中打印文本的字体可以嵌入到此文件中,也可以不嵌入。 在第一种情况下,文本将始终呈现在任何 PostScript 查看器或编辑器中,因为它始终与文本一起存在。在第二种情况下,我们应该预期所使用的字体存在于目标主机的系统文件夹中。如果所使用的字体不存在,PostScript 解释器可能会抛出错误。
第三种选择也与字体有关,因为字体是添加文本的关键。用于填充、绘制(或剪切)文本的字体可以以各种形式嵌入到 PS 文件中。现在支持嵌入 TrueType 和 Type3 字体类型。
第四种选择是可以使用指定的步长(宽度)写入文本的字形。这可以为文本增添一些艺术效果。
在下面的示例中,我们演示了使用 Aspose.Page for .NET 库在 PS 文档中添加文本的各种方法。
在新的 PS 文档中添加文本的算法包括以下步骤:
- 为生成的 PS 文件创建输出流。
- 创建 PsSaveOptions。如果我们使用自定义字体,请在保存选项中添加自定义字体文件夹。
- 使用已创建的输出流和保存选项创建 PsDocument。
- 创建必要的字体:系统 或 自定义。
- 使用创建的字体填充或勾勒文本轮廓。我们可以根据填充还是绘制文本来指定 System.Drawing.Brush 或 System.Drawing.Pen。或者,我们可以在一个方法中同时填充和勾勒文本轮廓。如果我们使用不带System.Drawing.Brush或System.Drawing.Pen的方法,文本将使用当前图形状态下的当前画笔/描边进行填充或勾勒轮廓。
- 关闭页面。
- 保存文档。
我们将示例代码拆分成几个小的代码片段,以便区分 PS 文档的初始准备、各种添加文本方法的使用以及文档的完成。
在这段代码中,我们创建了一个输出流和 PsSaveOptions,添加了一个自定义字体文件夹以便在代码中使用自定义字体,创建了一个 PsDocument 对象,将所有方法的通用文本设置为字符串变量,并创建了 fontSize 变量(该变量也在每个使用的方法中使用)。
1//Create an output stream for the PostScript document
2using (Stream outPsStream = new FileStream(dataDir + "AddText_outPS.ps", FileMode.Create))
3{
4 //Create save options with the A4 size
5 PsSaveOptions options = new PsSaveOptions();
6 // Set the custom fonts folder. It will be added to system fonts folders for finding the needed font.
7 options.AdditionalFontsFolders = new string[] { FONTS_FOLDER };
8 //A text to write to the PS file
9 string str = "ABCDEFGHIJKLMNO";
10 int fontSize = 48;
11
12 // Create a new 1-paged PS Document
13 PsDocument document = new PsDocument(outPsStream, options, false);
这里我们演示了如何使用系统字体用图形状态的当前颜色(即黑色)和新的SolidBrush填充文本。
1////////////////////////////////////// Using the sysem font (located in system fonts folders) for filling the text //////////////////
2 Font font = new Font("Times New Roman", fontSize, FontStyle.Bold);
3 //Fill the text with the default or already defined color. In given case it is black.
4 document.FillText(str, font, 50, 100);
5 //Fill the text with Blue color.
6 document.FillText(str, font, 50, 150, new SolidBrush(Color.Blue));
7/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
对于 Linux、MacOS 和其他非 Windows 操作系统,我们推荐使用我们的 Aspose.Page.Drawing Nuget 软件包。它使用 Aspose.Drawing 后端,而非 System.Drawing 系统库。
因此,请导入 Aspose.Page.Drawing 命名空间,而不是 System.Drawing 命名空间。在以上和以下代码片段中,将使用 Aspose.Page.Drawing.Font 代替 System.Drawing.Font,使用 Aspose.Page.Drawing.SolidBrush 代替 System.Drawing.SolidBrush,等等。我们在 GitHub 上的代码示例包含所有必要的替换。
结果
现在我们用自定义字体填充文本。
1////////////////////////////////////// Using the custom font (located in custom fonts folders) for filling the text /////////////////
2 DrFont drFont = ExternalFontCache.FetchDrFont("Palatino Linotype", fontSize, FontStyle.Regular);
3 //Fill the text with default or already defined color. In the given case it is black.
4 document.FillText(str, drFont, 50, 200);
5 //Fill the text with the Blue color.
6 document.FillText(str, drFont, 50, 250, new SolidBrush(Color.Blue));
7/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
结果
这里我们演示了如何使用系统字体,通过当前图形状态的笔触(即黑色)和新的画笔来勾勒文本轮廓。
1////////////////////////////////////// Using the sysem font (located in system fonts folders) for outlining the text ////////////////
2 //Outline the text with default or already defined pen. In the given case it is a black colored 1-points width pen.
3 document.OutlineText(str, font, 50, 300);
4 //Outline the text with the blue-violet colored 2-points width pen.
5 document.OutlineText(str, font, 50, 350, new Pen(new SolidBrush(Color.BlueViolet), 2));
6/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
结果
现在我们用自定义字体勾勒出文本轮廓。
1////////////////////////////////////// Using the custom font (located in custom fonts folders) for outlining the text /////////////////
2 //Outline the text with the default or already defined pen. In given case it is a black colored 1-points width pen.
3 document.OutlineText(str, drFont, 50, 450);
4 //Outline the text with the blue-violet colored 2-points width pen.
5 document.OutlineText(str, drFont, 50, 500, new Pen(new SolidBrush(Color.BlueViolet), 2));
6///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
结果
这里我们演示了如何使用系统字体,通过新的SolidBrush和Pen来填充和勾勒文本轮廓。
1 //Fill the text with the orange color and stroke with the blue colored 2-points width pen.
2 document.FillAndStrokeText(str, font, 50, 400, new SolidBrush(Color.Yellow), new Pen(new SolidBrush(Color.BlueViolet), 2));
结果
现在我们用自定义字体填充文本并勾勒出轮廓。
1 //Fill the text with the orange color and stroke with the blue colored 2-points width pen.
2 document.FillAndStrokeText(str, drFont, 50, 550, new SolidBrush(Color.Orange), new Pen(new SolidBrush(Color.Blue), 2));
结果
最后,我们用指定的字形宽度填充文本。宽度的数量必须等于字形的数量。
1 //Glyphs widths
2 float[] widths = new float[] { 87, 87, 87, 87, 34, 87, 87 };
3 //Fill ASCII text using with assigning glyphs widths.
4 document.FillText("BAMBOOK", widths, drFont, 50, 600, new SolidBrush(Color.Blue));
5
6 //Glyphs widths
7 widths = new float[] { 87, 34, 87, 87, 87, 87, 87 };
8 //Fill Unicode text using with assigning glyphs widths.
9 document.FillText("ЗООПАРК", widths, drFont, 50, 650, new SolidBrush(Color.Orange));
结果
关闭当前页面,保存文档。
1 //Close the current page
2 document.ClosePage();
3
4 //Save the document
5 document.Save();
请参阅使用 Java 在 PS 文档中处理文本。
在上面的示例中,我们使用了 PostScript 文件中嵌入的 TrueType 字体,因为这是 PsDocument 类中保存字体的默认行为。如果您需要更改此行为,请使用 PsSaveOptions,如下所示:
1 //Do not embed used fonts.
2 options.EmbedFonts = false;
1 //Embed used fonts as Type3 fonts.
2 options.EmbedFontsAs = FontConstants.EMBED_FONTS_TYPE3;
您可以从 GitHub下载示例和数据文件。