
概述
在 PostScript (PS) 文件中添加文本对于各行各业至关重要,从出版到图形设计。它允许动态内容更新,而无需更改整个文档。使用 Aspose.Page for .NET,开发人员可以将此功能无缝集成到他们的应用程序中。这个强大的 PostScript API 使用户能够以编程方式编辑 PostScript 文件。其灵活性和先进功能使其成为希望提升文档处理能力的 C# 开发人员的首选。因此,让我们学习如何使用 C# 在 PS 文件中添加文本。
PostScript API 安装
您可以从 官方发布页面 下载 Aspose.Page for .NET,或在您的包管理器控制台中使用以下命令:
Install-Package Aspose.Page
如何在 C# 中添加文本到 PS 文件 - 代码片段
按照以下步骤使用 Aspose.Page for .NET 向 PS 文件添加文本:
- 为 PostScript 文档创建输出流。
- 实例化 PsSaveOptions 类的实例。
- 设置自定义字体文件夹。它将添加到系统字体文件夹中以查找所需字体。
- 设置要写入 PS 文件的文本并定义字体大小。
- 通过初始化 PsDocument 类的对象来创建新的 PS 文档。
- 使用系统字体(位于系统字体文件夹中)填充文本。
- 调用 FillText 方法使用默认或已定义的颜色填充文本。在给定情况下,颜色为黑色。
- 通过调用 Save 方法保存文档。
以下 C# 代码片段演示了如何以编程方式在 PostScript 文件中插入文本:
using Aspose.Page.EPS.Device; | |
using Aspose.Page.EPS; | |
using System.Drawing; | |
// Define the working directory path. | |
string dataDir = "files"; | |
// Create output stream for PostScript document. | |
using (Stream outPsStream = new FileStream(dataDir + "AddText_outPS.ps", FileMode.Create)) | |
{ | |
// Instantiate an instance of the PsSaveOptions class. | |
PsSaveOptions options = new PsSaveOptions(); | |
// Set custom fonts folder. It will be added to system fonts folders for finding needed font. | |
options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" }; | |
// Set the text to write to the PS file and define the font size. | |
string str = "This is a text."; | |
int fontSize = 48; | |
// Create a new PS Document by initializing an object of the PsDocument class. | |
PsDocument document = new PsDocument(outPsStream, options, false); | |
// Using sysem font (located in system fonts folders) for filling text. | |
Font font = new Font("Times New Roman", fontSize, FontStyle.Bold); | |
// Call the FillText method to fill text with default or already defined color. In given case it is black. | |
document.FillText(str, font, 50, 100); | |
// Fill text with Blue color. | |
document.FillText(str, font, 50, 150, new SolidBrush(Color.Blue)); | |
// Close current page | |
document.ClosePage(); | |
// Save the document by calling the Save method. | |
document.Save(); | |
} |

使用 Unicode 字符串在 PS 文件中插入文本
string dataDir = "files"; | |
string FONTS_FOLDER = @"necessary_fonts/"; | |
//Create output stream for PostScript document | |
using (Stream outPsStream = new FileStream(dataDir + "AddTextUsingUnocodeString_outPS.ps", FileMode.Create)) | |
{ | |
//Create save options with A4 size | |
PsSaveOptions options = new PsSaveOptions(); | |
// Set custom fonts folder. It will be added to system fonts folders for finding needed font. | |
options.AdditionalFontsFolders = new string[] { FONTS_FOLDER }; | |
//A text to write to PS file | |
string str = "試してみます。"; | |
int fontSize = 48; | |
// Create new 1-paged PS Document | |
PsDocument document = new PsDocument(outPsStream, options, false); | |
// Using custom font (located in custom fonts folders) for filling text. | |
DrFont drFont = ExternalFontCache.FetchDrFont("Arial Unicode MS", fontSize, FontStyle.Regular); | |
//Fill text with default or already defined color. In given case it is black. | |
document.FillText(str, drFont, 50, 200); | |
//Fill text with Blue color. | |
document.FillText(str, drFont, 50, 250, new SolidBrush(Color.Blue)); | |
//Close current page | |
document.ClosePage(); | |
//Save the document | |
document.Save(); | |
} |
获取免费许可证
要探索 Aspose.Page for .NET 的全部潜力,请从 这里 获取免费试用许可证。
结论
我们已经了解了如何使用 Aspose.Page for .NET 在 PS 文件中添加文本。这个 PostScript API 提供了一种无缝编辑 PostScript 文件的方法,使其成为开发人员的宝贵选择。今天就探索 Aspose.Page for .NET,提升您的文档处理能力。
公开资源
探索其他资源,如文档和社区论坛,以加深您对 Aspose.Page for .NET 的理解。这些资源提供了超出博客内容的有价值的见解和支持。
常见问题解答 – FAQs
如何使用 Aspose.Page for .NET 向 PS 文件添加文本?
要添加文本,加载 PS 文件与 PsDocument
,创建 PsText
对象,将其添加到文档中,并保存更改。使用提供的 代码片段 作为指南。
Aspose.Page for .NET 是否适合编辑 PostScript 文件?
是的,Aspose.Page for .NET 非常适合编辑 PostScript 文件。它提供了强大的 PostScript API、易于集成以及高级自定义选项。
我可以在购买前试用 Aspose.Page for .NET 吗?
可以,您可以从 Aspose 的临时许可证页面 获取免费试用许可证。这使您能够在没有限制的情况下探索库的功能。