Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5164
  • Last Modified:

Merge multiple pdf files into one file programmatically

Hi  khkremer helped me to programmatically set the pdf filename using registry settings. That is working fine.  Now I am trying to combine the files programmatically using the command prompt so I can put this into a batch script, which our application can call.

I downloaded pdftk and that works well on some pdf files but not all of them.  I'm not sure why.  I tested on two different pc's and the problem appears to be with certain pdf files.

I attached a screenshot of the message I sometimes get when I try to merge pdf files using pdftk.

Can anyone tell me why pdftk is not working on all pdf files?  Is there another way to programmatically merge the files?  We have Adobe Acrobat Professional 9.0.  The files that are bombing were created with Acrobat 9.0 also.  I did not use any security settings, no passwords, etc.

Thanks,
Alexis
pdftk-error.jpg
0
alexisbr
Asked:
alexisbr
  • 4
  • 4
1 Solution
 
Karl Heinz KremerCommented:
You can automate Acrobat with VB - more on that later if you are interested in that solution.

Can you provide one of the files that are causing problems? If you do not feel comfortable enough to put up the file publicly, you could also email one to me - my email address can be found on my profile page.
0
 
alexisbrAuthor Commented:
Yes, I attached one fo the files.  These are just test files with examples of how the real files will look and output in the same way the real files will output the data.  The real application takes too long to run each time so we are testing with a sample app that runs in a few seconds.

I can use VB as long as it can be run as vbscript like the other one you helped me with.  The app we are using is not Access so I can't use VBA but the app can call batch files.

Thanks again for your help.
Alexis

T50-1.pdf
0
 
Karl Heinz KremerCommented:
The file is a PDF1.6 file, and PdfTk does not support that version. I converted the file to PDF1.4 with the Acrobat 9 PDF Optimizer, and now PdfTk does process the file.
A big advantage of using Acrobat automation is that you do not have to deal with these problems.
As I said before, I am not familiar with VBScript, so I'm not the right person to come up with a solution, but I know my way around Acrobat, and I'm pretty sure that together we can come up with something that works.

Take a look at the IAC document at
http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/iac/IACReference.pdf 
http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/iac/IACOverview.pdf 

The overview document describes how to use the IAC API in general, and the reference document describes every API function. I will try to come up with a simple VBS sample that demonstrates some of these functions. Give me a day or two, and if you don't hear anything from me, just remind me that I still owe you some work.
0
Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

 
Karl Heinz KremerCommented:
Let's see how this works (ideally, you would merge the files in a loop, and not like I did it, but I did not want to bother with command line processing or arguments):

set WshShell = CreateObject ("Wscript.Shell")
 
Set AcroApp = CreateObject("AcroExch.App")
Set FirstDoc = CreateObject("AcroExch.PDDoc")
 
FirstDoc.Open "c:\tmp\1.pdf"
 
 
Set MergeDoc = CreateObject("AcroExch.PDDoc")
MergeDoc.Open "c:\tmp\2.pdf"
FirstDoc.InsertPages FirstDoc.GetNumpages - 1, MergeDoc, 0, MergeDoc.GetNumPages, True
MergeDoc.Close
 
MergeDoc.Open "c:\tmp\3.pdf"
FirstDoc.InsertPages FirstDoc.GetNumpages - 1, MergeDoc, 0, MergeDoc.GetNumPages, True
MergeDoc.Close
 
MergeDoc.Open "c:\tmp\4.pdf"
FirstDoc.InsertPages FirstDoc.GetNumpages - 1, MergeDoc, 0, MergeDoc.GetNumPages, True
MergeDoc.Close
 
FirstDoc.Save &H1, "c:\tmp\merged.pdf"
 
FirstDoc.Close
 
Wscript.Echo "Done"

Open in new window

0
 
alexisbrAuthor Commented:
Thanks, khkremer.  I understand what you have done here and I believe I can set up the loop to handle this.  Unfortunately, I won't be back to this client until next Thursday so I won't be able to test until then.

By the way, what is a PDF1.6 file?  Does Adobe Acrobat 9 always create files of that type?  It would have been nice to use the pdftk tool.  It worked really well and would have been very easy to set up with the existing logic.

I appreciate your help on all this pdf stuff.  I have never had to work with pdf files before and I have learned a lot.
Alexis
0
 
Karl Heinz KremerCommented:
There are different versions of PDF - a long time ago Adobe released it's first version as PDF-1.0. Then they added features and released 1.1... and so on. With every release of Acrobat, new features were added, and the version number got bumped. A valid PDF 1.4 file can still be processed by a PDF-1.6 processor, but not the other way around. Acrobat 8 and 9 can create PDF-1.7, but the default is PDF-1.6.

If your files are created via Distiller (or the Adobe PDF printer), you can set the default job options to a different PDF level. Just open the preferences for your Adobe PDF printer in the control panel and select the "General" tab, then click on the "Printing Preferences..." button. On the new dialog go to the "Adobe PDF Settings" tab and modify the "Default Settings". One way to do that is to select defaults that are close to what you need (e.g. start with "Standard"), then click the "Edit" button right next to it. On the next dialog set the "Compatibility" to either PDF 1.4 or 1.5. Save your settings under a new name and select that as your default setting.

Having written all that, I realized that the Standard settings already produce PDF 1.5, so I took another look at your file and sure enough, it's PDF 1.5 (which is compatible with Acrobat 6). I did it again... Every now and then I mix up the numbers - Acrobat 6 compatible becomes PDF 1.6, even though in reality it's PDF 1.5. So, that means we still don't know why pdftk does not like these files. Maybe PdfTk also does not like certain PDF 1.5 files... That got me thinking... What version of pdftk am I running? Quick check with "pdftk --version" reveals that it's 1.12 - is that the latest? Hmmm... it's the latest that's distributed via the accesspdf.com web site, but not latest you can find on http://www.pdfhacks.com/pdftk/ - that' is 1.41.

Once I upgraded pdftk, your file can be processed without any problem. So, just upgrade PdfTk (I'm pretty sure you have version 1.12 as well), and you should be fine.
0
 
alexisbrAuthor Commented:
Thanks.  I was hoping this was the case.  I will try upgrading pdftk when I am there on Thursday and will let you know.  I have already combined all the vbscript code (what you wrote and what I had already written) into one process that can be run from our application

Regards,
Alexis
0
 
alexisbrAuthor Commented:
khkremer,
You were right.  I downloaded pdftk from the link you provided and installed it and now the script works fine.

Thanks again for all your help.  If I have any other questions on this project, I will open another question.
Regards,
Alexis
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now