在 PostScript 中处理文档 | C++
创建 PS 文档
Aspose.Page for C++ 提供了两个构造函数来创建 PsDocument 类。以下代码片段用于创建一个单页 PS 文档:
1 // The path to the documents directory.
2 System::String dir = RunExamples::GetDataDir_WorkingWithDocument();
3
4 //Create output stream for PostScript document
5 {
6 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dir + u"document.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
14 System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
15 //If you want to aassign page size other than A4, set page size in options
16 options->set_PageSize(PageConstants::GetSize(PageConstants::SIZE_A4(), PageConstants::ORIENTATION_PORTRAIT()));
17 //If you want to aassign page margins other empty, set page margins in options
18 options->set_Margins(PageConstants::GetMargins(PageConstants::MARGINS_ZERO()));
19 //If you plan to use fonts that located in non system folders, set additional fonts folders in options
20 options->set_AdditionalFontsFolders(System::MakeArray<System::String>({dir}));
21
22 //Set variable that indicates if resulting PostScript document will be multipaged
23 bool multiPaged = false;
24
25 // Create new multipaged PS Document with one page opened
26 System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, multiPaged);
27
28 //Close current page
29 document->ClosePage();
30 //Save the document
31 document->Save();
32 }
33 catch(...)
34 {
35 __dispose_guard_0.SetCurrentException(std::current_exception());
36 }
37 }
如果 PS 文档计划为多页格式,请将 multiPaged 变量设置为 true。
另一个构造函数允许创建具有指定页数的 PsDocument 对象:
1//Create output stream for PostScript document
2using (Stream outPsStream = new FileStream(dir + "document.ps", FileMode.Create))
3{
4 //Create save options
5 PsSaveOptions options = new PsSaveOptions();
6
7 // Create new multipaged PS Document with 2 pages. These two pages are not created. It must be added by AddPage() method.
8 PsDocument document = new PsDocument(outPsStream, options, 2);
9
10 //Adding pages and it's content
11
12 //Save the document
13 document.Save();
14}
15 // The path to the documents directory.
16 System::String dir = RunExamples::GetDataDir_WorkingWithDocument();
17
18 //Create output stream for PostScript document
19 {
20 System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dir + u"document.ps", System::IO::FileMode::Create);
21 // Clearing resources under 'using' statement
22 System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream});
23 // ------------------------------------------
24
25 try
26 {
27 //Create save options
28 System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
29
30 // Create new multipaged PS Document with 2 pages. These two pages are not created. It must be added by AddPage() method.
31 System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, 2);
32
33 //Adding pages and it's content
34
35 //Save the document
36 document->Save();
37 }
38 catch(...)
39 {
40 __dispose_guard_0.SetCurrentException(std::current_exception());
41 }
42 }
请参阅在 Java 中如何使用 PS 文档。
我们来看一下 PsSaveOptions 类,它封装了创建正确 PostScript 文档所需的选项。
SaveFormat 指定文档的输出格式。可以是 PS 或 EPS。在 Aspose.Page 库中,这两种格式的区别仅在于 PostScript 注释和文件扩展名。 此外,根据 EPS 文件规范,EPS 文件应为单页文件。默认使用 PS 格式。
PageSize 指定 PS 文档中页面的大小。但是,如果需要,我们可以为每个页面分配不同的页面大小。页面大小可以从 PageConstants 类中获取,如下例所示:
1 options->set_PageSize(PageConstants::GetSize(PageConstants::SIZE_International(), PageConstants::ORIENTATION_PORTRAIT()));
默认页面尺寸为“纵向”方向的“A4”。
- 边距 指定页面边界与页面内容的左、上、右和下边缘之间的空白字段。该值可从 PageConstants 类获取。
1 options->set_Margins(PageConstants::GetMargins(PageConstants::MARGINS_SMALL())); // 20 points for each margin
Default margins are “ZERO” (0, 0, 0, 0).
- BackGroundColor specifies a color of page’s background. Can be assigned as:
1 options->set_BackgroundColor(Color::FromArgb(211, 8, 48));
或者:
1 options->set_BackgroundColor(System::Drawing::Color::get_Yellow());
默认值为“null”,表示无背景。
EmbedFonts 控制 PsDocument 在保存到文件时的行为。如果设置为“false”,则使用的字体将不会写入 PS 文件中。在这种情况下,如果在目标主机的系统文件夹中找不到使用的字体,PostScript 解释器将抛出错误。
EmbedFontsAs 控制字体在 PS 文件中的嵌入方式。目前有两种方式适用于 TrueType 和 Type3 字体格式。该值可以通过 FontConstants 类进行设置,如下所示:
1 options->set_EmbedFontsAs(FontsConstants::EMBED_FONTS_TYPE3);
默认值为“TrueType”。
JpegQualityLevel 指定生成的 PS 文档中图像的压缩/质量级别。质量要求越高,压缩率越低,反之亦然。最低质量为 0,最高质量为 100。默认值为 75。
AdditionalFontsFolder 指定字体的存放位置。默认情况下,始终包含系统字体文件夹。
Debug 允许将调试信息输出到控制台。默认值为 false。
您可以从 GitHub 下载示例和数据文件。