如何在 PS 文件中添加文字

概述

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 文件

  1. 為 PostScript 文檔創建輸出流。
  2. 實例化 PsSaveOptions 類的實例。
  3. 設置自定義字體文件夾。它將被添加到系統字體文件夾中以尋找所需的字體。
  4. 設置要寫入 PS 文件的文本並定義字體大小。
  5. 通過初始化 PsDocument 類的對象來創建新的 PS 文檔。
  6. 使用系統字體(位於系統字體文件夾中)來填充文本。
  7. 調用 FillText 方法以使用默認或已定義的顏色填充文本。在給定的情況下它是黑色。
  8. 通過調用 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();
}
輸出:
編輯 PostScript 文件

使用 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 文件中添加文字?

要添加文字,使用 PsDocument 加載 PS 文件,創建一個 PsText 對象,將其添加到文檔中,然後保存更改。使用提供的 代碼片段 作為指南。

Aspose.Page for .NET 適合編輯 PostScript 文件嗎?

是的,Aspose.Page for .NET 非常適合編輯 PostScript 文件。它提供了一個強大的 PostScript API,易於集成和先進的自定義選項。

我可以在購買之前試用 Aspose.Page for .NET 嗎?

是的,您可以從 Aspose 的臨時許可證頁面 獲取免費試用許可證。這允許您在沒有限制的情況下探索庫的功能。

探索