How to extract a page range from a PDF to an individual PDF file
- Download Visual Basic 6 project
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.
Option
Explicit
' Force variable declaration
' Company : Planet PDF
' URL : http://www.planetpdf.com/
' Date : 06 April 1998
' Updated : 09 March 1999
' Description: Sub btnExtractPage_Click()
' This vb method uses IAC to extract a page range from PDF
' document to a new PDF.
' In order to use this function you must have a file called
' 'Source.pdf' in your temp folder. This method/function
' should be extended to suit the requirements of an
' organisation
Private
Sub
btnExtractPage_Click(
)
Dim
PDDocSource As
Object
,
PDDocTarget As
Object Dim
iStartPage As
Integer
,
iNumPages As
Integer
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' End End
If
' Open the PDF source file (the file we are going to
' take pages from)
If
PDDocSource.
Open
(
'c:tempsource.pdf')
<> True
Then MsgBox 'Unable to open the source PDF' End End
If
' Set the page range you wish to extract
iStartPage = 0 ' Don't forget that this is zero based
' 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' End End
If
' Save the new file
If
PDDocTarget.
Save(
&H1,
_
'c:temptarget.pdf')
<> True
Then MsgBox 'Unable to save the pdf' End End
If
'Close the PDF files
PDDocSource.
Close PDDocTarget.
Close
' Clean up
Set
PDDocSource = Nothing Set
PDDocTarget = Nothing
End
Sub