Solved

Acrobat Professional Delete pages based on Excel File

Posted on 2013-01-04
7
1,333 Views
Last Modified: 2013-01-08
Hi,

I have a 2,000 page PDF and I have adobe professional.  I need to delete pages out of this file if they contain a number which is found in an MS Excel file.  

The MS excel file is 2003 format. It contins a list of numbers. Any page in the PDF file with one of these numbers needs to be deleted.

I'm Guessing this is only possible with Javascript which I am mildly familiar with, so I have no idea how to do this.  

Thanks for your help.

Josh
0
Comment
Question by:joshcallahan1
  • 3
  • 3
7 Comments
 
LVL 7

Expert Comment

by:whosbetterthanme
Comment Utility
This article can help you:

http://commonsenseatheism.com/?p=8335
0
 

Author Comment

by:joshcallahan1
Comment Utility
Not quite what I mean.  I think there may be several ways to accomplish this.

-Print the excel sheet (which is a list of numbers) into pdf and use it to create an index to the pages.  I don't know how to do that.

-I think then it would be possible to delete all pages with bookmark desinations on them.

I'm not sure how to do it but this would be my best guess.  If someone could send me some javascript for this it would be great.  

Or send another Idea on how to solve this one. Thansk =)
0
 
LVL 26

Accepted Solution

by:
redmondb earned 500 total points
Comment Utility
Hi, joshcallahan1.

Please see attached. You'll need to change the Adobe reference and also the code to point to the input and output files. I've not done much with PDF's in VBA so please be careful!

The code is...
Option Explicit

Sub Delete_PDF_Pages()
' Adobe code based on http://vbcity.com/forums/t/51200.aspx
Dim xMsg           As String
Dim xInput         As String
Dim xOutput        As String
Dim xResponse      As Long
Dim xLast_Row      As Long
Dim xErrors        As Long
Dim xDeleted       As Long
Dim i              As Long
Dim j              As Long
Dim AcroApp        As CAcroApp
Dim AcroPDDoc      As CAcroPDDoc
Dim AcroHiliteList As CAcroHiliteList
Dim AcroTextSelect As CAcroPDTextSelect
Dim xarray()       As Variant
Dim PageNumber     As Variant
Dim PageContent    As Variant
Dim xContent       As Variant

xInput = "D:\TestPages.pdf"
xOutput = "D:\TestPages_Output.pdf"

xLast_Row = [A1].SpecialCells(xlLastCell).Row
ReDim xarray(xLast_Row)

xResponse = MsgBox("About to delete all pages which contain values from the range A1:A" & xLast_Row & Chr(10) _
            & Chr(10) & "Input:" & Chr(9) & xInput _
            & Chr(10) & "Output:" & Chr(9) & xOutput _
            & Chr(10) & Chr(10) & "('OK' to continue, 'Cancel' to quit.)", vbOKCancel, "Delete Pages")
If xResponse = 2 Then
    MsgBox "User chose not to continue. Run terminated."
    Exit Sub
End If

' Files and data OK?
If Dir(xInput) = "" Then xMsg = "Input file not found - " & xInput & Chr(10)
If Dir(xOutput) <> "" Then xMsg = "Output file exists - " & xOutput & Chr(10)
xarray = Application.Transpose(Range("A1:A" & xLast_Row))
For i = 1 To xLast_Row
    If Not IsNumeric(xarray(i)) Or xarray(i) = "" Then
        xMsg = "Non-numeric ""Delete"" value of """ & xarray(i) & """ found on row " & i & Chr(10)
        Exit For
    End If
Next
If xMsg <> "" Then
    MsgBox (xMsg & Chr(10) & "Run cancelled.")
    Exit Sub
End If

' Open the PDF...
Set AcroApp = CreateObject("AcroExch.App")
Set AcroPDDoc = CreateObject("AcroExch.PDDoc")
If AcroPDDoc.Open(xInput) <> True Then
    MsgBox (xInput & " couldn't be opened - run cancelled.")
    Exit Sub
End If

' Read each page...
For i = AcroPDDoc.GetNumPages - 1 To 0 Step -1

    Set PageNumber = AcroPDDoc.AcquirePage(i)
    Set PageContent = CreateObject("AcroExch.HiliteList")

    'Get up to 9,999 words from page...
    If PageContent.Add(0, 9999) <> True Then
        
        Debug.Print "Add Error on Page " & i + 1
        xErrors = xErrors + 1
    
    Else

        Set AcroTextSelect = PageNumber.CreatePageHilite(PageContent)
    
        If Not AcroTextSelect Is Nothing Then
            xContent = ""
            For j = 0 To AcroTextSelect.GetNumText - 1
                xContent = xContent & AcroTextSelect.GetText(j)
            Next j
            For j = 1 To xLast_Row
                If InStr(1, xContent, xarray(j)) > 0 Then
                    Debug.Print "Page " & i + 1 & " contains " & xarray(j) & " - " & xContent
                    ' To avoid problems with the delete...
                    Set AcroTextSelect = Nothing
                    Set PageContent = Nothing
                    Set PageNumber = Nothing
                    If AcroPDDoc.DeletePages(i, i) = False Then
                        MsgBox ("Error deleting page " & i + 1 & " - run cancelled.")
                        Exit Sub
                    End If
                    xDeleted = xDeleted + 1
                    Exit For
                End If
             Next
        End If
        
    End If

Next i

If AcroPDDoc.Save(PDSaveFull, xOutput) = False Then
    MsgBox "Cannot save the modified document"
    Exit Sub
Else
    MsgBox (xDeleted & " pages deleted. (" & xErrors & " errors.)")
End If
    
AcroPDDoc.Close
AcroApp.Exit

End Sub

Open in new window

Regards,
Brian.Delete-PDF-Pages.xlsTestPages.pdf
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Closing Comment

by:joshcallahan1
Comment Utility
Wow!! just ran this as a macro out of Excel after changing the source PDF files and it worked on the first try.  Best answer I've gotten on EE so far.  Thanks!!
0
 
LVL 26

Expert Comment

by:redmondb
Comment Utility
Thanks, joshcallahan1, glad to help!
0
 

Author Comment

by:joshcallahan1
Comment Utility
Oh I forgot to mention that in the VBA editor for the Excel file I had to go to Tools>References> and I clicked on the the various Adobe options, it won't work without that.
0
 
LVL 26

Expert Comment

by:redmondb
Comment Utility
Thanks, joshcallahan1. I'd cryptically mentioned that (You'll need to change the Adobe reference), but your post is much clearer for anyone coming across this.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Inserting page numbers in Portable Document Files not only enhances manageability but also makes them look professional. With numbered pages, the file appears more organized and it becomes easier to search for a particular page. The size and the vol…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now