我尝试更改所有注释链接目标以适应页面,我还需要验证链接。
这是我尝试的代码
Dim PdfReader As PdfReader
'Dim myBookmarks As New List(Of Dictionary(Of String, Object))()
Dim pg As Integer
PdfReader = New PdfReader("D:\\Annot_Testing.pdf")
Dim PageCount As Integer = PdfReader.NumberOfPages
For pg = 1 To PdfReader.NumberOfPages
Dim PageDictionary As PdfDictionary = PdfReader.GetPageN(pg)
Dim Annots As PdfArray = PageDictionary.GetAsArray(PdfName.ANNOTS)
If (Annots Is Nothing) OrElse (Annots.Length = 0) Then
Continue For
End If
''//Loop through each annotation
For Each A In Annots.ArrayList
''//I do not completely understand this but I think this turns an Indirect Reference into an actual object, but I could be wrong
''//Anyway, convert the itext-specific object as a generic PDF object
Dim AnnotationDictionary = DirectCast(PdfReader.GetPdfObject(A), PdfDictionary)
''//Make sure this annotation has a link
If Not AnnotationDictionary.Get(PdfName.SUBTYPE).Equals(PdfName.LINK) Then Continue For
''//Make sure this annotation has an ACTION
If AnnotationDictionary.Get(PdfName.ACTION) Is Nothing Then Continue For
''//Get the ACTION for the current annotation
Dim AnnotationAction = DirectCast(AnnotationDictionary.Get(PdfName.A), PdfDictionary)
''//Test if it is a named actions such as /FIRST, /LAST, etc
If AnnotationAction.Get(PdfName.S).Equals(PdfName.NAMED) Then
MsgBox("Yes named")
''//Otherwise see if its a GOTO page action
ElseIf AnnotationAction.Get(PdfName.S).Equals(PdfName.PAGE) Then
MsgBox("page")
ElseIf AnnotationAction.Get(PdfName.S).Equals(PdfName.GOTOR) Then
MsgBox("gotoR")
ElseIf AnnotationAction.Get(PdfName.S).Equals(PdfName.GOTO) Then
''//Make sure that it has a destination
If AnnotationAction.GetAsArray(PdfName.D) Is Nothing Then Continue For
Dim AnnotationReferencedPage = PdfReader.GetPdfObject(DirectCast(AnnotationAction.GetAsArray(PdfName.D).ArrayList(0), PRIndirectReference))
''//Re-loop through all of the pages in the main document comparing them to this page
For J = 1 To PageCount
If AnnotationReferencedPage.Equals(PdfReader.GetPageN(J)) Then
Trace.WriteLine(J)
Exit For
End If
Next
ElseIf AnnotationAction.Get(PdfName.S).Equals(PdfName.URI) Then
Else
MsgBox("else type")
End If
Next
Next
'bookmarkGlobalSetting(myBookmarks)
PdfReader.Close()
在附有3个annots的示例文档中,但是在通过代码阅读时它没有显示对它的操作,但是当它在acrobat中看到它时它是有用的。请建议我这个..
请从这里下载... https://drive.google.com/file/d/0B72uT6TzR_cdWGczRkY3ZmNpeTg/edit?usp=sharing
答案 0 :(得分:0)
这些注释没有任何 A 操作条目:
您希望找到带有链接的操作是错误的,因为它可能会有目的地,参见部分 12.5.6.5 ISO 32000-1的链接注释:
链接注释表示到文档中其他位置的目的地的超文本链接(请参阅12.3.2,“目标”)或要执行的操作(12.6,“操作”)。表173显示了特定于此类注释的注释字典条目。
您的代码仅处理后一种类型(要执行的操作)而不是前者(指向文档中其他位置的目标的超文本链接)
因此,您必须改进代码以处理前一种类型。您可以在表173 - 特定于ISO 32000-1的链接注释的其他条目中找到详细信息。
PS:我假设你的
If AnnotationDictionary.Get(PdfName.ACTION) Is Nothing Then Continue For
应该是
If AnnotationDictionary.Get(PdfName.A) Is Nothing Then Continue For