How do I remove link annotations from a PDF using iText?

后端 未结 2 573
独厮守ぢ
独厮守ぢ 2021-01-03 13:58

Here i want to remove Annotation(Link, Text, ..) from PDF permanently using iTextSharp.

Already i have tried

AnnotationDictionary.Remove(PdfName.LIN         


        
相关标签:
2条回答
  • 2021-01-03 14:34

    I got the answer for my question.

    Sample Code:

    //Get the current page
    PageDictionary = R.GetPageN(i);
    
    //Get all of the annotations for the current page
    Annots = PageDictionary.GetAsArray(PdfName.ANNOTS);
    
    foreach (PdfObject A in Annots.ArrayList)
    {
    //code to check the annotation 
    
    //remove the annotation
    Annots.Remove(int idx);
    
    }
    
    0 讨论(0)
  • 2021-01-03 14:37
            Dim pdfReader As New PdfReader(fileloc)
            For i = 1 To pdfReader.NumberOfPages
                Dim pageDict As PdfDictionary = pdfReader.GetPageN(i)
                Dim annots As PdfArray = pageDict.GetAsArray(PdfName.ANNOTS)
                Dim newAnnots As PdfArray = New PdfArray()
                If annots IsNot Nothing Then
                    For j As Integer = 0 To annots.Size() - 1
                        Dim annotDict As PdfDictionary = annots.GetAsDict(i)
                        If Not PdfName.LINK.Equals(annotDict.GetAsName(PdfName.SUBTYPE)) Then
                            newAnnots.Add(annots.GetAsDict(j))
                        End If
                    Next
                    pageDict.Put(PdfName.ANNOTS, newAnnots)
                End If
            Next
            Dim pdfStamper As PdfStamper = Nothing
            Dim extension = Path.GetExtension(fileloc)
            Dim filename As String = Path.GetFileNameWithoutExtension(fileloc)
            Dim filePath As String = Path.GetDirectoryName(fileloc)
            fileloc = filePath + "\" + filename + "new" + extension
            pdfStamper = New PdfStamper(pdfReader, New FileStream(fileloc, FileMode.Create))
            pdfStamper.FormFlattening = False
            pdfStamper.Close()
            pdfReader.Close()
        End Sub```
    
    0 讨论(0)
提交回复
热议问题