PDF Creation / Manipulation in JAVA

Writing to PDF is very easy with Lowagie’s iText package.

Program below demonstrates how easily one can create, update and manipulate pdf files (including pdf merging) in Java.

package com.kushal.util;

import java.io.FileOutputStream;
import java.util.HashMap;

import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class PDFManipulation {

public static void main(String[] args) {
try {
	// we create a reader for a certain document
	PdfReader reader = new PdfReader(
		"D:/GeneralWs/utilities/com/kushal/util/FormToFill.pdf");
	int n = reader.getNumberOfPages();
	// we create a stamper that will copy the document to a new file
	PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(
			"myOutPutFile.pdf"));
	// adding some metadata
	HashMap moreInfo = new HashMap();
	moreInfo.put("Author", "Kushal Paudyal");
	stamp.setMoreInfo(moreInfo);
	// adding content to each page
	int i = 0;
	PdfContentByte under;
	PdfContentByte over;

	Image img = Image
	.getInstance("D:/GeneralWs/utilities/com/kushal/util/Flood.jpg");

	BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN,
			BaseFont.WINANSI, BaseFont.EMBEDDED);
	img.setAbsolutePosition(400, 400);
	while (i < n) {
		i++;
		// watermark under the existing page
		under = stamp.getUnderContent(i);
		under.addImage(img);
		// text over the existing page
		over = stamp.getOverContent(i);
		over.beginText();
		over.setFontAndSize(bf, 18);
		over.setTextMatrix(30, 30);
		over.showText("page " + i);
		over.setFontAndSize(bf, 26);

		over.showTextAligned(Element.ALIGN_LEFT, 
		   "12N00607", 400, 730,0);
		over.endText();

	}
	// adding an extra page
	stamp.insertPage(1, PageSize.A4);
	over = stamp.getOverContent(1);
	over.beginText();
	over.setFontAndSize(bf, 18);
	over.showTextAligned(Element.ALIGN_LEFT,
		"DUPLICATE OF AN EXISTING PDF DOCUMENT", 30, 600, 0);
	over.endText();
	// adding a page from another document
	PdfReader rdr = new PdfReader("C:/secondFile.pdf");
	under = stamp.getUnderContent(1);
	under.addTemplate(stamp.getImportedPage(rdr, 3), 1, 0, 0, 1, 0,
			0);
	// closing PdfStamper will generate the new PDF file
	stamp.close();
} catch (Exception de) {
	de.printStackTrace();
}
}
}

[My Video Promotion]
Tagged , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to PDF Creation / Manipulation in JAVA

  1. Ranganadh says:

    Hello,

    You can also use the product DynamicPDF for Java from ceTe Software Pvt Ltd. This product will allow one to create PDF documents programmatically from scratch, merge existing PDF documents, add contents to existing PDF files, stamping PDFs, appending existing PDF documents, form filling, rotating and scaling PDFs, etc..

    Following is the code for creating a sample PDF document with one page and a label on it.

    //Create the document object.
    Document document = new Document();

    //Create a page object.
    Page page = new Page();

    //Create a label element (page elements to add to the page).
    Label label = new Label(“ceTe Software”, 10, 10, 100, 20);

    //Add the page element to the page.
    page.getElements().add(label);

    //Add the page to the document.
    document.getPages().add(page);

    //Save the PDF.
    document.draw(“C:/Temp/testDoc.pdf”);

    It allows you to save the PDF to disk or open the PDF on the browser. You can also save the PDF to a stream or a byte array.

    Following is the sample code to merge an existing PDF document and filling the form field.

    //Merger existing PDF
    MergeDocument document = new MergeDocument (“C:/Temp/DocB.pdf”);

    //Fill the form field
    document.getForm().getFields().getFormField(“fieldName”).setValue(“ceTe Software”);

    //save the PDF
    document.draw(“C:/Temp/DocC.pdf”);

    You can download the fully functional Evaluation version from our website http://www.cete.com/Products/DynamicPDFforJava/Download.csp, you can go through our products at http://www.cete.com.

    Thanks,

    Ranganadh,
    ceTe Software Support Team.

  2.  java PDF says:

    I am using DynamicPDF as told by Ranganadh. But not from the company he mentioned here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.