Copy link to clipboard
Copied
For below spltePDF function(from Adobe forum), it works fine for most of PDFs, but for some PDF (pdf is editable, no any protection, no image) can't be inserted by .InsertPages() function and return false, but he the PDDoc2.DeletePages function works welll. who can tell why?
Function SplitPDF(MyPath As String, strFileName As String) As Integer
Dim PDDoc As Acrobat.CAcroPDDoc, newPDF As Acrobat.CAcroPDDoc
Dim PDPage As Acrobat.CAcroPDPage
Dim thePDF As String, PNum As Long
Dim NewName As String
Dim i As Integer
Dim Result As Variant
Dim Filename As String
'...
If VBA.Right(VBA.Trim(MyPath), 1) <> "\" Then
MyPath = MyPath + "\"
End If
thePDF = MyPath & strFileName
Set PDDoc = CreateObject("AcroExch.pdDoc")
Result = PDDoc.Open(thePDF)
If Not Result Then
MsgBox "Can't open file: " & Filename
Exit Function
End If
'...
PNum = PDDoc.GetNumPages
For i = 0 To PNum - 1
Set newPDF = CreateObject("AcroExch.pdDoc")
newPDF.Create
NewName = VBA.Left(strFileName, VBA.Len(strFileName) - 4) & "_SplitPage_" & (i + 1) & "_of_" & PNum & ".pdf"
'newPDF.InsertPages -1, PDDoc, i, 1, 0
'startPage, _pdDoc, startPage, numpage, 0
If newPDF.InsertPages(0, PDDoc, i, 1, 0) = False Then
Set PDDoc2 = CreateObject("AcroExch.pdDoc")
Result = PDDoc2.Open(thePDF)
PDDoc2.DeletePages 0, i - 1
PDDoc2.DeletePages i + 1, PNum - 1
PDDoc2.Save 1, MyPath & NewName
PDDoc2.Close
Set PDDoc2 = Nothing
Else
'PDDoc.Save 1, MyPath & NewName
newPDF.Save 1, MyPath & NewName
newPDF.Close
Set newPDF = Nothing
Set PDDoc = Nothing
End If
Next i
SplitPDF = PNum
End Function
Copy link to clipboard
Copied
The parameters you're supplying to the method are incorrect. Read its documentation very carefully.
Copy link to clipboard
Copied
What can you see at the properties of this documents?
Copy link to clipboard
Copied
I realise this is two years old now but I have the same problem. The code above seems to me to be written in accordance with the documentation. In my case, insertPages fails without inserting any pages. I'm using C# and have this:
var pdf = new Acrobat.AcroPDDoc();
pdf.Open(f.FullName); // some PDF file with many pages
var pdfNew = new Acrobat.AcroPDDoc();
pdfNew.Create();
pdfNew.InsertPages(1, pdf, 0, 2, 0); // tried various parameters here
Console.WriteLine(pdfNew.GetNumPages()); // always returns zero
Copy link to clipboard
Copied
Why do you post this in the forum for Acrobat Javascript? You don't use Javascript.
Copy link to clipboard
Copied
It is JavaScript, actually. The VBA object can execute JS code.
Copy link to clipboard
Copied
Actually, I think you're right. They seem to be using methods of the VBA object itself, instead of running JS code.
In that case this question should be asked over at the Acrobat SDK forum.