Working with Document in PostScript | C++
Create PS Document
Aspose.Page for C++ offers two constructors in order to create PsDocument class. The following code snippet creates a 1-paged PS document:
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 }
If PS document is planning to be multi-paged, set multiPaged variable to true.
Another constructor allows creating PsDocument object with a defined number of pages:
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 }
See working with PS document in Java.
Let’s look at the PsSaveOptions class that encapsulates possible options helping to create the right PostScript document.
SaveFormat specifies an output format of documents. Can be PS or EPS. In Aspose.Page library differences between these two formats are reflected only in PostScript comments and a file extension. Also in accordance with the EPS file specification EPS files should be 1-paged. PS format is used by default.
PageSize specifies the size of the pages in the PS document. However, we can assign different page sizes for every page if it is required. The page size can be obtained from PageConstants class like in following example:
1 options->set_PageSize(PageConstants::GetSize(PageConstants::SIZE_International(), PageConstants::ORIENTATION_PORTRAIT()));
The default page size is “A4” in “Portrait” orientation.
- Margins specifies blank fields between the page boundaries and left, up, right, and bottom edges of the page content. It can be obtained from PageConstants class.
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));
or:
1 options->set_BackgroundColor(System::Drawing::Color::get_Yellow());
Default value is “null” that means no background.
EmbedFonts controls the behavior of PsDocument while saving it to the file. If “false”, used fonts will not be written in the PS file. In this case, the PostScript interpreter will throw an error if the used font cannot be found in system folders on the target host.
EmbedFontsAs controls the way how fonts will be embedded in the PS file. At this moment two ways work TrueType and Type3 font formats. The value can be set with help of FontConstants class like the following:
1 options->set_EmbedFontsAs(FontsConstants::EMBED_FONTS_TYPE3);
The default value is “TrueType”.
JpegQualityLevel specifies a level of compression/quality of images in the resulting PS document. The more quality is required the less compression will be and vice versa. Minimal quality is 0, and maximal - 100. The default is 75.
AdditionalFontsFolder specifies locations where to find fonts. System font folders are always included by default.
Debug allows outputting debug information to the console. The default value is false.
You can download examples and data files from GitHub.