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:

  1. Create an output stream for the resulting PS file.
  2. Instantiate a PsSaveOptions object with default options.
  3. Create a 2-page PsDocument using the previously created output stream and save options.
  4. Open the first page with the default page size of the document (A4 in Portrait orientation).
  5. Close the page.
  6. Open the second page with a new size.
  7. Close the page.
  8. 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()

See working with the pages in PS documents in .NET, Java.

The following code snippet also creates a 2-paged PS document, but there are 7 steps to be taken:

  1. Create an output stream for the resulting PS file.
  2. Initiate PsSaveOptions object with default options.
  3. 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).
  4. Close the page.
  5. Open the second page with a new size.
  6. Close the page.
  7. 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.

Have any questions about Aspose.Page?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.