Insert page into PS file
.NET API Solution to add pages to PS file
PS documents may have many pages. Aspose.Page API solution offers two ways to manage the number of pages in the PS file. In the first variant, we just set up the number of pages in the constructor of PsDocument. In the second one instead of the number of pages we provide PsDocument's constructor with a boolean value that indicates whether it will be a one- or multiple-paged PS document. However, we should use the OpenPage() and ClosePage() methods to add pages to the document explicitly. PostScript supports multi-sized pages in its content, so we can call the OpenPage() with the necessary size. The default page size is A4 and is set up by PsSaveOptions. Page management is possible only on just-created PsDocument.
Add pages to PS documents we need:
Aspose.Page for .NET API which is a feature-rich, powerful and easy-to-use document manipulation and conversion API for C# platform.
Open the NuGet package manager, and search for Aspose.Page and install. You may also use the following command from the Package Manager Console.
Package Manager Console Command
PM> Install-Package Aspose.Page
Steps to add pages to a PS file. Variant 1.
- Create an output stream for the resulting PS file.
- Create a PS file using the PsDocument Class , the outputstream and the number of pages.
- Create an empty page with the OpenPage() method.
- Close the page 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.
using Aspose.Page.EPS;
using Aspose.Page.EPS.Device;
using System.Drawing;
using System.IO;
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_WorkingWithPages();
//Create output stream for PostScript document
using (Stream outPsStream = new FileStream(dataDir + "document1.ps", FileMode.Create))
{
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
// Create new 2-paged PS Document
PsDocument document = new 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, 700);
//Add content
//Close the second page
document.ClosePage();
//Save the document
document.Save();
}
Steps to add pages to a PS file. Variant 2.
- Create an output stream for the resulting PS file.
- Create a PS file using the PsDocument Class , the outputstream and multipaged boolean value.
- Close the page with the ClosePage() method.
- If you need to add a page with different size use 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 2.
string dataDir = RunExamples.GetDataDir_WorkingWithPages();
//Create output stream for PostScript document
using (Stream outPsStream = new FileStream(dataDir + "document2.ps", FileMode.Create))
{
//Create save options with A4 size
PsSaveOptions options = new PsSaveOptions();
//Set variable that indicates if resulting PostScript document will be multipaged
bool multiPaged = true;
// Create new multipaged PS Document with one page opened
PsDocument document = new PsDocument(outPsStream, options, multiPaged);
//Add content
//Close the first page
document.ClosePage();
//Add the second page with different size
document.OpenPage(500, 300);
//Add content
//Close the second page
document.ClosePage();
//Save the document
document.Save();
}
FAQ
1. Is it possible to add pages in a PostScript (PS) document?
Aspose.Page lets you do this when you create a new PS file. But when you have PS files created, it’s not directly possible to add or delete pages there because PostScript is primarily a page description language, and once the content is rendered, it’s fixed.
2. What are the other options for managing the number of pages in a PS file?
You can convert PostScript files to other document formats like PDF using tools such as Adobe Acrobat or Ghostscript, where page manipulation is supported. After making changes to the PDF document, then convert it back to PostScript if necessary.
3. Can I add a page of a different size to the PS file?
Yes, you can do this within a newly created file using the OpenPage() Method.
PS What is PS File Format
PS format is one of the page description language (PDL) formats. It is capable to contain graphic as well as text information on the page. That is why the format was supported by most of the programs for image editing. The postscript file itself is a kind of instruction for printers. It contains information on what and how to print from its page.