Insert page into PS file
C++ API Solution to add pages to PS file
There are two ways to control how many pages your PostScript (PS) document has with the Aspose.Page API. You can either specify the exact number of pages upfront or choose to create a single-page or multi-page document. However, to add pages one by one, you must use the OpenPage() and ClosePage() methods. PS documents can have pages of different sizes, so you can set the size you want with OpenPage(). By default, pages are A4 size. It's important to note that you can only manage pages in newly created PS documents, not existing ones.
On this page, we show you two options for adding pages to a PS file using Aspose.Page for C++. But first, you need to:
Get Aspose.Page for C++ API which is a feature-rich, powerful and easy-to-use document manipulation and conversion C++ API.
Open the NuGet package manager, and search for Aspose.Page.Cpp and install. You may also use the following command from the Package Manager Console.
Package Manager Console Command
PM> Install-Package Aspose.Page.Cpp
C++ code to add pages to a PS file. Variant 1.
Steps to add pages to a PS file. Variant 1.
- Construct the path to the documents directory.
- Create an output stream for the resulting PS file.
- Create the save options with A4 size
- Create a PS file using the PsDocument Class , the outputstream and the number of pages.
- Create an empty page with the OpenPage() Method.
- App the content to this page and close it with the ClosePage() Method.
- If you need to add a page with a different size use the same OpenPage() Method with new size.
- Close the page with the ClosePage() Method.
- Save the PS document using the Save() Method.
C++ code to add pages to a PS file. Variant 1.
// The path to the documents directory.
System::String dataDir = RunExamples::GetDataDir_WorkingWithPages();
//Create output stream for PostScript document
{
System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"document1.ps", System::IO::FileMode::Create);
// Clearing resources under 'using' statement
System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream});
// ------------------------------------------
try
{
//Create save options with A4 size
System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
// Create new 2-paged PS Document
System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, 2);
//Add the first page
document->OpenPage();
//Add content
//Close the first page
document->ClosePage();
//Add the second page with different size
document->OpenPage(400.0f, 700.0f);
//Add content
//Close the second page
document->ClosePage();
//Save the document
document->Save();
}
catch(...)
{
__dispose_guard_0.SetCurrentException(std::current_exception());
}
}C++ code to add pages to a PS file. Variant 2.
Steps to add pages to a PS file. Variant 2.
- Construct the path to the documents directory.
- Create an output stream for the resulting PS file.
- Create the save options with A4 size
- Create a new PS file using the PsDocument Class .
- If you need to add a page with different size use OpenPage() Method with new size.
- After adding content to pages close them with the ClosePage() Method.
- Save the PS document using the Save() Method.
C++ code to add pages to a PS file. Variant 2.
// The path to the documents directory.
System::String dataDir = RunExamples::GetDataDir_WorkingWithPages();
//Create output stream for PostScript document
{
System::SharedPtr<System::IO::Stream> outPsStream = System::MakeObject<System::IO::FileStream>(dataDir + u"document2.ps", System::IO::FileMode::Create);
// Clearing resources under 'using' statement
System::Details::DisposeGuard<1> __dispose_guard_0({ outPsStream});
// ------------------------------------------
try
{
//Create save options with A4 size
System::SharedPtr<PsSaveOptions> options = System::MakeObject<PsSaveOptions>();
//Set variable that indicates if resulting PostScript document will be multipaged
bool multiPaged = true;
// Create new multipaged PS Document with one page opened
System::SharedPtr<PsDocument> document = System::MakeObject<PsDocument>(outPsStream, options, multiPaged);
//Add content
//Close the first page
document->ClosePage();
//Add the second page with different size
document->OpenPage(500.0f, 300.0f);
//Add content
//Close the second page
document->ClosePage();
//Save the document
document->Save();
}
catch(...)
{
__dispose_guard_0.SetCurrentException(std::current_exception());
}
}
PS What is PS File Format
PS (PostScript) is a powerful page‑description language that combines text and graphics in a device‑independent way. It is widely supported by printers and professional image editors. PS files contain the rendering instructions for the page and can be converted to PDF, EPS, or raster formats.