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.

Before removing punch holes After removing punch holes

Information

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.

  1. Create a GdPictureImaging object.

  2. Select the image by passing its path to the CreateGdPictureImageFromFile method of the GdPictureImaging object.

  3. Remove the punch holes with the RemoveHolePunch method of the GdPictureImaging object. This method takes the following parameters:

    1. The image ID.

    2. 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.

  4. Save the output in a new image with the SaveAsPNG method of the GdPictureImaging object.

  5. Release the image resource with the ReleaseGdPictureImage method of the GdPictureImaging 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
Used Methods and Properties

Related Topics