Remove Lines from Images and PDFs in C#

Some documents have lines on the page, which makes pages difficult to read. This guide explains how to automatically remove lines from your images and PDF documents.

The images below show what a document looks like before and after removing lines.

Before removing lines After removing lines

Information

Don’t preprocess documents before recognizing text with OCR. The GdPicture.NET OCR engine preprocesses documents automatically with better results than manual preprocessing.

Removing All Lines

To automatically detect all horizontal or vertical lines 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 lines with the RemoveLines method of the GdPictureImaging object. This method takes the following parameters:

    1. The image ID.

    2. Members of the LineRemoveOrientation enumeration, separated by vertical bar | characters. This parameter specifies the types of lines that are removed.

  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 all horizontal lines from the document:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
// Load the image from a file.
int imageId = gdpictureImaging.CreateGdPictureImageFromFile(@"C:/temp/source.png");
// Remove the lines.
gdpictureImaging.RemoveLines(imageId, LineRemoveOrientation.Horizontal);
// 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 lines.
    gdpictureImaging.RemoveLines(imageId, LineRemoveOrientation.Horizontal)
    ' 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

Removing Specific Lines

To remove specific lines that meet your criteria, 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 lines with the RemoveLines method of the GdPictureImaging object. This method takes the following parameters:

    1. The image ID.

    2. Members of the LineRemoveOrientation enumeration, separated by vertical bar | characters. This parameter specifies the types of lines that are removed.

    3. The maximum gap between the lines. Lines with more space between them aren’t removed.

    4. The maximum thickness of the lines. Thicker lines aren’t removed.

    5. The minimum length of the lines. Shorter lines aren’t removed.

    6. The maximum character interception of the lines. Lines with more character interception aren’t removed.

    7. Some characters might be affected by the line removal. For example, some characters might have gaps in them where a line crossed them. Specify whether to correct these characters.

  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 only removes specific horizontal and vertical lines from the document:

using GdPictureImaging gdpictureImaging = new GdPictureImaging();
// Load the image from a file.
int imageId = gdpictureImaging.CreateGdPictureImageFromFile(@"C:/temp/source.png");
// Remove the lines.
gdpictureImaging.RemoveLines(imageId, LineRemoveOrientation.Horizontal | LineRemoveOrientation.Vertical,
    10, 7, 500, 5, true);
// 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 lines.
    gdpictureImaging.RemoveLines(imageId, LineRemoveOrientation.Horizontal, 10, 7, 500, 5, True)
    ' 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