VintaSoft Imaging .NET SDK v8.6
Vintasoft.Imaging.Pdf.Tree Namespace / PdfPage Class / RemoveText Method / RemoveText(PdfTextRegion[]) Method
The text regions to remove.
Example



In This Topic
    RemoveText(PdfTextRegion[]) Method
    In This Topic
    Removes specified text regions from page content.
    Syntax
    'Declaration
     
    Public Overloads Sub RemoveText( _
       ByVal ParamArray regions() As PdfTextRegion _
    ) 
    public void RemoveText( 
       params PdfTextRegion[] regions
    )
    public: void RemoveText( 
       params PdfTextRegion*[]* regions
    ) 
    public:
    void RemoveText( 
       ... array<PdfTextRegion^>^ regions
    ) 

    Parameters

    regions
    The text regions to remove.
    Remarks
    Important! - PDF document must be packed (Vintasoft.Imaging.Pdf.PdfDocument.Pack) after content removal otherwise the removed content can be restored.
    Example
    This example 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.Pdf.Content.TextExtraction.PdfTextRegion)()
                
                ' 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.Pdf.Content.TextExtraction.PdfTextRegion() = 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.Pdf.Content.TextExtraction.PdfTextRegion()
        Dim textRegions As New System.Collections.Generic.List(Of Vintasoft.Imaging.Pdf.Content.TextExtraction.PdfTextRegion)()
                
        Dim textRegion As Vintasoft.Imaging.Pdf.Content.TextExtraction.PdfTextRegion = 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.Pdf.Content.TextExtraction.PdfTextRegion> textRegions = 
                    new System.Collections.Generic.List<Vintasoft.Imaging.Pdf.Content.TextExtraction.PdfTextRegion>();
                
                // 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.Pdf.Content.TextExtraction.PdfTextRegion[] 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.Pdf.Content.TextExtraction.PdfTextRegion[] SimpleTextSearchOnPdfPage(
        Vintasoft.Imaging.Pdf.Tree.PdfPage page, string text)
    {
        System.Collections.Generic.List<Vintasoft.Imaging.Pdf.Content.TextExtraction.PdfTextRegion> textRegions = 
            new System.Collections.Generic.List<Vintasoft.Imaging.Pdf.Content.TextExtraction.PdfTextRegion>();
                
        Vintasoft.Imaging.Pdf.Content.TextExtraction.PdfTextRegion 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: Windows 10, Windows 8, Windows 7, Windows Vista, Windows XP, Windows Server 2012, Windows Server 2008, Windows Server 2003

    See Also