Most PDF readers (like Adobe Acrobat or Foxit) allow users to organize PDF pages. For example, inserting, deleting, reordering, rotating and extracting pages. This article will introduce how to implement these functionalities programmatically in C# and VB.NET.
The following topics will be covered in this article:
- Insert Pages into a PDF Document
- Delete Pages from a PDF Document
- Reorder Pages in a PDF Document
- Rotate Pages in a PDF Document
- Extract Pages from a PDF Document
Installation
Spire.PDF for .NET is used here to organize PDF pages. It’s a commercial library for creating, editing, converting and printing PDF documents. A free version of Spire.PDF for .NET is also available, but it should be pointed out that it’s limited to 10 pages while loading PDF files.
You can install either of them via NuGet:
Commercial version: Install-Package Spire.PDF
Free version: Install-Package FreeSpire.PDF
Insert Pages into a PDF Document in C# and VB.NET
Spire.PDF offers a PdfDocument.Pages.Insert(pageIndex) method to insert a page at a specified location of a PDF document. If you want to add a page to the end the document, use the PdfDocument.Pages.Add() method instead.
The following steps show how to insert a page and add a page to a PDF document:
- Create an instance of PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Insert a blank page at the specified location of the document using PdfDocument.Pages.Insert(pageIndex) method.
- Add a Page to the end of the document using PdfDocument.Pages.Add() method.
- Save the result document using PdfDocument.SaveToFile() method.
C#
using Spire.Pdf;
namespace InsertPages
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.LoadFromFile("Sample.Pdf");
//Insert a page at the first index position
pdf.Pages.Insert(0);
//Add a page to the end of the document
pdf.Pages.Add();
//Save the result document
pdf.SaveToFile("InsertPages.pdf");
}
}
}
VB.NET
Imports Spire.Pdf
Namespace InsertPages
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a PdfDocument instance
Dim pdf As PdfDocument = New PdfDocument()
'Load a PDF document
pdf.LoadFromFile("Sample.Pdf")
'Insert a page at the first index position
pdf.Pages.Insert(0)
'Add a page to the end of the document
pdf.Pages.Add()
'Save the result document
pdf.SaveToFile("InsertPages.pdf")
End Sub
End Class
End Namespace
Delete Pages from a PDF Document in C# and VB.NET
The PdfDocument.Pages.RemoveAt(pageIndex) method is used to remove a specific page from a PDF document.
The following are the steps to remove a page from a PDF document:
- Create an instance of PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Delete a specific page by page index using PdfDocument.Pages.RemoveAt(pageIndex) method.
- Save the result document using PdfDocument.SaveToFile() method.
C#
using Spire.Pdf;
namespace DeletePages
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.LoadFromFile("Sample.Pdf");
//Delete the first page by page index
pdf.Pages.RemoveAt(0);
//Save the result document
pdf.SaveToFile("DeletePages.pdf");
}
}
}
VB.NET
Imports Spire.Pdf
Namespace DeletePages
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a PdfDocument instance
Dim pdf As PdfDocument = New PdfDocument()
'Load a PDF document
pdf.LoadFromFile("Sample.Pdf")
'Delete the first page by page index
pdf.Pages.RemoveAt(0)
'Save the result document
pdf.SaveToFile("DeletePages.pdf")
End Sub
End Class
End Namespace
Reorder Pages in a PDF Document in C# and VB.NET
To reorder or rearrange the pages in a PDF document, you can use the PdfDocument.Pages.ReArrange() method.
The following steps show how to reorder pages in a PDF document:
- Create an instance of PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Create an int array for new page order.
- Reorder the pages in the document using PdfDocument.Pages.ReArrange(orderArray) method.
- Save the result document using PdfDocument.SaveToFile() method.
C#
using Spire.Pdf;
namespace ReorderPages
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.LoadFromFile("Sample.Pdf");
//Create an int array for new page order
//Original page order: 0, 1, 2, 3. Changed page order: 1, 0, 2, 3
int[] orderArray = new int[] { 1, 0, 2, 3 };
//Reorder the pages
pdf.Pages.ReArrange(orderArray);
//Save the result document
pdf.SaveToFile("ReorderPages.pdf");
}
}
}
VB.NET
Imports Spire.Pdf
Namespace ReorderPages
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a PdfDocument instance
Dim pdf As PdfDocument = New PdfDocument()
'Load a PDF document
pdf.LoadFromFile("Sample.Pdf")
'Create an int array for new page order
'Original page order: 0, 1, 2, 3. Changed page order: 1, 0, 2, 3
Dim orderArray = New Integer() {1, 0, 2, 3}
'Reorder the pages
pdf.Pages.ReArrange(orderArray)
'Save the result document
pdf.SaveToFile("ReorderPages.pdf")
End Sub
End Class
End Namespace
Rotate Pages in a PDF Document in C# and VB.NET
The PdfPageBase.Rotation property is used to get or set the rotation angle of a PDF page. You can rotate a page by 0/90/180/270 degrees.
The following are the steps to rotate a page in a PDF document:
- Create an instance of PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Get the desired page by page index through PdfDocument.Pages[pageIndex] property.
- Get the original rotation angle of the page through PdfPageBase.Rotation property.
- Increase the original rotation angle by desired degrees.
- Apply the new rotation angle to the page through PdfPageBase.Rotation property.
- Save the result document using PdfDocument.SaveToFile() method.
C#
using Spire.Pdf;
namespace RotatePages
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.LoadFromFile("Sample.pdf");
//Get the first page
PdfPageBase page = pdf.Pages[0];
//Get the original rotation angle of the page
int rotation = (int)page.Rotation;
//Rotate the page 180 degrees clockwise based on the original rotation angle
rotation += (int)PdfPageRotateAngle.RotateAngle180;
page.Rotation = (PdfPageRotateAngle)rotation;
//Save the result document
pdf.SaveToFile("RotatePages.pdf");
}
}
}
VB.NET
Imports Spire.Pdf
Namespace RotatePages
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a PdfDocument instance
Dim pdf As PdfDocument = New PdfDocument()
'Load a PDF document
pdf.LoadFromFile("Sample.pdf")
'Get the first page
Dim page As PdfPageBase = pdf.Pages(0)
'Get the original rotation angle of the page
Dim rotation = CInt(page.Rotation)
'Rotate the page 180 degrees clockwise based on the original rotation angle
rotation += CInt(PdfPageRotateAngle.RotateAngle180)
page.Rotation = CType(rotation, PdfPageRotateAngle)
'Save the result document
pdf.SaveToFile("RotatePages.pdf")
End Sub
End Class
End Namespace
Extract Pages from a PDF Document in C# and VB.NET
You can extract a specific page or a range of pages from a PDF document to another by using the PdfDocument.InsertPage(PdfDocument, pageIndex) method or the PdfDocument.InsertPageRange(PdfDocument, startPageIndex, endPageIndex) method.
The following steps show how to extract a page or a range of pages from an existing PDF document to a new PDF document:
- Create an instance of PdfDocument class.
- Load a PDF document using PdfDocument.LoadFromFile() method.
- Create an instance of PdfDocument class.
- Extract a specific page or a range of pages from the source PDF document to the new PDF document using PdfDocument.InsertPage(PdfDocument, pageIndex) method or PdfDocument.InsertPageRange(PdfDocument, startPageIndex, endPageIndex) method.
- Save the new PDF document using PdfDocument.SaveToFile() method.
C#
using Spire.Pdf;
namespace ExtractPages
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a PDF document
pdf.LoadFromFile("Sample.Pdf");
//Create a new PDF
PdfDocument newPdf = new PdfDocument();
//Extract the first page of the source PDF to the new PDF
newPdf.InsertPage(pdf, 0);
//Extract a range of pages (page 1 - 4) of the source PDF to the new PDF
//newPdf.InsertPageRange(pdf, 0, 3);
//Save the new PDF document to file
newPdf.SaveToFile("ExtractPages.pdf");
}
}
}
VB.NET
Imports Spire.Pdf
Namespace ExtractPages
Friend Class Program
Private Shared Sub Main(ByVal args As String())
'Create a PdfDocument instance
Dim pdf As PdfDocument = New PdfDocument()
'Load a PDF document
pdf.LoadFromFile("Sample.Pdf")
'Create a new PDF
Dim newPdf As PdfDocument = New PdfDocument()
'Extract the first page of the source PDF to the new PDF
newPdf.InsertPage(pdf, 0)
'Extract a range of pages (page 1 - 4) of the source PDF to the new PDF
'newPdf.InsertPageRange(pdf, 0, 3);
'Save the new PDF document to file
newPdf.SaveToFile("ExtractPages.pdf")
End Sub
End Class
End Namespace