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.
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.
9data_dir = Util.get_data_dir_working_with_pages()
10
11# Create an output stream for the PostScript document
12with open(data_dir + "document1.ps", "wb") as out_ps_stream:
13 # Create save options with A4 size
14 options = PsSaveOptions()
15
16 # Create a new 2-paged PS Document
17 document = PsDocument(out_ps_stream, options, 2)
18
19 # Add the first page
20 document.open_page(None)
21
22 # Add content
23
24 # Close the first page
25 document.close_page()
26
27 # Add the second page with a different size
28 document.open_page(400, 700)
29
30 # Add content
31
32 # Close the second page
33 document.close_page()
34
35 # Save the document
36 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.
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.
9data_dir = Util.get_data_dir_working_with_pages()
10
11# Create an output stream for PostScript document
12with open(data_dir + "document2.ps", "wb") as out_ps_stream:
13 # Create save options with A4 size
14 options = PsSaveOptions()
15
16 # Set a variable that indicates if resulting PostScript document will be multipaged
17 multi_paged = True
18
19 # Create new multipaged PS Document with one page opened
20 document = PsDocument(out_ps_stream, options, multi_paged)
21
22 # Add content
23
24 # Close the first page
25 document.close_page()
26
27 # Add the second page with different size
28 document.open_page(500, 300)
29
30 # Add content
31
32 # Close the second page
33 document.close_page()
34
35 # Save the document
36 document.save()
You can download examples and data files from GitHub.