| Article: |
Dynamically Creating PDFs in a Web Application | |
| Subject: | How to use iText template to fill data values on a Form? | |
| Date: | 2004-08-05 15:38:33 | |
| From: | sunni | |
|
I am new to iText. I want to do the similar task as mentioned in the article "....We converted the Word doc to PDF. Then using iText template capability, we loaded the PDF file into our application, .... to fill data values on the form and output the final PDF document.
|
||
Showing messages 1 through 3 of 3.
-
How to use iText template to fill data values on a Form?
2005-05-05 06:20:54 ShubhadaAnand [View]
-
How to use iText template to fill data values on a Form?
2005-03-18 01:11:43 baby2046 [View]
/**
* $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 {
/**
* OCX^X
*/
private static Log log = TRLogFactory.getLog(PdfProcessor.class);
/**
*[¼@Ì] OCX^XÌæ¾
*[@@\] OCX^Xðæ¾·é
*
* @return Log OCX^X
*/
public Log getLog() {
return log;
}
/**
*[¼@Ì] PDF [ì¬
*[@@\] PDF [ì¬Æ·é
*
* @param pdfForms PDF [f[^Æev[g
* @param outStream PDF [t@CÌAEg
*/
public PdfProcessor(List pdfForms, OutputStream outStream) {
// PDF [ð쬷é
try {
// PDF [hL g
Document mergedPDF = new Document();
// PDFhL gðì¬
PdfCopy writer = new PdfCopy(mergedPDF, outStream);
// PDFhL gðJ¯é
mergedPDF.open();
// PDF [y[Wð쬷é
Iterator iterator = pdfForms.iterator();
while (iterator.hasNext()) {
Object object = iterator.next();
// PDF [y[WÌf[^Æev[gt@Cp[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ãÌtB[hðæ¾·é
AcroFields form = stamp.getAcroFields();
HashMap formFields = form.getFields();
Set formFieldNames = formFields.keySet();
Iterator nameIterator = formFieldNames.iterator();
// PDF [ãÌtB[hÌlðÝè·é
while (nameIterator.hasNext()) {
// tB[h¼ðæ¾·é
String fieldName = (String) nameIterator.next();
// tB[hlðæ¾·é
Object value = valueMap.get(fieldName);
// tB[hlðÝè·é
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();
}
}*/
}
-
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.



I am newbie to iText.Can you please guide me to convert DOC to PDF?
Thanks a lot,
Shubhada