问题检查pdf标志pdfAnnotation.HasFlag错误

时间:2019-01-14 09:42:24

标签: annotations itext flags itext7

我正在使用一种检查PDF批注的工具。我能够检查这些标志,但是在一个特定的标志上遇到了错误。可能是什么原因?

    Public Sub GetComments()

    Dim oComments As New PDFcomments
    Dim reader As PdfReader = New PdfReader("C:\Users\jeee\Desktop\1_3047 - Type 1.pdf")
    Dim pdfdocument As New PdfDocument(reader)


    For i As Integer = 1 To pdfdocument.GetNumberOfPages

        Dim pdfPage As PdfPage = pdfdocument.GetPage(i)
        Dim oAnnotations As IList(Of Annot.PdfAnnotation) = pdfPage.GetAnnotations()


        For Each oAnnotation As Annot.PdfAnnotation In oAnnotations

            Dim oAnnotationSubType As PdfName = oAnnotation.GetSubtype

            If oAnnotationSubType.ToString = "/FreeText" Then
                Debug.Print(oAnnotation.GetTitle.ToString)
                Debug.Print(oAnnotation.GetContents.ToString)
                Debug.Print(oAnnotation.GetFlags)

                Debug.Print(oAnnotation.HasFlag(1))
                Debug.Print(oAnnotation.HasFlag(2))
                Debug.Print(oAnnotation.HasFlag(3))
                Debug.Print(oAnnotation.HasFlag(4))

            End If

        Next
    Next

End Sub
  

System.ArgumentException:“一次只能检查一个标志。”

导致错误的行:Debug.Print(oAnnotation.HasFlag(3))

调试结果。

  

吉   测试注解   4   假   错误

注意:如果我将Debug.Print(oAnnotation.HasFlag(4))行放在Debug.Print(oAnnotation.HasFlag(3))前面,则错误仍然出现在同一行。

1 个答案:

答案 0 :(得分:1)

HasFlag记录如下:

    /// <summary>
    /// Checks if the certain flag that specifies a characteristic of the annotation
    /// is in enabled state (see ISO-320001 12.5.3, "Annotation Flags").
    /// </summary>
    /// <remarks>
    /// Checks if the certain flag that specifies a characteristic of the annotation
    /// is in enabled state (see ISO-320001 12.5.3, "Annotation Flags").
    /// This method allows only one flag to be checked at once, use constants listed in
    /// <see cref="SetFlag(int)"/>
    /// .
    /// </remarks>
    /// <param name="flag">
    /// an integer interpreted as set of one-bit flags. Only one bit must be set in this integer, otherwise
    /// exception is thrown.
    /// </param>
    /// <returns>true if the given flag is in enabled state.</returns>
    public virtual bool HasFlag(int flag)

因此,参数必须是一个整数,解释为一组一位标志。此整数中只能设置一位。整数3显然有两位设置。

您似乎认为该参数的含义类似于第n个标志,但实际上意味着值为n 的标志。

因此允许的值为1、2、4、8 ...,但特别是不是3。