Can vba automation be used to view a subset of pages in the same PDF document (ie to hide irrelevant blocks of pages)?

Posted on 2014-09-01
Last Modified: 2014-09-01
My excel 2010 piece uses vba to open a PDF file and display the page with a defined page number.

Is it possible to HIDE some pages in a document of (say) 100 pages?
For example, if I wish to display pages 40-50, and could HIDE pages 1-39 and 51-100, then the reader could not scroll onto irrelevant pages. However, for another consultation, I would wish to pick another block of pages to read, and hence a different block of pages to hide.

Can this be done, perhaps through Layers? If so, would those layers be definable dynamically, or would they have to be created when the PDF file is created?

Question by:Kelvin4
    LVL 26

    Accepted Solution

    The only option as I see it is automating Acrobat Professional (not reader) thru VBA to extract the pages you need to a new pdf document.
    Sub ExtractPDFPages(strSourceFullPath As String, strDestinationFullPath As String, iStartPage As Integer, iNumPages As Integer)
        Dim PDDocSource As Object, PDDocTarget As Object
        Set PDDocSource = CreateObject("AcroExch.PDDoc")
        Set PDDocTarget = CreateObject("AcroExch.PDDoc")
        ' Create a new PDDoc
        If PDDocTarget.Create <> True Then
            MsgBox "Unable to create a new PDF"
            Exit Sub
        End If
        ' Open the PDF source file (the file we are going to take pages from)
        If PDDocSource.Open(strSourceFullPath) <> True Then
            MsgBox "Unable to open the source PDF"
            Exit Sub
        End If
        ' Set the page range you wish to extract
        ' Don't forget that this is zero based
        iStartPage = 0
        ' Set the number of pages you wish to extract
        iNumPages = 2
        ' Insert the pages from the source PDF file to the target PDF
        If PDDocTarget.InsertPages(-1, PDDocSource, iStartPage, iNumPages, False) <> True Then
            MsgBox "Unable to insert the source pages"
            Exit Sub
        End If
        ' Save the new file
        If PDDocTarget.Save(&H1, strDestinationFullPath) <> True Then
            MsgBox "Unable to save the pdf"
            Exit Sub
        End If
        'Close the PDF files
        ' Clean up
        Set PDDocSource = Nothing
        Set PDDocTarget = Nothing
    End Sub

    Open in new window

    Sample usage:
    ExtractPDFPages "C:\Expert-Exchange Demos\Source.pdf", "C:\Expert-Exchange Demos\Destination.pdf", 40, 11

    Open in new window

    Will extract pages 40-50 to a new pdf.

    Author Closing Comment

    Just the information I sought - many thanks!
    LVL 26

    Expert Comment

    My pleasure.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    This article is in response to a question here ( at Experts Exchange. The Original Poster has a scanned signature and wants to make the background transparent so that the signature may be pl…
    Update 21-May-2015: I temporarily removed the source code to make major changes to the program. Regards, Joe In a previous Experts Exchange article, How To Rename-Move a Batch of PDF Files Based on Contents of the Files (http://www.experts-exchan…
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    Sometimes we receive PDF files that are in the wrong orientation. They may be sideways or even upside down. This most commonly happens with scanned or faxed documents. It is possible to rotate the view of these PDFs with the free Adobe Reader produc…

    746 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