检查 pdf 标志 pdfAnnotation.HasFlag 错误时出现问题
Problem checking pdf flags pdfAnnotation.HasFlag error
我正在开发一个检查 PDF 注释的工具。我能够检查标志,但在一个特定标志上出现错误。可能是什么原因?
Public Sub GetComments()
Dim oComments As New PDFcomments
Dim reader As PdfReader = New PdfReader("C:\Users\jeee\Desktop_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: 'Only one flag must be checked at once.'
导致错误的行:Debug.Print(oAnnotation.HasFlag(3))
调试结果。
JeEe
Test annotation
4
False
False
注意:如果我将行 Debug.Print(oAnnotation.HasFlag(4))
放在 Debug.Print(oAnnotation.HasFlag(3))
前面,错误仍然发生在同一行。
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。
我正在开发一个检查 PDF 注释的工具。我能够检查标志,但在一个特定标志上出现错误。可能是什么原因?
Public Sub GetComments()
Dim oComments As New PDFcomments
Dim reader As PdfReader = New PdfReader("C:\Users\jeee\Desktop_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: 'Only one flag must be checked at once.'
导致错误的行:Debug.Print(oAnnotation.HasFlag(3))
调试结果。
JeEe Test annotation 4 False False
注意:如果我将行 Debug.Print(oAnnotation.HasFlag(4))
放在 Debug.Print(oAnnotation.HasFlag(3))
前面,错误仍然发生在同一行。
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。