IAC – How to remove a single bookmark from a folder of PDFs using VB6

If you don’t have it you should download the href=’http://partners.adobe.com/asn/index.jsp’>Acrobat SDK first up – and read through the IAC documentation. The demonstration is a simple program that allows you to remove a single bookmark from a folder of PDFs. You will need the VB6 runtimes to use this.

This code sample is posted here for the general benefit of the PDF development community. Attribution and usage guidelines are as noted in the code source; please respect the wishes of the author when using this code.


' How to remove a single bookmark from a folder full of PDFs
Option Explicit
' Author : Karl De Abrew
' Company : Planet PDF
' Date : 20 May, 1999
' URL : http://www.planetpdf.com/
' Version : 1.0
' Description: This is a simple demonstration of how a single
' bookmark can be removed from all the files in a given folder

'///////////////////////////////////////////////////
'///////////////////////////////////////////////////

Function RemoveBookmarkFromFile(strSourceFileName As String) As Boolean
On Error GoTo ErrorHandler
Dim AcroApp As CAcroApp
Dim AVDoc As CAcroAVDoc
Dim PDDoc As CAcroPDDoc
Dim PDBookmark As CAcroPDBookmark

Set AcroApp = CreateObject('AcroExch.App')
AcroApp.Show

' Open the PDF
Set AVDoc = CreateObject('AcroExch.AVDoc')
If AVDoc.Open(strSourceFileName, '') <> True Then
RemoveBookmarkFromFile = False
Exit Function
End If

Set PDDoc = AVDoc.GetPDDoc
Set PDBookmark = CreateObject('AcroExch.PDBookmark')

' Get the bookmark requested
If (PDBookmark.GetByTitle(PDDoc, m_txtBookmarkToRemove) = True) Then
' Kill the bookmark
PDBookmark.Destroy

' Perform a full save - this will remove optimising/linearising. Use &H5 for

linearised
If PDDoc.Save(&H1, strSourceFileName) <> True Then
RemoveBookmarkFromFile = False
Exit Function
End If
End If

' Close the PDF
AVDoc.Close False

' Cleanup
Set PDDoc = Nothing
Set PDBookmark = Nothing
Set AVDoc = Nothing
Set AcroApp = Nothing

RemoveBookmarkFromFile = True
Exit Function

'////////////////////////////////////////////////////
ErrorHandler:
' All other errors handled here
If IsCriticalError Then
RemoveBookmarkFromFile = False
Exit Function
Else
Resume
End If
End Function

'///////////////////////////////////////////////////
'///////////////////////////////////////////////////

Private Function IsCriticalError() As Boolean
Dim strErrorMessage As String
Select Case Err.Number ' Evaluate error number.
Case Else
strErrorMessage = 'Please contact

info@planetpdf.com and inform them that' & Chr$(13) & _
'the error message reported by the operating system

was ' & Chr$(13) & _
Chr$(34) + Trim(Str(Err.Number)) & ' ' &

Err.Description + Chr$(34)
MsgBox strErrorMessage, , 'Conversion error' +

Str(Err.Number)
IsCriticalError = True
Exit Function
End Select
IsCriticalError = False
End Function

'///////////////////////////////////////////////////
'///////////////////////////////////////////////////

Private Sub btnRemoveBookmarks_Click()
Dim strFile As String
Dim strFolder As String

' Set the source folder
strFolder = m_txtSourceFolder

' Grab the first file to process
strFile = Dir(strFolder + '*.pdf')

' Loop through all word files
While strFile <> ''

' Attempt to convert the file to PDF
If (RemoveBookmarkFromFile(strFolder + strFile) = False) Then
' Hmmm, looks like something went wrong - let's prompt the user to

see if they wish to quit
If (MsgBox('There has been a problem with processing the file ' +

strFile + Chr(13) + 'Do you wish to exit', vbYesNo) = vbYes) Then
' Finish up - let's get out of here
Exit Sub
End If
End If

' Grab the next file
strFile = Dir
Wend
End Sub

'///////////////////////////////////////////////////
'///////////////////////////////////////////////////

You May Also Like

About the Author: Planet PDF Team

Leave a Reply