VintaSoft Imaging .NET SDK 15.1: Documentation for .NET developer
Vintasoft.Imaging.Pdf.Tree Namespace / PdfPage Class / RemoveText Methods / RemoveText(TextRegion[]) Method
Syntax Remarks Example Requirements SeeAlso
In This Topic
RemoveText(TextRegion[]) Method (PdfPage)
In This Topic
Removes specified text regions from page content.
Syntax
'Declaration

Public Overloads Sub RemoveText( _
ByVal ParamArray regions
The text regions to remove.
() As Vintasoft.Imaging.Text.TextRegion _
)
public void RemoveText(
params Vintasoft.Imaging.Text.TextRegion[] regions
)

Parameters

regions
The text regions to remove.
Remarks

Important! - PDF document must be packed (Pack) after content removal otherwise the removed content can be restored.

Example

Here is an example that shows how to find a text on PDF page and remove text from PDF page:


''' <summary>
''' Searches and removes specified text on all pages of PDF document.
''' </summary>
''' <param name="inputPdfFilename">The name of input PDF file.</param>
''' <param name="outputPdfFilename">The name of output PDF file.</param>
''' <param name="textToRemove">The text to remove.</param>
Public Shared Sub TestFindAndRemoveTextOnAllPages(inputPdfFilename As String, outputPdfFilename As String, ParamArray textToRemove As String())
    ' open document
    Using document As New Vintasoft.Imaging.Pdf.PdfDocument(inputPdfFilename)
        ' if there is a text to remove
        If textToRemove.Length > 0 Then
            ' create a list that contains text regions to remove
            Dim textRegions As New System.Collections.Generic.List(Of Vintasoft.Imaging.Text.TextRegion)()

            ' for each page
            For Each page As Vintasoft.Imaging.Pdf.Tree.PdfPage In document.Pages
                ' clear a list of text regions to remove
                textRegions.Clear()

                ' for all text strings that must be remove
                For i As Integer = 0 To textToRemove.Length - 1
                    ' search text string on PDF page
                    Dim searchedText As Vintasoft.Imaging.Text.TextRegion() = SimpleTextSearchOnPdfPage(page, textToRemove(i))
                    ' if text is found
                    If searchedText IsNot Nothing AndAlso searchedText.Length > 0 Then
                        ' add searched text to a list of text for removing
                        textRegions.AddRange(searchedText)
                    End If
                Next

                ' if PDF page contains text regions with text to remove
                If textRegions.Count > 0 Then
                    ' remove text regions from PDF page
                    page.RemoveText(textRegions.ToArray())
                End If
            Next
        End If

        ' if names of source and destination files are the same
        If inputPdfFilename = outputPdfFilename Then
            ' pack PDF document
            document.Pack()
        Else
            ' if names of source and destination files are different
            ' pack source PDF document to specified file
            document.Pack(outputPdfFilename)
        End If
    End Using
End Sub

''' <summary>
''' Searches a text string on PDF page.
''' </summary>
''' <param name="page">PDF page where text should be searched.</param>
''' <param name="text">Text to search.</param>
''' <returns>An array of text regions on PDF page where text was found.</returns>
Public Shared Function SimpleTextSearchOnPdfPage(page As Vintasoft.Imaging.Pdf.Tree.PdfPage, text As String) As Vintasoft.Imaging.Text.TextRegion()
    Dim textRegions As New System.Collections.Generic.List(Of Vintasoft.Imaging.Text.TextRegion)()

    Dim textRegion As Vintasoft.Imaging.Text.TextRegion = Nothing
    Dim startIndex As Integer = 0
    Do
        ' search text
        textRegion = page.TextRegion.FindText(text, startIndex, False)
        ' if text is found
        If textRegion IsNot Nothing Then
            ' add searched text to a result
            textRegions.Add(textRegion)
            ' shift start index
            startIndex += textRegion.TextContent.Length
        End If
    Loop While textRegion IsNot Nothing

    Return textRegions.ToArray()
End Function


/// <summary>
/// Searches and removes specified text on all pages of PDF document.
/// </summary>
/// <param name="inputPdfFilename">The name of input PDF file.</param>
/// <param name="outputPdfFilename">The name of output PDF file.</param>
/// <param name="textToRemove">The text to remove.</param>
public static void TestFindAndRemoveTextOnAllPages(
    string inputPdfFilename,
    string outputPdfFilename,
    params string[] textToRemove)
{
    // open document
    using (Vintasoft.Imaging.Pdf.PdfDocument document = new Vintasoft.Imaging.Pdf.PdfDocument(inputPdfFilename))
    {
        // if there is a text to remove
        if (textToRemove.Length > 0)
        {
            // create a list that contains text regions to remove
            System.Collections.Generic.List<Vintasoft.Imaging.Text.TextRegion> textRegions =
                new System.Collections.Generic.List<Vintasoft.Imaging.Text.TextRegion>();

            // for each page
            foreach (Vintasoft.Imaging.Pdf.Tree.PdfPage page in document.Pages)
            {
                // clear a list of text regions to remove
                textRegions.Clear();

                // for all text strings that must be remove
                for (int i = 0; i < textToRemove.Length; i++)
                {
                    // search text string on PDF page
                    Vintasoft.Imaging.Text.TextRegion[] searchedText = SimpleTextSearchOnPdfPage(page, textToRemove[i]);
                    // if text is found
                    if (searchedText != null && searchedText.Length > 0)
                        // add searched text to a list of text for removing
                        textRegions.AddRange(searchedText);
                }

                // if PDF page contains text regions with text to remove
                if (textRegions.Count > 0)
                    // remove text regions from PDF page
                    page.RemoveText(textRegions.ToArray());
            }
        }

        // if names of source and destination files are the same
        if (inputPdfFilename == outputPdfFilename)
            // pack PDF document
            document.Pack();
        // if names of source and destination files are different
        else
            // pack source PDF document to specified file
            document.Pack(outputPdfFilename);
    }
}

/// <summary>
/// Searches a text string on PDF page.
/// </summary>
/// <param name="page">PDF page where text should be searched.</param>
/// <param name="text">Text to search.</param>
/// <returns>An array of text regions on PDF page where text was found.</returns>
public static Vintasoft.Imaging.Text.TextRegion[] SimpleTextSearchOnPdfPage(
    Vintasoft.Imaging.Pdf.Tree.PdfPage page, string text)
{
    System.Collections.Generic.List<Vintasoft.Imaging.Text.TextRegion> textRegions =
        new System.Collections.Generic.List<Vintasoft.Imaging.Text.TextRegion>();

    Vintasoft.Imaging.Text.TextRegion textRegion = null;
    int startIndex = 0;
    do
    {
        // search text
        textRegion = page.TextRegion.FindText(text, ref startIndex, false);
        // if text is found
        if (textRegion != null)
        {
            // add searched text to a result
            textRegions.Add(textRegion);
            // shift start index
            startIndex += textRegion.TextContent.Length;
        }
    } while (textRegion != null);

    return textRegions.ToArray();
}

Requirements

Target Platforms: .NET 10; .NET 9; .NET 8; .NET 7; .NET 6; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

See Also