Working with Pages in PS file | Python
Contents
[
Hide
Show
]Add Pages to PS Document
Aspose.Page for Python via .NET provides two methods for adding pages to a PsDocument object.
The following code snippet demonstrates how to create a 2-page PS document in 8 steps:
- Create an output stream for the resulting PS file.
- Instantiate a PsSaveOptions object with default options.
- Create a 2-page PsDocument using the previously created output stream and save options.
- Open the first page with the default page size of the document (A4 in Portrait orientation).
- Close the page.
- Open the second page with a new size.
- Close the page.
- Save the document.
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_pages()
3
4# Create an output stream for the PostScript document
5with open(data_dir + "document1.ps", "wb") as out_ps_stream:
6 # Create save options with A4 size
7 options = PsSaveOptions()
8
9 # Create a new 2-paged PS Document
10 document = PsDocument(out_ps_stream, options, 2)
11
12 # Add the first page
13 document.open_page(None)
14
15 # Add content
16
17 # Close the first page
18 document.close_page()
19
20 # Add the second page with a different size
21 document.open_page(400, 700)
22
23 # Add content
24
25 # Close the second page
26 document.close_page()
27
28 # Save the document
29 document.save()
The following code snippet also creates a 2-paged PS document, but there are 7 steps to be taken:
- Create an output stream for the resulting PS file.
- Initiate PsSaveOptions object with default options.
- Create a multi-paged PsDocument with the already created output stream and save options. In this case, the first page is already opened and its size is the default page size of the document (A4 in Portrait orientation).
- Close the page.
- Open the second page with a new size.
- Close the page.
- Save the document. This way of adding pages is useful when the document has 1 page or it is unknown if it will be a 1- or 2-paged document.
1# The path to the documents directory.
2data_dir = Util.get_data_dir_working_with_pages()
3
4# Create an output stream for PostScript document
5with open(data_dir + "document2.ps", "wb") as out_ps_stream:
6 # Create save options with A4 size
7 options = PsSaveOptions()
8
9 # Set a variable that indicates if resulting PostScript document will be multipaged
10 multi_paged = True
11
12 # Create new multipaged PS Document with one page opened
13 document = PsDocument(out_ps_stream, options, multi_paged)
14
15 # Add content
16
17 # Close the first page
18 document.close_page()
19
20 # Add the second page with different size
21 document.open_page(500, 300)
22
23 # Add content
24
25 # Close the second page
26 document.close_page()
27
28 # Save the document
29 document.save()
You can download examples and data files from GitHub.