Merging PDF Files with Java

in #programming7 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:  
There are 3 pages
Pages

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.

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

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.

great! thanks for the info 😻

@AdGoggleKo Just tweeted your post. 😊


That's great! Thank you.

salam. mamnon babate hemayatetun. movafagh bashid.

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

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

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

Informative post

سلام اقای کیانی عزیز
استیم اپ هم داره برا اندروید ؟

سلام، شبتون بخیر.

بله، اپلیکیشن eSteem هست که در ابتدا به‌وسیله‌ی ‎ @good-karma نوشته شده است.

You are great man.

Why you are not doing posts now a days,?

Danke schön 💗 prima

سلام. پست های بسیار خوبی رو در وبلاگتون مطالعه کردم. امیدوارم مجددا فعالیتتون رو در این شبکه از سر بگیرید و به کارتون ادامه بدید چون واقعا کارتون درسته.

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

Dear sir ghasemkiani :
hello, i want to rent your SP, i want to rent about 20k of your SP. I have little hope .. I just send bidbot because nobody votes for me,...I hope you will reply to my message.^^
Thank you :D

Hi. Thank you for your message. Unfortunately, I have no plan for delegating my steem power. As you know, there are services that delegate steem power, like @blocktrades. I think you can use those services. Thank you again, and good luck!

sorry for disturbing you ^^
thanks for your advice.😄

Salam
mamnon as lotfeton

thank you very much for visiting my best friend

perfect, I am happy with successful people like you. please help me upvote. thank you

Thank you

Thanks for Your support and kindness sir :)God bless you.

Great article. Thanks for the upvotes as well!

asalamuallaikum thank you've shared

عرض ادب اقای دکتر
سری به بلاگ ما هم بزنید 🌷

bro support

دکتر با درصد بیشتری حمایتم کن
🙏

تشکر از لطفتون

Good post.

Thanks for information @ghasemkiani.

Are you thinking about to create Java classes of your tool?

I mean, it would great to import your project into Java to accomplish the task of merging, without dealing with JavaScript.

I think that's the logical thing to do. But for quick solutions, I prefer JavaScript. Java is more suitable for larger projects. In the recent years, I have mostly used JavaScript, so it is easier for me to write in JavaScript. As you can see, my programming posts are all about JavaScript.

Sadly

Online java teacher

thank you for your information master @ghasemkiani
I follow you to always get information that is very important information, such as information listed above.
you have a very remarkable talent master @ghasemkiani

thanks very much..

Very nice tip for the programmers. Thank you for sharing such information.

great got to practice

nice post bro

اسلام علیکم برادر

2018082615353023.jpg

Thank you so friend like my post, friend are you Arabic?

78208b3f Ghasemkiani @ghasemkiani upvote @freedomshift/a-contest-to-win-steem-basic-income-… (24.3%)

Thank you!

There is no post, blog or comment, for me to upvote you back.

Programming is a little bit confusing, but we will learn from your post.

Vehicle programming is really fascinating
The Java program is irreplaceable because it has a lot of benefits

Great info post will help more in daily life

Thanks for the information dear. but I can't do t myself CZ I'm really bad at programming ... my friend can help me from this. :)

Programing is difficult but is beneficial

Helpful post

you came back after a long time with awesome blog

Assalamoalaikum. Really helpful post bro. have a nice day

When you want start a new series? Waiting for your post, sir.

tnx a lot ,,, sir,,,, for visiting into my post,,,,,

Hi
Thank you for upvoting my post on bidbots.
When do you think you will be posting again?
I would like to return the favor.
Thanks
@shortsegments

https://steemit.com/steem/@shortsegments/steemit-tools-intro-to-bit-bots
Feel free to check out my latest posts: https://steemit.com/promo-steemit/@shortsegments/what-special-features-makes-steemit-wallet-secure

thanks for the upvote on my recent post!

عرض ادب و احترام
اگر ممکنه استیم چت رو چک بفرمایید🌷

good post thank you

Assalamualaikum Mr @ghasemkiani how are you and thank for Upvote and visit my blog

There are 3 pages
Pages