' ' PDFNet Copyright (c) 2001-2008 by PDFTron Systems Inc. All Rights Reserved. ' Imports System Imports pdftron Imports pdftron.Common Imports pdftron.Filters Imports pdftron.SDF Imports pdftron.PDF Module Module1 Sub AnnotationHighLevelAPI(ByRef doc As PDFDoc) ' The following code snippet traverses all annotations in the document Console.WriteLine("Traversing all annotations in the document...") Dim itr As PageIterator = doc.GetPageIterator() While itr.HasNext() ' Read every page Console.WriteLine("Page {0:d}: ", itr.GetPageNumber()) Dim page As Page = itr.Current() Dim num_annots As Integer = page.GetNumAnnots() Dim i As Integer = 0 While i < num_annots Dim ann As Annot = page.GetAnnot(i) If ann.IsValid() Then Console.WriteLine("Annot Type: {0:s}", ann.GetSDFObj().Get("Subtype").Value().GetName()) Dim bbox As Rect = ann.GetRect() Console.WriteLine(" Position: {0:f}, {1:f}, {2:f}, {3:f}", bbox.x1, bbox.y1, bbox.x2, bbox.y2) Dim type As Annot.Type = ann.GetType() If type = ann.Type.e_Link Then Dim action As Action = ann.GetLinkAction() If action.IsValid() Then If action.GetType() = action.Type.e_GoTo Then Dim dest As Destination = action.GetDest() If dest.IsValid() Then Dim page_num As Integer = dest.GetPage().GetIndex() Console.WriteLine(" Links to: page number {0:d} in this document", page_num) End If ElseIf action.GetType() = action.Type.e_URI Then Dim uri As String = action.GetSDFObj().Get("URI").Value().GetAsPDFText() Console.WriteLine(" Links to: {0:s}", uri) ' ... End If End If ElseIf type = ann.Type.e_Widget Then ' ... ElseIf type = ann.Type.e_FileAttachment Then ' ... End If End If i = i + 1 End While itr.Next() End While ' Use the high-level API to create new annotations. ' Create a stamp annotation ... Dim goto_page_3 As Action = action.CreateGoto(Destination.CreateFitH(doc.GetPage(3), 0)) Dim link As Annot = Annot.CreateLink(doc.GetSDFDoc(), New Rect(85, 458, 503, 502), goto_page_3) ' Set the annotation border width to 3 points... Dim s As Annot.BorderStyle.Style = Annot.BorderStyle.Style.e_solid Dim bs As Annot.BorderStyle = New Annot.BorderStyle(s, 3) link.SetBorderStyle(bs) link.SetColor(New ColorPt(0, 0, 1)) ' Add the new annotation to the first page Dim first_page As Page = doc.GetPage(1) first_page.AnnotPushBack(link) ' Create a stamp annotation ... Dim stamp As Annot = Annot.CreateStamp(doc.GetSDFDoc(), New Rect(30, 30, 300, 200)) stamp.SetStampName("Draft") first_page.AnnotPushBack(stamp) ' Create a file attachment annotation (embed the 'peppers.jpg'). Dim file_attach As Annot = Annot.CreateFileAttachment(doc.GetSDFDoc(), New Rect(80, 280, 200, 320), input_path + "peppers.jpg") first_page.AnnotPushBack(file_attach) ' ... End Sub Sub AnnotationLowLevelAPI(ByRef doc As PDFDoc) Dim page As Page = doc.GetPage(1) Dim annots As Obj = page.GetAnnots() If annots Is Nothing Then ' If there are no annotations, create a new annotation ' array for the page. annots = doc.CreateIndirectArray() page.GetSDFObj().Put("Annots", annots) End If ' Create a Text annotation Dim annot As Obj = doc.CreateIndirectDict() annot.PutName("Subtype", "Text") annot.PutBool("Open", True) annot.PutString("Contents", "The quick brown fox ate the lazy mouse.") annot.PutRect("Rect", 266, 116, 430, 204) ' Insert the annotation in the page annotation array annots.PushBack(annot) ' Create a Link annotation Dim link1 As Obj = doc.CreateIndirectDict() link1.PutName("Subtype", "Link") Dim dest As Destination = Destination.CreateFit(doc.GetPage(2)) link1.Put("Dest", dest.GetSDFObj()) link1.PutRect("Rect", 85, 705, 503, 661) annots.PushBack(link1) ' Create another Link annotation Dim link2 As Obj = doc.CreateIndirectDict() link2.PutName("Subtype", "Link") Dim dest2 As Destination = Destination.CreateFit(doc.GetPage(3)) link2.Put("Dest", dest2.GetSDFObj()) link2.PutRect("Rect", 85, 638, 503, 594) annots.PushBack(link2) ' Note that PDFNet APi can be used to modify existing annotations. ' In the following example we will modify the second link annotation ' (link2) so that it points to the 10th page. We also use a different ' destination page fit type. ' link2 = annots.GetAt(annots.Size()-1); link2.Put("Dest", Destination.CreateXYZ(doc.GetPage(10), 100, 792 - 70, 10).GetSDFObj()) ' Create a third link annotation with a hyperlink action (all other ' annotation types can be created in a similar way) Dim link3 As Obj = doc.CreateIndirectDict() link3.PutName("Subtype", "Link") link3.PutRect("Rect", 85, 570, 503, 524) ' Create a URI action Dim action As Obj = link3.PutDict("A") action.PutName("S", "URI") action.PutString("URI", "http://www.pdftron.com") annots.PushBack(link3) End Sub ' Relative path to the folder containing test files. Dim input_path As String = "../../../TestFiles/" Dim output_path As String = "../../../TestFiles/Output/" Sub Main() PDFNet.Initialize() PDFNet.SetResourcesPath("../../../../resources") Try Console.WriteLine("-------------------------------------------------") Console.WriteLine("Opening the input file...") Dim doc As PDFDoc = New PDFDoc(input_path + "numbered.pdf") doc.InitSecurityHandler() ' An example of using SDF/Cos API to add any type of annotations. AnnotationLowLevelAPI(doc) doc.Save(output_path + "annotation_test1.pdf", SDF.SDFDoc.SaveOptions.e_linearized) ' An example of using the high-level PDFNet API to read existing annotations, ' to edit existing annotations, and to create new annotation from scratch. AnnotationHighLevelAPI(doc) doc.Save(output_path + "annotation_test2.pdf", SDF.SDFDoc.SaveOptions.e_linearized) doc.Close() Console.WriteLine("Done.") Catch ex As PDFNetException Console.WriteLine(ex.Message) Catch ex As Exception MsgBox(ex.Message) End Try PDFNet.Terminate() End Sub End Module