annotations - 检查 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))
放在同一行上仍然会发生错误。
解决方案
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。
推荐阅读
- c - 为什么这个程序打印带有奇怪字符的字符串?尽管释放内存泄漏?
- sql - 如何使用 lag 返回当前行的值与下一行的值之间的差异?
- reactjs - React dnd 使用自定义拖动层重新渲染太多,react.memo
- r - 如何打印函数的计算方法
- c++ - 将 2d 数组初始化为初始化列表 C++ 中的一个值
- ruby-on-rails - 如何使用 @rails/webpacker 加载本地字体?
- file - 如何在 Haskell 中反复读取大数据文件的洗牌行?
- dataframe - PySpark Filter between - 根据组提供上限和下限列表
- java - 从活动中访问片段方法中的方法
- ssh - 以编程方式从 SFTP 服务器下载千兆字节文件的有效方法