首页 > 解决方案 > 检查 pdf 标志的问题 pdfAnnotation.HasFlag 错误

问题描述

我正在开发一种检查 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))

调试结果。

JeEe 测试注释 4 False False

注意:如果我把Debug.Print(oAnnotation.HasFlag(4))前面的行Debug.Print(oAnnotation.HasFlag(3))放在同一行上仍然会发生错误。

标签: annotationsitextflagsitext7

解决方案


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。


推荐阅读