Extracting pages from one PDF and adding them into new PDF
Hello,
I am looking to extract PDF pages from an existing document into a new document using VB.NET and the Adobe Acrobat Library/API (Interop.Acrobat). Essentially, I want to take a large amount of pages and break them down into separate documents. For example. I want to break down a 100 page document into 2 50 page documents, etc.
I have written some code below; once I can write into a single PDF, I will them loop and write out to as many PDF documents as I need. Line 33 returns nothing, as I assume the underlying document I am attempting to access has not been created. The other code I have written accesses an already created document using the .Open() method. What do I need to do in order to create the document object before I step to Line 33? I do not see any methods under _acroDoc2 to create a new PDF, only to Open() existing documents.
My code is as follows:
Dim filePath = "Path/To/Source/File"
'create adobe and get document
Dim _acroDoc As New Acrobat.AcroAVDoc()
Dim _myAdobe As New Acrobat.AcroApp()
_myAdobe.Hide()
Dim _pdDoc As Acrobat.AcroPDDoc
_pdDoc = Nothing
_acroDoc.Open(filePath, "sourceFile")
_pdDoc = DirectCast(_acroDoc.GetPDDoc(), Acrobat.AcroPDDoc)
'retrieve total page count from document
Dim pageCount = _pdDoc.GetNumPages()
'determine how many pages per document
'Dim pdfPageLength = Math.Floor(maxPagesAllowed / letterRecord.DocumentPageLength) * (letterRecord.DocumentPageLength)
Dim pdfPageLength = 50
'create new instance of adobe to extract pages from original instance
Dim _acroDoc2 As New Acrobat.AcroAVDoc()
Dim _myAdobe2 As New Acrobat.AcroApp()
_myAdobe2.Hide()
'get second empty document instance to write a new PDF
Dim _pdDoc2 As New Acrobat.AcroPDDoc
_pdDoc2 = DirectCast(_acroDoc2.GetPDDoc(), Acrobat.AcroPDDoc) 'this line equals nothing as document isnt instantiated
Dim startPage As Double = 0
Dim endPage As Double = pdfPageLength
'insert pages from original PDF document instance
_pdDoc2.InsertPages(startPage, _pdDoc, startPage, endPage, 0)
'save and close
Dim savePath = "C:\Users\myusername\Desktop\newPDF.pdf"
Dim savemergefile = _pdDoc2.Save(1, savePath)
