How to detect blank pages in PDF files with PDF document in .NET Core?
The Syncfusion Essential® PDF library is a powerful and feature-rich .NET PDF framework that allows developers to create, read, and manipulate PDF documents programmatically. Using this library, you can efficiently detect blank or empty pages within PDF files in C#.
Steps to detect blank pages in a PDF programmatically:
- Create a new project: Start a new Console application in .NET Core.
- Install required packages: Add the Syncfusion.Pdf.WinForms, Syncfusion.PdfToImageConverter.WinForms and Syncfusion.Pdf.OCR.WinForms NuGet package as a reference in the console application from Nuget.org.
- Set up the environment: In the
Program.csfile, include the following namespaces.
using System;
using System.Collections.Generic;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
- Use the following code sample in
Program.csto detect blank pages in a PDF programmatically using .NET.
using (FileStream file = new FileStream(Path.GetFullPath("Data/Input.pdf"), FileMode.Open, FileAccess.Read))
{
// Load the existing PDF document
PdfLoadedDocument ldoc = new PdfLoadedDocument(file);
PdfToImageConverter converter = new PdfToImageConverter();
converter.Load(file);
List<int> emptyPages = new List<int>();
for (int i = 0; i < ldoc.Pages.Count; i++)
{
PdfLoadedPage lpage = ldoc.Pages[i] as PdfLoadedPage;
if (IsBlankPage(lpage, i, converter))
{
emptyPages.Add(i + 1); // +1 because pages are normally 1‑based
Console.WriteLine($"Page {i + 1} is blank.");
}
}
ldoc.Close(true);
Console.WriteLine();
Console.WriteLine($"Total blank pages: {emptyPages.Count}");
}
- Determine blank pages: Identifies whether a PDF page contains any visible content, such as text, images, or shapes, and classifies it as blank if no meaningful content is detected.
// Determines whether a page is blank.
private static bool IsBlankPage(PdfLoadedPage lpage, int pageIndex, PdfToImageConverter converter)
{
bool isBlankPage = false;
if (lpage.Annotations.Count > 0)
{
isBlankPage = false;
return isBlankPage;
}
// For images.
System.Drawing.Image[] images = lpage.ExtractImages();
if (images.Length > 0)
{
foreach (System.Drawing.Image img in images)
{
if (!IsEmpty(img as Bitmap))
{
isBlankPage = false;
break;
}
else
isBlankPage = true;
}
}
else
{
//For shapes
Stream imageSteam = converter.Convert(pageIndex, false, false);
imageSteam.Position = 0;
if (imageSteam != null && IsEmpty(new Bitmap(imageSteam)))
{
isBlankPage = true;
}
else
{
isBlankPage = false;
}
}
return isBlankPage;
}
- Check Bitmap Content: Evaluates a bitmap image by counting non‑white pixels to determine whether it is visually empty.
// Determines if a bitmap is "empty" by counting non-white pixels.
private static bool IsEmpty(Bitmap image)
{
Rectangle bounds = new Rectangle(0, 0, image.Width, image.Height);
BitmapData bmpData = image.LockBits(bounds, ImageLockMode.ReadWrite, image.PixelFormat);
IntPtr ptr = bmpData.Scan0;
int bytes = Math.Abs(bmpData.Stride) * image.Height;
byte[] rgbValues = new byte[bytes];
Marshal.Copy(ptr, rgbValues, 0, bytes);
image.UnlockBits(bmpData);
// Count non-white pixels
int nonWhitePixelCount = rgbValues.Count(b => b != 255);
// If less than 1% of pixels are non-white, consider it blank
int threshold = (image.Width * image.Height) / 100; // 1%
return nonWhitePixelCount < threshold;
}
A complete working sample is available for download from GitHub.
By executing the program, you will generate the following PDF document.
Take a moment to peruse the documentation to learn how to handle pages with various settings and operations.
Conclusion
I hope you found this guide helpful in learning how to detect blank pages in PDF documents programmatically using .NET.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!