在 PostScript 中使用页面 | C++
Contents
[
Hide
Show
]向 PS 文档添加页面
Aspose.Page for C++ 提供了两种向 PsDocument 对象添加页面的方法。
以下代码片段通过 8 个步骤创建一个 2 页的 PS 文档:
- 为生成的 PS 文件创建输出流。
- 使用默认选项创建 PsSaveOptions 对象。
- 使用已创建的输出流和保存选项创建一个 2 页的 PsDocument。
- 使用文档的默认页面尺寸(纵向 A4 尺寸)打开第一页。
- 关闭页面。
- 使用新的尺寸打开第二页。
- 关闭页面。
- 保存文档。
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 }
以下代码片段同样创建了一个两页的 PS 文档,但包含 7 个步骤:
- 为生成的 PS 文件创建输出流。
- 使用默认选项创建 PsSaveOptions 对象。
- 使用已创建的输出流和保存选项创建多页 PsDocument。在本例中,第一页已打开,其大小为文档的默认页面大小(纵向 A4 尺寸)。
- 关闭页面。
- 以新的大小打开第二页。
- 关闭页面。
- 保存文档。 当文档只有 1 页或不确定是 1 页还是 2 页时,这种添加页面的方式非常有用。
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 }
您可以从 GitHub下载示例和数据文件。