Merging PDF Files with Java

in #programming6 years ago (edited)

Merging PDF files is a common task that we usually need to do in our daily activities. Of course, it is possible and fairly easy to merge PDF files in various applications, including Adobe Acrobat or other similar applications. But sometimes you need to automate this task and a programmatic way could save you a lot of trouble when you need to do this very frequently.

Merging PDF Files

As you know, Java added scripting support in Java SE 6. This means that you can use other programming languages on the Java platform.

The most common scripting language used with Java is JavaScript. For this reason, Java is shipped with an out-of-the-box JavaScript engine, called Nashorn.

Today, I needed to merge several PDF documents and I noticed that my old programs that I used to use for this task were, well, old. So I wrote a simple JavaScript file to do the task. It uses the excellent iText library originally written by Bruno Lowagie.

Here is the script:

// merge.js

var File = java.io.File;
var PdfDocument = com.itextpdf.kernel.pdf.PdfDocument;
var PdfReader = com.itextpdf.kernel.pdf.PdfReader;
var PdfWriter = com.itextpdf.kernel.pdf.PdfWriter;
var PdfMerger = com.itextpdf.kernel.utils.PdfMerger;

function merge(destFilename, sourceFilenames) {
  var file = new File(destFilename).getAbsoluteFile();
  file.getParentFile().mkdirs();
  var destDocument = new PdfDocument(new PdfWriter(file));

  var merger = new PdfMerger(destDocument);

  sourceFilenames.forEach(function (sourceFilename) {
    var sourceDocument = new PdfDocument(new PdfReader(new File(sourceFilename)));
    merger.merge(sourceDocument, 1, sourceDocument.getNumberOfPages());
    sourceDocument.close();
  });

  destDocument.close();
}

var destFilename = arguments[0];
var sourceFilenames = arguments.slice(1);

merge(destFilename, sourceFilenames);

When using this simple program, you should provide the destination filename as the first argument and the source filenames as the next arguments. For example, if your destination filename is dest.pdf and the source filenames are source1.pdf, source2.pdf, and source3.pdf, then you can run the program on Windows as follows:

set classpath=itext7-io-7.0.2.jar;itext7-kernel-7.0.2.jar;slf4j-api-1.7.2.jar
java -cp %classpath% jdk.nashorn.tools.Shell merge.js -- dest.pdf source1.pdf source2.pdf source3.pdf

Of course, this post was just about using the iText library. Setting the classpath and managing the dependencies is a vast topic in itself and depends on your development environment.

Sort:  

friends please shaman my steemit account, I want to help the family economy

https://steemit.com/hive-185836/@wahyudi2010/melakukan-power-up-untuk-pertama-kali

Assalamu alaikum sir,
How are you?

Hi. Thank you, I'm fine. Just a little busy with my translation works.

Sounds good. i am also little bit busy with my job. But it's high time to get some benefit from it. i am really missing the opportunity.

thanks for share just an informative and helping post

this i really best way

r u ok??? long time no post ...

Thank you. I am just fine. I am currently busy with my day job. Thank you for asking, though. I hope you are doing fine, and I wish you all the best on Steemit!

thank you very much .. & hope u will be available soon. Allah hafez

ေက်းဇူးတင္ပါတယ္ခင္ဗ်ာthank you

Great work

Itext library is great for java and c# library (contains of 4 dlls) called iTextSharp is based on this java library .But greater is use java packages from script languages that i saw here ! thanks for sharing

Thank you. For quick tasks I prefer to use JavaScript over Java, because it is easier to write the code and run.

thank you many many for upvote me..

badol how are you keep in touch

good how are you brother. keep in touch and great to help each other.

Thanks for the valuable posts on your page
I am grateful to be here in the community with you with love and respect No monopoly No racism No discrimination This is what we need Love, appreciation, respect and cooperation
I will be happy to join my page and support me permanently
Greetings

great! thanks for the info 😻

@AdGoggleKo Just tweeted your post. 😊


That's great! Thank you.

salam. mamnon babate hemayatetun. movafagh bashid.

سلام. ممنون از لطف شما. موفق باشید.

درود عرض ادب و احترام
تشکر بابت حمایتتون اقای دکتر❤🌷

سلام. خواهش می‌کنم. موفق باشید.

Coin Marketplace

STEEM 0.30
TRX 0.11
JST 0.033
BTC 64104.40
ETH 3148.52
USDT 1.00
SBD 4.25