7 Replies Latest reply on Jan 5, 2017 7:52 AM by andrewm50226713

    Java Script Created Bookmark Issue in a PDF

    andrewm50226713

      I am having troubles with my bookmarks I created in VB.net. I used a javascript object to create them.

       

      The problem is, when I add an additional sheet in the front of the created PDF, the bookmarks don't update. They still point to their original page number.

      When I open the PDF and create the bookmarks in Adobe, the bookmarks update when I add sheets to the PDF....

       

      Any advise would be greatly appreciated!

       

      Here is my code :

       

       

       

       

      Sub MergePDFs(files() As String)
             
              Dim i As Long, pgNum As Long, mergedDocNumPages As Long
              Dim AcroApp As New Acrobat.AcroApp, DocFragments() As Acrobat.CAcroPDDoc
              Dim todaysDate As String
              Dim jso As Object 'Java Script Object
              Dim jsBookmarkPgNum As String
              Dim jsBookmarkCounter As Integer = 0
              Dim jsBookmarkName As String

              todaysDate = Format(Date.Today, "MMMM dd, yyyy")

              Dim DestFile As String
              DestFile = "C:\Temp\PDF CD's\Modification Drawings " & todaysDate & ".pdf"

              ReDim DocFragments(0 To UBound(files))

              If Len(Dir(DestFile)) Then Kill(DestFile)
              For i = 0 To UBound(files)

                  ' Check PDF file presence
                  If Dir(Trim(files(i))) = "" Then
                      MsgBox("File not found.", 0, "Canceled")
                      Exit For
                  End If
                  ' Open PDF document
                  DocFragments(i) = CreateObject("AcroExch.PDDoc")
                  DocFragments(i).Open(Trim(files(i)))

                  'Create the Bookmark
                  If files(i) Like "*BOM*" Then
                      jsBookmarkPgNum = "this.pageNum= " & pgNum
                      jsBookmarkName = files(i).Substring(36, 12) & " Rev " & files(i).Substring(49, 1)
                      jso = DocFragments(0).GetJSObject
                      jso.bookmarkRoot.createChild(jsBookmarkName, jsBookmarkPgNum, jsBookmarkCounter)
                      jsBookmarkCounter = jsBookmarkCounter + 1

                  End If

                  If i Then
                      ' Merge PDF fragment into DocFragments(0) document
                      ' Calc the number of pages in the merged document
                      mergedDocNumPages = DocFragments(i).GetNumPages()
                      DocFragments(0).InsertPages(pgNum - 1, DocFragments(i), 0, mergedDocNumPages, True)

                      pgNum = pgNum + mergedDocNumPages
                      ' Release this fragment from memory
                      DocFragments(i).Close()
                      DocFragments(i) = Nothing

                  Else
                      ' Calc the number of pages in DocFragments(0) document
                      pgNum = DocFragments(0).GetNumPages()
                  End If
              Next

              'Check if we mergeded any fragments
              If i > UBound(files) Then
                  ' Save the merged document {DocFragments(0)} to DestFile
                  If DocFragments(0).Save(1, DestFile) Then
                      MsgBox("The resulting file is created:" & vbLf & DestFile, 0, "Done")
                  Else
                      MsgBox("Cannot save the resulting document:" & DestFile, 48, "Canceled")
                  End If
              End If

              ' Release the krakken! oh and the memory also
              If Not DocFragments(0) Is Nothing Then DocFragments(0).Close()
              DocFragments(0) = Nothing

              ' Exit the Acrobat application
              AcroApp.Exit()
              AcroApp = Nothing

          End Sub