Deleting pages of a PDF with iText7 and C#
It's easy to use iText7 and C# to delete pages of a PDF. A byte array of that PDF and a list of integers is all you need.
This takes in a byte
array and any sort of IEnumerable
of type int
(List
/Array
/etc) to represent which page numbers to delete. This list is a normal 1 based index, meaning page 1 of the document is a 1 in the list.
For example, to delete pages 2, 5 and 6 you could pass in:
new List<int> {2, 5, 6}
Then with iText7, call the following:
public byte[] DeletePages(byte[] pdf, IEnumerable<int> pageNumbers)
{
using (var inputStream = new MemoryStream(pdf))
{
using (var reader = new PdfReader(inputStream))
{
using (var outputStream = new MemoryStream())
{
using (var writer = new PdfWriter(outputStream))
{
using (var document = new PdfDocument(reader, writer))
{
foreach (var pageNumber in pageNumbers)
{
document.RemovePage(pageNumber);
}
}
}
return outputStream.ToArray();
}
}
}
}
As many iText7 Pdf objects implement IDisposable
we have to manually release the unmanaged resources. In the order of above, the resources being managed are:
MemoryStream
for reading in thebyte
array.PdfReader
to read theMemoryStream
.- Another
MemoryStream
to serve as the PDF stream after the pages have been deleted. PdfWriter
to write to the output stream.PdfDocument
to take in the reader and produce results in the writer.
Feel free to swap out the using
blocks for .close()
if that fits your style. We can also pass a stream instead - removing the need for the first MemoryStream
using
block.