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