Remove Punch Holes from Images and PDFs in C#
Physical documents can have punch holes used to bind sheets of paper together. In scans of physical documents, punch holes make pages difficult to read. This article explains how to automatically remove punch holes from your images and PDF documents.
The images below show what a document looks like before and after removing punch holes.
Don’t preprocess documents before recognizing text with OCR. The GdPicture.NET OCR engine preprocesses documents automatically with better results than manual preprocessing.
To automatically detect punch holes in a document and remove them, follow the steps below.
-
Create a
GdPictureImaging
object. -
Select the image by passing its path to the
CreateGdPictureImageFromFile
method of theGdPictureImaging
object. -
Remove the punch holes with the
RemoveHolePunch
method of theGdPictureImaging
object. This method takes the following parameters:-
The image ID.
-
Optional: Members of the
HolePunchMargins
enumeration, separated by vertical bar|
characters. This parameter specifies the sides of the page from which the punch holes are removed. If you don’t specify this parameter, punch holes are removed from the left side of the page by default.
-
-
Save the output in a new image with the
SaveAsPNG
method of theGdPictureImaging
object. -
Release the image resource with the
ReleaseGdPictureImage
method of theGdPictureImaging
object.
The example below removes punch holes from the left side of the page:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); // Load the image from a file. int imageId = gdpictureImaging.CreateGdPictureImageFromFile(@"C:/temp/source.png"); // Remove the punch holes. gdpictureImaging.RemoveHolePunch(imageId); // Save the output in a new image. gdpictureImaging.SaveAsPNG(imageId, @"C:/temp/output.png"); gdpictureImaging.ReleaseGdPictureImage(imageId);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Load the image from a file. Dim imageId As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:/temp/source.png") ' Remove the punch holes. gdpictureImaging.RemoveHolePunch(imageId) ' Save the output in a new image. gdpictureImaging.SaveAsPNG(imageId, "C:/temp/output.png") gdpictureImaging.ReleaseGdPictureImage(imageId) End Using
The example below removes punch holes from the left and right sides of the page:
using GdPictureImaging gdpictureImaging = new GdPictureImaging(); // Load the image from a file. int imageId = gdpictureImaging.CreateGdPictureImageFromFile(@"C:/temp/source.png"); // Remove the punch holes. gdpictureImaging.RemoveHolePunch(imageId, HolePunchMargins.MarginLeft | HolePunchMargins.MarginRight); // Save the output in a new image. gdpictureImaging.SaveAsPNG(imageId, @"C:/temp/output.png"); gdpictureImaging.ReleaseGdPictureImage(imageId);
Using gdpictureImaging As GdPictureImaging = New GdPictureImaging() ' Load the image from a file. Dim imageId As Integer = gdpictureImaging.CreateGdPictureImageFromFile("C:/temp/source.png") ' Remove the punch holes. gdpictureImaging.RemoveHolePunch(imageId, HolePunchMargins.MarginLeft | HolePunchMargins.MarginRight) ' Save the output in a new image. gdpictureImaging.SaveAsPNG(imageId, "C:/temp/output.png") gdpictureImaging.ReleaseGdPictureImage(imageId) End Using