Working with Document in PostScript | .NET
Create PS Document
Aspose.Page for .NET offers two constructors in order to create PsDocument class. The following code snippet creates a 1-paged PS document:
1// Create new multipaged PS document from scratch.
2
3string outputFileName = "document_out.ps";
4
5//Create save options
6PsSaveOptions options = new PsSaveOptions();
7//If you want to aassign page size other than A4, set page size in options
8options.PageSize = PageConstants.GetSize(PageConstants.SIZE_A4, PageConstants.ORIENTATION_PORTRAIT);
9//If you want to aassign page margins other empty, set page margins in options
10options.Margins = PageConstants.GetMargins(PageConstants.MARGINS_ZERO);
11//If you plan to use fonts that located in non system folders, set additional fonts folders in options
12options.AdditionalFontsFolders = new string[] { DataDir };
13
14//Set variable that indicates if resulting PostScript document will be multipaged
15bool multiPaged = false;
16
17// Create new multipaged PS Document with one page opened
18PsDocument document = new PsDocument(OutputDir + outputFileName, options, multiPaged);
19
20//Close current page
21document.ClosePage();
22//Save the document
23document.Save();
If PS document is planning to be multi-paged, set multiPaged variable to true.
Another constructor allows creating PsDocument object with a defined number of pages:
1// Create new PS document with defined number of pages.
2
3string outputFileName = "document_out.ps";
4
5//Create save options
6PsSaveOptions options = new PsSaveOptions();
7//If you want to aassign page size other than A4, set page size in options
8options.PageSize = PageConstants.GetSize(PageConstants.SIZE_A4, PageConstants.ORIENTATION_PORTRAIT);
9//If you want to aassign page margins other empty, set page margins in options
10options.Margins = PageConstants.GetMargins(PageConstants.MARGINS_ZERO);
11//If you plan to use fonts that located in non system folders, set additional fonts folders in options
12options.AdditionalFontsFolders = new string[] { DataDir };
13
14// Create new multipaged PS Document with 2 pages. These two pages are not created. It must be added by OpenPage() method.
15PsDocument document = new PsDocument(OutputDir + outputFileName, options, 2);
16
17//Add the first page
18document.OpenPage();
19//Close current page
20document.ClosePage();
21//Save the document
22document.Save();
See working with PS document in Java.
Let’s look at the PsSaveOptions class that encapsulates possible options helping to create the right PostScript document.
SaveFormat specifies an output format of documents. Can be PS or EPS. In Aspose.Page library differences between these two formats are reflected only in PostScript comments and a file extension. Also in accordance with the EPS file specification EPS files should be 1-paged. PS format is used by default.
PageSize specifies the size of the pages in the PS document. However, we can assign different page sizes for every page if it is required. The page size can be obtained from PageConstants class like in following example:
1options.PageSize = PageConstants.GetSize(PageConstants.SIZE_International, PageConstants.ORIENTATION_PORTRAIT);
The default page size is “A4” in “Portrait” orientation.
- Margins specifies blank fields between the page boundaries and left, up, right, and bottom edges of the page content. It can be obtained from PageConstants class.
1options.Margins = PageConstants.GetMargins(PageConstants.MARGINS_SMALL); // 20 points for each margin
Default margins are “ZERO” (0, 0, 0, 0).
- BackGroundColor specifies a color of page’s background. Can be assigned as:
1options.BackgroundColor = Color.FromArgb(211, 8, 48);
or:
1options.BackgroundColor = Color.Yellow;
For Linux, MacOS and other non-Windows operation systems we offer to use our Aspose.Page.Drawing Nuget package. It uses Aspose.Drawing backend instead of System.Drawing system library.
So import Aspose.Page.Drawing namespace instead of System.Drawing one. In the above code snippets Aspose.Page.Drawing.Color will be used instead of System.Drawing.Color. Our code examples on GitHub contain all the necessary substitutions.
Default value is “null” that means no background.
EmbedFonts controls the behavior of PsDocument while saving it to the file. If “false”, used fonts will not be written in the PS file. In this case, the PostScript interpreter will throw an error if the used font cannot be found in system folders on the target host.
EmbedFontsAs controls the way how fonts will be embedded in the PS file. At this moment two ways work TrueType and Type3 font formats. The value can be set with help of FontConstants class like the following:
1options.EmbedFontsAs = FontsConstants.EMBED_FONTS_TYPE3
The default value is “TrueType”.
JpegQualityLevel specifies a level of compression/quality of images in the resulting PS document. The more quality is required the less compression will be and vice versa. Minimal quality is 0, and maximal - 100. The default is 75.
AdditionalFontsFolder specifies locations where to find fonts. System font folders are always included by default.
Debug allows outputting debug information to the console. The default value is false.
You can download examples and data files from GitHub.