Working with Document in PS file | Python
Create PS Document
Aspose.Page for Python via .NET includes two constructors for creating PsDocument class. The following code snippet explains a creation of 1-paged PS document:
1from aspose.page.eps import *
2from aspose.page.eps.device import *
3from util import Util
4###############################################
5###### Class and Method declaration here ######
6###############################################
7
8# The path to the documents directory.
9dir = Util.get_data_dir_working_with_document()
10
11# Create an output stream for a PostScript document
12with open(dir + "document.ps", "wb") as out_ps_stream:
13 # Create save options
14 options = PsSaveOptions()
15 # If you want to aassign a page size other than A4, set the page size in options
16 options.page_size = PageConstants.get_size(PageConstants.SIZE_A4, PageConstants.ORIENTATION_PORTRAIT)
17 # If you want to aassign page margins other empty, set the page margins in options
18 options.margins = PageConstants.get_margins(PageConstants.MARGINS_ZERO)
19 # If you plan to use fonts that located in non system folders, set additional fonts folders in options
20 options.additional_fonts_folders = [ dir ]
21
22 # Set a variable that indicates if resulting PostScript document will be multipaged
23 multi_paged = False
24
25 # Create a new multipaged PS Document with one page opened
26 document = PsDocument(out_ps_stream, options, multi_paged)
27
28 # Close the current page
29 document.close_page()
30 # Save the document
31 document.save()
If you want PS document to be multi-paged, set multiPaged variable to true.
The other constructor allows creating PsDocument object with a defined number of pages:
1from aspose.page.eps import *
2from aspose.page.eps.device import *
3from util import Util
4###############################################
5###### Class and Method declaration here ######
6###############################################
7
8with open(dir + "document.ps", "wb") as out_ps_stream:
9# Create save options
10options = PsSaveOptions()
11# If you want to assign the page size other than A4, set the page size in options
12options.page_size = PageConstants.get_size(PageConstants.SIZE_A4, PageConstants.ORIENTATION_PORTRAIT)
13# If you want to aassign page margins other empty, set the page margins in options
14options.margins = PageConstants.get_margins(PageConstants.MARGINS_ZERO)
15# If you plan to use fonts that located in non system folders, set additional fonts folders in options
16options.additional_fonts_folders = [ dir ]
17
18# Create a new multipaged PS Document with one page opened
19document = PsDocument(out_ps_stream, options, 2)
20
21# Close the current page
22document.close_page()
23# Save the document
24document.save()
Let’s take a look at the PsSaveOptions class that encapsulates possible options helping to create the correct PostScript document.
SaveFormat parameter determines the output format of documents, which can be either PS or EPS. In the Aspose.Page library, the distinctions between these two formats are primarily reflected in PostScript comments and the file extension. Additionally, according to the EPS file specification, EPS files are intended to be single-paged. By default, the PS format is utilized.
page_size parameter specifies the size of the pages in the PS document. However, it is possible to assign different page sizes for each page if necessary.
The page size can be obtained from PageConstants class as demonstrated in the following example:
1options.page_size = PageConstants.get_size(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.get_margins(PageConstants.MARGINS_SMALL)); // 20 points for each margin
Default margins are “ZERO” (0, 0, 0, 0).
- back_ground_color specifies a color of page’s background. Can be assigned as:
1options.back_ground_color = aspose.pydrawing.Color(211, 8, 48);
or:
1options.back_ground_color = aspose.pydrawing.Color.YELLOW;
Default value is “null” that means no background.
embed_fonts parameter determines the output format of documents, which can be either PS or EPS. In the Aspose.Page library, the distinctions between these two formats are primarily reflected in PostScript comments and the file extension. Additionally, according to the EPS file specification, EPS files are intended to be single-paged. By default, the PS format is utilized.
embed_fonts_as parameter specifies the size of the pages in the PS document. However, it is possible to assign different page sizes for each page if necessary. The value can be set with help of FontConstants class like the following:
1options.embed_fonts_as = FontsConstants.EMBED_FONTS_TYPE3;
The default value is “TrueType”.
jpeg_quality_level parameter determines the compression level and quality of images within the resulting PS document. Higher quality requires less compression, and vice versa. Quality ranges from 0 to 100, with 0 representing minimal quality and 100 representing maximal quality. The default setting is 75.
additional_fonts_folder parameter specifies locations where fonts can be found. By default, system font folders are always included.
debug enables the output of debug information to the console. The default value is false.
You can download examples and data files from GitHub.