0% found this document useful (0 votes)
341 views

Modifying An Existing Document With IText PDF API in Java

This document discusses modifying existing PDF documents using the iText PDF API in Java. It explains that the PdfStamper class allows adding content like headers, footers, and watermarks to existing PDFs. The document provides an example code that uses a PdfStamper to add a watermark image to each page of a PDF file. It also describes how to get the under or over content of a page using the PdfStamper to add content below or above the existing content.

Uploaded by

strokenfilled
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
341 views

Modifying An Existing Document With IText PDF API in Java

This document discusses modifying existing PDF documents using the iText PDF API in Java. It explains that the PdfStamper class allows adding content like headers, footers, and watermarks to existing PDFs. The document provides an example code that uses a PdfStamper to add a watermark image to each page of a PDF file. It also describes how to get the under or over content of a page using the PdfStamper to add content below or above the existing content.

Uploaded by

strokenfilled
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

17/5/2014 Modifying an existing document with IText PDF API in Java.

http://tutorials.jenkov.com/java-itext/modifying-pdf-documents.html 1/3
Java PDF
Generation with
IText
Introduction
Getting Started
Document
Chunk
Phrase
Paragraph
Chapter + Section
Anchor
List
Tables
Images
Superscript +
Subscript
Underline +
Strikethrough
Fonts

Modifying Existing
PDF Documents

By Jakob
Jenkov
11
Share on Facebook
Java IText: Modifying
Existing PDF Documents
Connect with me:

IText can modify existing PDF files in many different ways. Here I'll just
cover one of the most used modifications - stamping an existing PDF
with text or images. Get the book "IText in Action" to get the full story
on manipulating existing PDF documents.
If you already have a finished PDF, and just want to add a header, footer
or watermark to it, IText provides the
com.itextpdf.pdf.PdfStamper class.
First you read the existing document using a PdfReader, then modify
it using the PdfStamper.
Here is a simple code example:
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.FileOutputStream;
import java.io.IOException;
public class PdfStamperExample {
public static void main(String[] args) {
Jenkov.com
Books Tutorials About RSS
17/5/2014 Modifying an existing document with IText PDF API in Java.
http://tutorials.jenkov.com/java-itext/modifying-pdf-documents.html 2/3
Get all my free
tips & tutorials!
Connect with me,
or sign up for my
news letter or RSS
feed, and get all my
tips that help you
become a more
skilled and efficient
developer.

Newsletter
First Name *
Last Name *
try {
PdfReader pdfReader = new PdfReader("HelloWorld.pdf");
PdfStamper pdfStamper = new PdfStamper(pdfReader,
new FileOutputStream("HelloWorld-Stamped.pdf"));
Image image = Image.getInstance("watermark.png");
for(int i=1; i<= pdfReader.getNumberOfPages(); i++){
PdfContentByte content =
pdfStamper.getUnderContent(i);
image.setAbsolutePosition(100f, 700f);
content.addImage(image);
}
pdfStamper.close();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
Adding Content to the PDF
Document
To add content to the document you need to access to a
PdfContentByte from the PdfStamper. You can add content either
above or below the existing content in the PDF document. Here is how
you obtain the PdfContentByte from the PdfStamper.
PdfContentByte underContent = pdfStamper.getUnderContent(1);
PdfContentByte overContent = pdfStamper.getOverContent(1);
The number passed as parameter is the page number of the page to get
the under or over content for.
The PdfContentByte object has methods for adding all kinds of
content to a PDF including text, graphics, images etc.
17/5/2014 Modifying an existing document with IText PDF API in Java.
http://tutorials.jenkov.com/java-itext/modifying-pdf-documents.html 3/3
Email *
Yes, give me tips!
Connect with
me:


Newsletter - Get all my free tips!
First Name * Last Name *
Email *
Yes, give me tips!
First Media TV Internet
firstmedia.com
Pasang TV Kabel & Internet di Rumah Internet Super Cepat Up to 32 Mbps

You might also like