# Planet PDF Forum Archive

Wowsers! This is page is old, head to the LIVE Planet PDF Forum. It features more than 10 conferences, covering everything from beginner to in-depth developer and pre-press discussions. If you wish to continue... one & two archive covers 1999-2011 (160,000 pages).

New Forum | Previous | Next | (P-PDF) Developers

Topic: Split PDF into many files - optimization problem
Conf: (P-PDF) Developers, Msg: 59356
From: ptribulski
Date: 5/29/2002 05:38 PM

I was trying to optimize an VB/IAC "Split PDF into many files" process by re-using a temp PDF file.  I ran into a bit of a problem with the optimization.

Originally if was using code to open a "BASE" file,
and for each page opening a blank pdf, inserting the page from the BASE, saving and closing the new PDF as a new name. This works, but is slow:
====================================================       Dim pddoc  As Acrobat.CAcroPDDoc
Dim avNewDoc As Acrobat.CAcroAVDoc
Dim pdNewDoc  As Acrobat.CAcroPDDoc

filename = "c:\big.pdf"
EmptyFileName = "c:\oneblankpage.pdf"
Workfile = "c:\Page"

Set avDoc = CreateObject("AcroExch.AVDoc")
avDoc.Open filename, "BASE FILE"
Set pddoc = avDoc.GetPDDoc
lastPageNum = pddoc.GetNumPages -1

Set avNewDoc = CreateObject("AcroExch.AVDoc")

for ThisPageNum = 0 to lastPageNum
avNewDoc.Open EmptyFileName, "WORK PDF"
'Note that EmptyFileName is an Existing PDF with one blank page

Set pdNewDoc = avNewDoc.GetPDDoc
pdNewDoc.InsertPages 0, pddoc, ThisPageNum, 1, True
pdNewDoc.DeletePages 0, 0 'get rid of first "dummy page"
TempfileName = WorkFile & ThisPageNum & ".pdf"
pdNewDoc.Save(&H1, TempFilename)

avNewDoc.Close True
Next ThisPageNum

Set avNewDoc = Nothing
avDoc.Close (True)
Set avDoc = Nothing
====================================================

I encountered such a performance penalty opening the TEMP file each time, and got a little cocky. I modified the code to reuse the open TEMP file.  I knew I was in for some trouble if I did not use the PDSaveCollectGarbage (&H32) on the save, so I added that to the logic. The resulting files still get pretty big as I progress along, as if I am not releasing resources for the pages I delete along the way.  Here is the "optimized" code which will produce some pretty large one page PDF files:
====================================================
Dim pddoc  As Acrobat.CAcroPDDoc
Dim avNewDoc As Acrobat.CAcroAVDoc
Dim pdNewDoc  As Acrobat.CAcroPDDoc

filename = "c:\big.pdf"
EmptyFileName = "c:\oneblankpage.pdf"
WorkFile = "c:\Page"

Set avDoc = CreateObject("AcroExch.AVDoc")
avDoc.Open filename, "BASE FILE"
Set pddoc = avDoc.GetPDDoc
lastPageNum = pddoc.GetNumPages - 1

Set avNewDoc = CreateObject("AcroExch.AVDoc")
avNewDoc.Open EmptyFileName, "WORK PDF"

For ThisPageNum = 0 To lastPageNum
Set pdNewDoc = avNewDoc.GetPDDoc

pdNewDoc.InsertPages 0, pddoc, ThisPageNum, 1, True
pdNewDoc.DeletePages 0, 0 'get rid of the page from the "last" pass
TempFilename = WorkFile & Trim(Str(ThisPageNum)) & ".pdf"
pdNewDoc.Save &H1 + &H4 + &H2 + &H32, TempFilename

Next ThisPageNum

avNewDoc.Close True
Set avNewDoc = Nothing

avDoc.Close (True)
Set avDoc = Nothing

====================================================

Any thoughts on how to save the PDF file and release the resources for pages which are no longer included in the file?

Thanks,
Pete


### Debenu PDF Tools Pro

It's simple to use and will let you preview and edit PDF files, it's a Windows application that makes...

# Back to the past, 15 years ago! Open Publish 2002

Looking back to 2002, it's amazing how much of the prediction became a reality. Take a read and see what you think!

# Debenu PDF Aerialist

The ultimate plug-in for Adobe Acrobat. Advanced splitting, merging, stamping, bookmarking, and link control. Take Acrobat to the next level.

# Adding a PDF Stamp Comment

OK, so you want to stamp your document. Maybe you need to give reviewers some advice about the document's status or sensitivity. This tip from author Ted Padova demonstrates how to add stamps with the Stamp Tool along with related comments.