Women in Technology

Hear us Roar



Article:
  Dynamically Creating PDFs in a Web Application
Subject:   How to use iText template to fill data values on a Form?
Date:   2005-03-18 01:11:43
From:   baby2046
Response to: How to use iText template to fill data values on a Form?

/**
* $Id: PdfProcessor.java,v 1.9 2005/03/15 03:18:17 dongwen Exp $ Copyright (c)
* 2003-2004 SJNS CORPORATION
*/
package jp.co.tr.com.report;


import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;


import jp.co.tr.com.log.TRLogFactory;
import jp.co.tr.com.utils.ILogable;


import org.apache.commons.logging.Log;


import com.lowagie.text.Document;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.pdf.PRAcroForm;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;


/**
*

[–¼@Ì] PDF’ •[ì¬ƒc[ƒ‹


*

[‹@@”\] PDF’ •[ì¬ƒc[ƒ‹‚Æ‚·‚é


*
*

Copyright(c) SJNS 2003 2004


*/
public class PdfProcessor implements ILogable {


/**
* ƒƒOƒCƒ“ƒXƒ^ƒ“ƒX
*/
private static Log log = TRLogFactory.getLog(PdfProcessor.class);


/**
*

[–¼@Ì] ƒƒOƒCƒ“ƒXƒ^ƒ“ƒX‚̎擾


*

[‹@@”\] ƒƒOƒCƒ“ƒXƒ^ƒ“ƒX‚ðŽæ“¾‚·‚é


*
* @return Log ƒƒOƒCƒ“ƒXƒ^ƒ“ƒX
*/
public Log getLog() {
return log;
}


/**
*

[–¼@Ì] PDF’ •[ì¬


*

[‹@@”\] PDF’ •[ì¬‚Æ‚·‚é


*
* @param pdfForms PDF’ •[ƒf[ƒ^‚ƃeƒ“ƒvƒŒ[ƒg
* @param outStream PDF’ •[ƒtƒ@ƒCƒ‹‚̃AƒEƒg
*/
public PdfProcessor(List pdfForms, OutputStream outStream) {


// PDF’ •[‚ðì¬‚·‚é
try {
// PDF’ •[ƒhƒLƒ…ƒƒ“ƒg
Document mergedPDF = new Document();
// PDFƒhƒLƒ…ƒƒ“ƒg‚ðì¬
PdfCopy writer = new PdfCopy(mergedPDF, outStream);
// PDFƒhƒLƒ…ƒƒ“ƒg‚ðŠJ‚¯‚é
mergedPDF.open();


// –ˆPDF’ •[ƒy[ƒW‚ðì¬‚·‚é
Iterator iterator = pdfForms.iterator();


while (iterator.hasNext()) {


Object object = iterator.next();


// –ˆPDF’ •[ƒy[ƒW‚̃f[ƒ^‚ƃeƒ“ƒvƒŒ[ƒgƒtƒ@ƒCƒ‹ƒp[ƒX‚ðŽæ“¾‚·‚é
PDFForm pdfForm = (PDFForm) object;
String templateName = pdfForm.getPdfTemplateName();
Map valueMap = pdfForm.getData();


// –ˆPDF’ •[ƒy[ƒW‚ðì¬‚·‚é
PdfReader singlePDF = new PdfReader(templateName);
//zhangjun :
//PRAcroForm tmpForm = singlePDF.getAcroForm();
//here, the tmpForm is not null.

ByteArrayOutputStream pdfStream = new ByteArrayOutputStream();
PdfStamper stamp = new PdfStamper(singlePDF, pdfStream);


// PDF’ •[ƒy[ƒWã‚̃tƒB[ƒ‹ƒh‚ðŽæ“¾‚·‚é
AcroFields form = stamp.getAcroFields();
HashMap formFields = form.getFields();
Set formFieldNames = formFields.keySet();
Iterator nameIterator = formFieldNames.iterator();


// PDF’ •[ã‚̃tƒB[ƒ‹ƒh‚Ì’l‚ðÝ’è‚·‚é
while (nameIterator.hasNext()) {
// ƒtƒB[ƒ‹ƒh–¼‚ðŽæ“¾‚·‚é
String fieldName = (String) nameIterator.next();
// ƒtƒB[ƒ‹ƒh’l‚ðŽæ“¾‚·‚é
Object value = valueMap.get(fieldName);


// ƒtƒB[ƒ‹ƒh’l‚ðÝ’è‚·‚é
if (value != null) {
String fieldStringValue = (String) value;
form.setField(fieldName, fieldStringValue);
}
}


// PDF’ •[‚ðMerge‚·‚é
//zhangjun :
//PRAcroForm tmpForm = singlePDF.getAcroForm();
//here, the tmpForm is not null.


stamp.setFormFlattening(true);
stamp.close();
//zhangjun :
//PRAcroForm tmpForm = singlePDF.getAcroForm();
//here, the tmpForm is not null.


//zhangjun:
//here, pdfStream has get the content.
PdfReader completedPDF = new PdfReader(pdfStream.toByteArray());
int numPages = completedPDF.getNumberOfPages();


for (int i = 0; i < numPages; ++i) {
PdfImportedPage page = writer.getImportedPage(completedPDF, i + 1);
writer.addPage(page);
}

//zhangjun: here, the completedform is null. there is no form in the PRAcroForm.
PRAcroForm completedform = completedPDF.getAcroForm();
if (completedform != null) {
writer.copyAcroForm(completedPDF);
}
}


// PDF’ •[ì¬I—¹‚µ‚Ü‚·
mergedPDF.close();
} catch (Exception ex) {
log.error(ex.getMessage());
ex.printStackTrace();
}
}


/*public static void main(String[] args) {
try {
Vector v = new Vector();
v.add(TestPdf.getInstance(1));
v.add(TestPdf.getInstance(2));


OutputStream os = new FileOutputStream(""
+ System.currentTimeMillis() + ".pdf");
PdfProcessor process = new PdfProcessor(v, os);
os.flush();
os.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}*/


}

Full Threads Oldest First

Showing messages 1 through 1 of 1.

  • How to use iText template to fill data values on a Form?
    2006-10-12 16:58:57  dynamicpdf [View]

    Hi, Iam also facing same req.to fill a pdf using itext. This code lot of stuff and abstract. can you please elabore or give some comments to understand. Thanks in advance.