Remove Specific PDF Pages
To remove single or multiple pages from a PDF document, please use the removePage
method. The edited PDF document can then be exported using the saveAs
method of IronPDF, a powerful PDF library for .NET that allows you to create, edit, and manipulate PDF files programmatically. You can learn more about the features of IronPDF on the IronPDF official website.
// Import necessary namespaces
using IronPdf;
public class PdfEditor
{
public static void Main(string[] args)
{
// Specify the path to your PDF file
var inputPath = "input.pdf";
// Load the PDF into an IronPdf.PdfDocument object
PdfDocument pdf = PdfDocument.FromFile(inputPath);
// Example: Remove page 1 from the PDF
// Page numbers are zero-based index
pdf.RemovePage(0);
// Save the modified PDF to a new file
var outputPath = "output.pdf";
pdf.SaveAs(outputPath);
// Inform the user that the process is complete
Console.WriteLine("Page removed and new PDF saved as " + outputPath);
}
}
// Import necessary namespaces
using IronPdf;
public class PdfEditor
{
public static void Main(string[] args)
{
// Specify the path to your PDF file
var inputPath = "input.pdf";
// Load the PDF into an IronPdf.PdfDocument object
PdfDocument pdf = PdfDocument.FromFile(inputPath);
// Example: Remove page 1 from the PDF
// Page numbers are zero-based index
pdf.RemovePage(0);
// Save the modified PDF to a new file
var outputPath = "output.pdf";
pdf.SaveAs(outputPath);
// Inform the user that the process is complete
Console.WriteLine("Page removed and new PDF saved as " + outputPath);
}
}
' Import necessary namespaces
Imports IronPdf
Public Class PdfEditor
Public Shared Sub Main(ByVal args() As String)
' Specify the path to your PDF file
Dim inputPath = "input.pdf"
' Load the PDF into an IronPdf.PdfDocument object
Dim pdf As PdfDocument = PdfDocument.FromFile(inputPath)
' Example: Remove page 1 from the PDF
' Page numbers are zero-based index
pdf.RemovePage(0)
' Save the modified PDF to a new file
Dim outputPath = "output.pdf"
pdf.SaveAs(outputPath)
' Inform the user that the process is complete
Console.WriteLine("Page removed and new PDF saved as " & outputPath)
End Sub
End Class
Explanation:
IronPdf: The code utilizes IronPdf, a .NET library for PDF manipulation. Make sure to have this library installed in your project.
PdfDocument.FromFile(inputPath): This loads the PDF from the given file path into a
PdfDocument
object.RemovePage(0): This method is called on the
PdfDocument
object to remove the first page of the PDF. Page numbering starts with 0, so0
represents the first page.SaveAs(outputPath): Any changes made to the
PdfDocument
object are saved to a new file specified byoutputPath
.- Console.WriteLine: Prints a completion message to the console, indicating the location of the modified PDF.
Keep in mind to replace "input.pdf"
and "output.pdf"
with your actual file paths.