Add Pages to PDF Document
Aspose.PDF for .NET API provides full flexibility to work with pages in a PDF document using C# or any other .NET language. It maintains all the pages of a PDF document in PageCollection that can be used to work with PDF pages. Aspose.PDF for .NET lets you insert a page to a PDF document at any location in the file as well as add pages to the end of a PDF file. This section shows how to add pages to a PDF using C#.
Add or Insert Page in a PDF File
Aspose.PDF for .NET lets you insert a page to a PDF document at any location in the file as well as add pages to the end of a PDF file.
The following code snippet also work with Aspose.PDF.Drawing library.
Insert Empty Page in a PDF File at Desired Location
To insert an empty page in a PDF file:
- Create a Document class object with the input PDF file.
- Call the PageCollection collection’s Insert method with specified index.
- Save the output PDF using the Save method.
The following code snippet shows you how to insert a page in a PDF file.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void InsertAnEmptyPage()
{
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_AsposePdf_Pages();
// Open document
using (var document = new Aspose.Pdf.Document(dataDir + "InsertEmptyPage.pdf"))
{
// Insert an empty page in a PDF
document.Pages.Insert(2);
// Save output file
document.Save(dataDir + "InsertEmptyPage_out.pdf");
}
}
In example above, we added empty page with default parameters. If you need to make page size the same as another page in document you should add a few lines of code:
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void InsertAnEmptyPageWithParameters()
{
var page = document.Pages.Insert(2);
//copy page parameters from page 1
page.ArtBox = document.Pages[1].ArtBox;
page.BleedBox = document.Pages[1].BleedBox;
page.CropBox = document.Pages[1].CropBox;
page.MediaBox = document.Pages[1].MediaBox;
page.TrimBox = document.Pages[1].TrimBox;
}
Add an Empty Page at the End of a PDF File
Sometimes, you want to ensure that a document ends on an empty page. This topic explains how to insert an empty page at the end of the PDF document.
To insert an empty page at the end of a PDF file:
- Create a Document class object with the input PDF file.
- Call the PageCollection collection’s Add method, without any parameters.
- Save the output PDF using the Save method.
The following code snippet shows you how to insert an empty page at the end of a PDF file.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void InsertAnEmptyPageAtTheEnd()
{
// The path to the documents directory
string dataDir = RunExamples.GetDataDir_AsposePdf_Pages();
// Open document
using (var document = new Aspose.Pdf.Document(dataDir + "InsertEmptyPageAtEnd.pdf"))
{
// Insert an empty page at the end of a PDF file
document.Pages.Add();
// Save output file
document.Save(dataDir + "InsertEmptyPageAtEnd_out.pdf");
}
}