Working with Pages in PostScript | C++
Contents
[
Hide
Show
]Add Pages to PS Document
Aspose.Page for C++ offers two ways of adding pages to PsDocument object.
The following code snippet creates a 2-paged PS document in 8 steps:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions object with default options.
- Create a 2-paged PsDocument with an already 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.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithPages();
3
4 //Create output stream for PostScript document
5 {
6 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"document1.ps", System::IO::FileMode::Create);
7 // Clearing resources under 'using' statement
8 System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream});
9 // ------------------------------------------
10
11 try
12 {
13 //Create save options with A4 size
14 System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15
16 // Create new 2-paged PS Document
17 System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, 2);
18
19 //Add the first page
20 document->OpenPage();
21
22 //Add content
23
24 //Close the first page
25 document->ClosePage();
26
27 //Add the second page with different size
28 document->OpenPage(400.0f, 700.0f);
29
30 //Add content
31
32 //Close the second page
33 document->ClosePage();
34
35 //Save the document
36 document->Save();
37 }
38 catch(...)
39 {
40 __dispose_guard_0.SetCurrentException(std::current_exception());
41 }
42 }
The following code snippet creates also a 2-paged PS document, but with 7 steps:
- Create an output stream for the resulting PS file.
- Create PsSaveOptions object with default options.
- 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).
- 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.
2 System::String dataDir = RunExamples::GetDataDir_WorkingWithPages();
3
4 //Create output stream for PostScript document
5 {
6 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"document2.ps", System::IO::FileMode::Create);
7 // Clearing resources under 'using' statement
8 System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream});
9 // ------------------------------------------
10
11 try
12 {
13 //Create save options with A4 size
14 System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15
16 //Set variable that indicates if resulting PostScript document will be multipaged
17 bool multiPaged = true;
18
19 // Create new multipaged PS Document with one page opened
20 System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, multiPaged);
21
22 //Add content
23
24 //Close the first page
25 document->ClosePage();
26
27 //Add the second page with different size
28 document->OpenPage(500.0f, 300.0f);
29
30 //Add content
31
32 //Close the second page
33 document->ClosePage();
34
35 //Save the document
36 document->Save();
37 }
38 catch(...)
39 {
40 __dispose_guard_0.SetCurrentException(std::current_exception());
41 }
42 }
You can download examples and data files from GitHub.