19
Apr
11

Inserting text onto an existing PDF


This page describes the process of taking an existing PDF file and inserting page numbers on each page.

Requirements

  • Java 5
  • Maven 2 – tutorial available on site nav bar

Project Setup

Create a project using Maven archetype. Open up the command prompt and navigate to an empty directory.

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart
groupId: com.test
artifactId: iTextTest

Answer the rest of the questions with defaults “Just hit the enter key”

Open up the pom.xml file and insert the following into the dependency section.

<dependency>
    <groupId>com.lowagie</groupId>
    <artifactId>itext</artifactId>
    <version>2.1.7</version>
</dependency>

If you are using eclipse then regenerate your project

mvn eclipse:clean eclipse:eclipse

Refresh the project and you should have all the dependencies necessary to proceed.

Implementation

The following program takes a PDF file via standard input and splits out the modified pdf to standard out.

package com.test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

/**
 * The following application inserts a page number on to each page of the PDF.
 * cat [pdf_file] | mvn -q exec:java -Dexec.mainClass="com.test.InsertPageNumber" > [out.pdf]
 */
public class InsertPageNumber {

	public InsertPageNumber() {
		stampPageNumbers(System.in, System.out);
	}

	public void stampPageNumbers(InputStream is, OutputStream os) {
		try {
			PdfReader reader = new PdfReader(is);
			int n = reader.getNumberOfPages();

			PdfStamper stamp = new PdfStamper(reader, os);
			int i = 1;
		      PdfContentByte over;
		      
			BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA,
					BaseFont.WINANSI, BaseFont.EMBEDDED);

			while (i <= n) {
				over = stamp.getOverContent(i);
				over.beginText();
				over.setFontAndSize(bf, 18);
				over.showText("page " + i);
				over.endText();
				i++;
			}
			stamp.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String args[]) {
		new InsertPageNumber();
	}
}

Compile the program

mvn compile

Run the program

On the command prompt just type the following command…

cat f1040.pdf | mvn -q exec:java -Dexec.mainClass="com.test.InsertPageNumber" > out.pdf

On windows just remember to replace the “cat” command with “type”.

Open out.pdf using your favorite PDF reader and you should be able to see the new page numbers on the bottom left of the page.

Advertisements

0 Responses to “Inserting text onto an existing PDF”



  1. Leave a Comment

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 78 other followers

April 2011
S M T W T F S
« Mar   May »
 12
3456789
10111213141516
17181920212223
24252627282930

Blog Stats

  • 822,357 hits

%d bloggers like this: