Posts Tagged ‘itext

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.




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

Join 75 other followers

April 2017
S M T W T F S
« Mar    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Blog Stats

  • 807,309 hits