Add text to PS files
C# .NET API solution to work with texts of PS files
Working with fonts in PostScript files has some peculiarities that are important to understand. Here are a few key points:
Fonts used in PostScript files can either be embedded within the file or referenced externally. Embedded fonts will be available when the file is opened on different systems but they can increase the file size. Regardless of the initial font used for rendering text Aspose.Page’s PsDocument embeds a truncated version of the initial font in which only glyphs used in the document are added. Therefore embedded font’s volume can be significantly smaller than one of the initial fonts. Especially it is fair for multi-language fonts like Times New Roman, Courier, Arial, etc. By default, Aspose.Page always embeds fonts, but if you would like just to reference them, you need to set EmbedFonts property in PsSaveOption to false.
PostScript supports a large number of fonts, both outlines and bitmap, Adobe Type0 (composite), Type1, Type3, Type14 (CFF), Type42 (True Type), and CID-Keyed. But, for simplification, Aspose.Page’s PsDocument accepts only True Type, Open Type, and CFF fonts (with limitations) because these font types are the most popular and very similar. With simple fonts, PostScript can render characters only in the ASCII range (0-255). Therefore if rendered text contains characters that code more than 255, Aspose.Page always creates composite (Type0) font in which it connects simple font (for example Type 42) with a CMAP object, that maps the character code to the glyph ID.
When working with texts in PostScript, it's essential to ensure that the necessary font files are available and accessible to the PostScript interpreter or printer so the printed result looks well.
Font rendering quality in PostScript can vary depending on the output device or software used to interpret the file. So do not forget to test the file on different devices and printers to ensure consistent and accurate font display.
Understanding these aspects can help ensure proper font usage and consistent rendering in PostScript-based workflows. But no matter what, - Aspose.Page provides you with the functionality to manage the fonts of PS files easily. With this API you can add texts of different colors and brushes using custom or system fonts. To learn more about how to deal with PS files and how to work with texts of PS files in particular follow the documentation.
To insert texts to PS documents we need:
Aspose.Page for .NET API which is a feature-rich, powerful and easy-to-use document manipulation and conversion API for C# platform.
Open the NuGet package manager, and search for Aspose.Page and install. You may also use the following command from the Package Manager Console.
Package Manager Console Command
PM> Install-Package Aspose.Page
Steps to add text to a PS file C#.
The code snippet below demonstrates how to add text using a Unicode string to a PostScript (PS) document using the Aspose.Page library in C#. To see a fuller version of the example and more examples go to Aspose.Page-for-.NET GitHub project .
- Initialize the dataDir variable with the path to the directory containing the documents.
- Set the variable FONTS_FOLDER to set to the path of the folder containing the necessary fonts.
- Create an output stream for the PostScript document using the FileStream class.
- Specify the save options for the PostScript document using the PsSaveOptions object.
- The AdditionalFontsFolders property of the options object is set to an array containing the path to the FONTS_FOLDER. This allows the system to locate any required fonts in that folder.
- Specify the text and its size that are going to be added.
- Create a new instance of PsDocument with the output stream, options, and false as parameters. This initializes a new PostScript document with the specified options.
- Call the ClosePage() method on the document object, indicating that the current page is completed.
C# Code to add text to a PS file
using Aspose.Page;
using Aspose.Page.EPS;
using Aspose.Page.EPS.Device;
using Aspose.Page.Font;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
// Secify the path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithText();
string FONTS_FOLDER = RunExamples.GetDataDir_Data() + @"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);
//Close current page
document.ClosePage();
//Save the document
document.Save();
}
FAQ
1. Can I add text to a PostScript (PS) document?
Yes, you can add text to a PostScript document using text-drawing operators and commands including moveto
to specify the text starting position, show
to display the text, and setfont
to set the font and size.
2. How do I add text with specific formatting in a PS file?
Use the setfont
operator to select the desired font and size, and other text-related operators to adjust attributes such as color, style, and alignment.
3. What are some best practices for adding text to PostScript (PS) documents?
When adding text to a PostScript document, it’s essential to consider readability, alignment, and consistency with the document’s overall design.
PS What is PS File Format
PS format is one of the page description language (PDL) formats. It is capable to contain graphic as well as text information on the page. That is why the format was supported by most of the programs for image editing. The postscript file itself is a kind of instruction for printers. It contains information on what and how to print from its page.