Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Acrobat Professional Delete pages based on Excel File

Posted on 2013-01-04
7
Medium Priority
?
1,697 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
ID: 38744682
This article can help you:

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

Author Comment

by:joshcallahan1
ID: 38745208
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 2000 total points
ID: 38748552
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:joshcallahan1
ID: 38751692
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
ID: 38751964
Thanks, joshcallahan1, glad to help!
0
 

Author Comment

by:joshcallahan1
ID: 38755977
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
ID: 38757576
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

824 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