Anda di halaman 1dari 2

iText in Action: example part2.chapter06.

StampStationery Page 1 of 2

About us  |  News  |  Contact

iText ® Licenses Support Search Go

You are here: Home > Support > Book > Chapter 6 > StampStationery

iText in Action 2nd Edition


The StampStationery example is part of the book iText in Action (ISBN 9781935182610).
It's a small standalone application. You can use this example for inspiration, but please buy the book if there's something you don't understand
about the example. Read Chapter 6 for more info. Note that this example depends on DatabaseConnection, HsqldbConnection, Movie,
PojoFactory, FilmFonts, PojoToElementFactory, MovieComparator, Stationery, and maybe some other examples.

Chapter 6: Working with existing PDFs


Keywords for this example: Existing PDFs, PdfReader, PdfStamper, Stationery, Watermark, Direct content > Under / over content
If you want this example to work, you need the following jars: iText.jar, hsqldb.jar
This example uses the following resources: filmfestival.script.
This example is used as a helper class for: InsertPages

part2.chapter06.StampStationery
If you compile and execute this example, you'll get the following result:
original.pdf
stamped_stationary.pdf
You can download the full source code of StampStationery, or read it here:
 
/*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
 
package part2.chapter06;
 
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;
import java.util.TreeSet;
 
import com.lowagie.database.DatabaseConnection;
import com.lowagie.database.HsqldbConnection;
import com.lowagie.filmfestival.FilmFonts;
import com.lowagie.filmfestival.Movie;
import com.lowagie.filmfestival.MovieComparator;
import com.lowagie.filmfestival.PojoFactory;
import com.lowagie.filmfestival.PojoToElementFactory;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
 
public class StampStationery {
/** The original PDF file. */
public static final String ORIGINAL
= "results/part2/chapter06/original.pdf";
/** The resulting PDF. */
public static final String RESULT
= "results/part2/chapter06/stamped_stationary.pdf";
 
/**
* Main method.
* @param args no arguments needed
* @throws DocumentException
* @throws IOException
* @throws SQLException
*/
public static void main(String[] args)
throws IOException, DocumentException, SQLException {
Stationery.createStationary(Stationery.STATIONERY);
StampStationery stationary = new StampStationery();
stationary.createPdf(ORIGINAL);
stationary.manipulatePdf(ORIGINAL, Stationery.STATIONERY, RESULT);
}
 
/**
* Manipulates a PDF file src with the file dest as result
* @param src the original PDF
* @param stationery a PDF that will be added as background
* @param dest the resulting PDF
* @throws IOException
* @throws DocumentException
*/
public void manipulatePdf(String src, String stationery, String dest)
throws IOException, DocumentException {
// Create readers
PdfReader reader = new PdfReader(src);

http://itextpdf.com/examples/iia.php?id=119 3/3/2011
iText in Action: example part2.chapter06.StampStationery Page 2 of 2

PdfReader s_reader = new PdfReader(stationery);


// Create the stamper
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
// Add the stationery to each page
PdfImportedPage page = stamper.getImportedPage(s_reader, 1);
int n = reader.getNumberOfPages();
PdfContentByte background;
for (int i = 1; i <= n; i++) {
background = stamper.getUnderContent(i);
background.addTemplate(page, 0, 0);
}
// CLose the stamper
stamper.close();
}
 
/**
* Creates a PDF document.
* @param filename the path to the new PDF document
* @throws DocumentException
* @throws IOException
* @throws SQLException
*/
public void createPdf(String filename)
throws SQLException, IOException, DocumentException {
// Create a database connection
DatabaseConnection connection = new HsqldbConnection("filmfestival");
// step 1
Document document = new Document(PageSize.A4, 36, 36, 72, 36);
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
Statement stm = connection.createStatement();
ResultSet rs = stm.executeQuery(
"SELECT country, id FROM film_country ORDER BY country");
while (rs.next()) {
document.add(new Paragraph(rs.getString("country"), FilmFonts.BOLD));
document.add(Chunk.NEWLINE);
Set<Movie> movies =
new TreeSet<Movie>(new MovieComparator(MovieComparator.BY_YEAR));
movies.addAll(PojoFactory.getMovies(connection, rs.getString("id")));
for(Movie movie : movies) {
document.add(new Paragraph(movie.getMovieTitle(), FilmFonts.BOLD));
if (movie.getOriginalTitle() != null)
document.add(new Paragraph(movie.getOriginalTitle(), FilmFonts.ITALIC));
document.add(new Paragraph(
String.format("Year: %d; run length: %d minutes",
movie.getYear(), movie.getDuration()), FilmFonts.NORMAL));
document.add(PojoToElementFactory.getDirectorList(movie));
}
document.newPage();
}
// step 5
document.close();
// Close the database connection
connection.close();
}
}
 

Content © 2010 1T3XT BVBA

http://itextpdf.com/examples/iia.php?id=119 3/3/2011

Anda mungkin juga menyukai