combine two pdf files

In this program we are going to tell you how you can
read a pdf file and combine more than one pdf into one.
To make a program over this, firstly we need to import
some packages. Remember to make this program the first and foremost thing to
remember is to place the iText.jar in WEB-INF/lib of your web
application. The packages and classes we are talking about are java.io.* for input
output, com.lowagie.text.pdf.Document, com.lowagie.text.Rectangle,
com.lowagie.text.pdf.BaseFonti, com.lowagie.text.pdf.PdfContentByte,
com.lowagie.text.pdf.PdfImportedPage, com.lowagie.text.pdf.PdfWriter and
com.lowagie.text.pdf.PdfReader.
Now create a file named Comboine2page.
Remember the name of the file should be such that the reader can understand what
the program is going to perform. Inside the class declare a main method inside
which we are going to write the logic of our program.
First of all create a PdfReader object for
reading the pdf file. By the object of PdfReader we can get the number of
pages in the pdf file. Now make a object of class Document. The Document
describes a document's page size, margins, and other important attributes. It
works as a container for a document's chapters, sections, images, paragraphs,
and other content.
Now create a document writer that writes the equivalent
syntax for a document's content to a specific OutputStream. PdfWriter.getInstance()
creates a PDF document writer that writes PDF syntax to concerned
file by a FileOutputStream.
Now open the document by document.open(). By
the object of the PdfWriter we get the reference of PdfContentType.
It will be obtained by adding the method getDirectContent() to the
previously generated PdfWriter object. At last
closes the document by using the document.close(). The closing of the
document is important because it flushes and closes the OutputStream
instance.
The code of the program is given below:
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
public class Comboine2page {
public static void main(String[] args)throws Exception {
System.out.println("Combines two page");
PdfReader reader = new PdfReader("HelloWorldPdf.pdf");
int n = reader.getNumberOfPages();
Rectangle psize = reader.getPageSize(1);
float width = psize.height();
float height = psize.width();
Document document = new Document(new Rectangle(width, height));
PdfWriter Pdfwriter = PdfWriter.getInstance(document,
new FileOutputStream("combine2Page1.pdf"));
document.open();
PdfContentByte cb = Pdfwriter.getDirectContent();
int i = 0;
int p = 0;
while (i < n) {
document.newPage();
p++;
i++;
PdfImportedPage page1 = Pdfwriter.getImportedPage(reader, i);
cb.addTemplate(page1, .5f, 0, 0, .5f, 60, 120);
if (i < n) {
i++;
PdfImportedPage page2 = Pdfwriter.getImportedPage(reader, i);
cb.addTemplate(page2, .5f, 0, 0, .5f, width / 2 + 60, 120);
}
BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
BaseFont.CP1252,BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.setFontAndSize(bf, 19);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "page " + p
+ " of " + ((n / 2) + (n % 2 > 0? 1 : 0)), width / 2, 40, 0);
cb.endText();
}
document.close();
}
}
|
The output of the program is given below: 
Download
this example.

|