Working with Text in PostScript | .NET

Contents
[ Hide Show ]

Add Text in PS Document

In this article, we consider the ways how a text can be added to a PS document.
Obviously, the text should be written in a document with some font. Fonts can be stored in system folders, and in this case, we call them system fontsand can be stored in custom folders, which are folders in which someone put fonts for particular usage. Usually, these fonts are absent in system folders. We call such fonts custom fonts. Aspose.Page for .NET library offers methods for using both system and custom fonts.

For using system fonts we just fill or outline a text with native .NET’s System.Drawing.Font. Numbers in the method’s call are x and y coordinates of the top-left corner of the text.

1document.FillText(str, new System.Drawing.Font("Times New Roman", fontSize, FontStyle.Bold), 50, 100);

For using custom fonts we, firstly add custom fonts folder in PsSaveOptions, then fetch Aspose.Page.Font.DrFont. And finally fill or outline text with this DrFont object.

1options.AdditionalFontsFolders = new string[] { FONTS_FOLDER };
2Aspose.Page.Font.DrFont drFont = ExternalFontCache.FetchDrFont("Palatino Linotype", fontSize, FontStyle.Regular);
3document.FillText(str, drFont, 50, 100);

Another option is working with text that also belongs to a font. The font that is used to print text in the PostScript document can be embedded in this file or not embedded. In the first case, the text will always be rendered in any PostScript viewer or editor because it always lies with the text. In the second case, we should expect that the used font exists in system folders on the target host. If the used font doesn’t exist PostScript interpreter can throw an error.
The third option is also about a font because a font is the main thing in adding text. A font used for filling or drawing (or may be clipping) a text can be embedded in a PS file in various forms. Now TrueType and Type3 font types in embedding are supported.
In the example below we demonstrate the usage of various methods of adding text in a PS document with Aspose.Page for .NET library.

An algorithm for adding a text in new PS document includes the following steps:

  1. Create an output stream for the resulting PS file.
  2. Create PsSaveOptions. If we will use a custom font we add custom fonts folder(s) in the save options.
  3. Create PsDocument with the already created output stream and save options.
  4. Create necessary font, system or custom.
  5. Fill or outline the text with the created font. Here we can assign System.Drawing.Brush or System.Drawing.Pen in dependence whether we fill or draw the text. Or we can fill and outline the text in one method. If we use a method without System.Drawing.Brush or System.Drawing.Pen, the text will be filled or outlined with current paint/stroke in the current graphics state.
  6. Close the page.
  7. Save the document.

We split the example code into little code snippets in order to separate the initial preparation for the PS document, the usage of every method for adding text, and the completion of the document.

In this piece of code, we create an outputstream and PsSaveOptions, add a custom fonts folder for using the custom font in the code, create a PsDocument object, set common for all methods text as a string variable, and create fontSize variable that is also used in every used method.

 1//Create a output stream for PostScript document
 2using (Stream outPsStream = new FileStream(dataDir + "AddText_outPS.ps", FileMode.Create))
 3{
 4    //Create save options with A4 size
 5    PsSaveOptions options = new PsSaveOptions();
 6    // Set custom fonts folder. It will be added to system fonts folders for finding needed font.
 7    options.AdditionalFontsFolders = new string[] { FONTS_FOLDER };
 8    //A text to write to PS file
 9    string str = "ABCDEFGHIJKLMNO";
10    int fontSize = 48;
11
12    // Create new 1-paged PS Document
13    PsDocument document = new PsDocument(outPsStream, options, false);

Here we demonstrate the usage of system font for filling the text with the current color of the graphics state (that is black) and with the new SolidBrush.

1////////////////////////////////////// Using sysem font (located in system fonts folders) for filling the text //////////////////
2    System.Drawing.Font font = new System.Drawing.Font("Times New Roman", fontSize, FontStyle.Bold);
3    //Fill the text with 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/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The result

Add Text image1

Add Text image2

Now we fill the text with custom font.

1////////////////////////////////////// Using 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 given case it is black.
4	  document.FillText(str, drFont, 50, 200);
5	  //Fill the text with Blue color.
6	  document.FillText(str, drFont, 50, 250, new SolidBrush(Color.Blue));
7/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The result

Add Text image3

Add Text image4

Here we demonstrate the usage of system font for outlining the text with the current stroke of graphics state (that is black) and with the new Pen.

1////////////////////////////////////// Using sysem font (located in system fonts folders) for outlining the text ////////////////
2	  //Outline the text with default or already defined pen. In given case it is black colored 1-points width pen.
3	  document.OutlineText(str, font, 50, 300);
4	  //Outline the text with blue-violet colored 2-points width pen.
5	  document.OutlineText(str, font, 50, 350, new Pen(new SolidBrush(Color.BlueViolet), 2));
6/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The result

Add Text image5

Add Text image6

Now we outline the text with custom font.

1////////////////////////////////////// Using custom font (located in custom fonts folders) for outlining the text /////////////////
2		//Outline the text with default or already defined pen. In given case it is black colored 1-points width pen.
3		document.OutlineText(str, drFont, 50, 450);
4		//Outline the text with blue-violet colored 2-points width pen.
5		document.OutlineText(str, drFont, 50, 500, new Pen(new SolidBrush(Color.BlueViolet), 2));		
6///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

The result

Add Text image8

Add Text image9

Here we demonstrate the usage of system font for filling and outlining the text with new SolidBrush and Pen.

1    //Fill the text with orange color and stroke with blue colored 2-points width pen.
2    document.FillAndStrokeText(str, font, 50, 400, new SolidBrush(Color.Yellow), new Pen(new SolidBrush(Color.BlueViolet), 2));

The result

Add Text image7

And finally we fill and an outline the text with custom font.

1		//Fill the text with orange color and stroke with blue colored 2-points width pen.
2		document.FillAndStrokeText(str, drFont, 50, 550, new SolidBrush(Color.Orange), new Pen(new SolidBrush(Color.Blue), 2));

The result

Add Text image10

Close the current page, save the document.

1    //Close current page
2    document.ClosePage();
3    
4    //Save the document
5    document.Save();

See working with a text in PS documents in Java and C++.


In the example above, there are used fonts embedded in the PostScript file as TrueType fonts, because it is the default behavior of saving fonts in PsDocument class. If you need to change this behavior, please, use PsSaveOptions like the following:

1		//Do not embed used fonts.
2		options.EmbedFonts = false;
1		//Embed used fonts as Type3 fonts.
2		options.EmbedFontsAs = FontConstants.EMBED_FONTS_TYPE3;

You can download examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.