Working with Pages in PS file | Java

Contents
[ Hide Show ]

Add Pages to PS Document

Aspose.Page for Java offers two ways of adding pages to PsDocument object.
The following code snippet creates a 2-paged PS document in 8 steps:

  1. Create an output stream for the resulting PS file.
  2. Create PsSaveOptions object with default options.
  3. Create a 2-paged PsDocument with an already 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.
 1//Create an output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddPages1_outPS.ps");
 3//Create a save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6// Create new 2-paged PS Document
 7PsDocument document = new PsDocument(outPsStream, options, 2);
 8
 9//Add the first page with the document's page size
10document.openPage(null);
11
12//Add content
13
14//Close the first page
15document.closePage();
16
17//Add the second page with the different size
18document.openPage(400, 700);
19
20//Add content
21
22//Close current page
23document.closePage();
24//Save the document
25document.save();

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

The following code snippet creates also a 2-paged PS document, but with 7 steps:

  1. Create an output stream for the resulting PS file.
  2. Create PsSaveOptions object with default options.
  3. Create multi-paged PsDocument with 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.
 1//Create output stream for PostScript document
 2FileOutputStream outPsStream = new FileOutputStream(dataDir + "AddPages2_outPS.ps");
 3//Create save options with A4 size
 4PsSaveOptions options = new PsSaveOptions();
 5
 6//Set variable that indicates if resulting PostScript document will be multipaged
 7boolean multiPaged = true;
 8
 9// Create new multipaged PS Document with one page opened
10PsDocument document = new PsDocument(outPsStream, options, multiPaged);
11
12//Add content
13
14//Close the first page
15document.closePage();
16
17//Add the second page with the different size
18document.openPage(500, 300);
19
20//Add content
21
22//Close current page
23document.closePage();
24//Save the document
25document.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.