Hi.....if you guys dont mind....here's the code I
wrote.

/*************************************************************************************
Author:                 Najaf A. Shah
Created:                Jan. 11, 2005
Last Revision:          Jan. 11, 2005
Purpose:                This program can generate a flattened PDF
(outPDF) from a
                        template (templatePDF) and CustomerStmt object
**************************************************************************************/


import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PRAcroForm;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.AcroFields;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.FdfReader;
import com.lowagie.text.pdf.*;
import java.util.*;
import java.lang.Math;
import com.lowagie.text.Document;

import java.util.Iterator;
import java.io.IOException;
import java.io.FileOutputStream;

public class generatePDF
{

        public final static int LINES_PER_PAGE = 40;
        public final static String TEMP_DIR = "/tmp/";
        public static PdfReader originalReader;
        public static HashMap cache;

        generatePDF(String templateFName, CustomerStmt cust)
throws IOException, DocumentException
        {
                String outFName = "";

                originalReader = new PdfReader(templateFName);
                cache = new HashMap();

                Date now;
                CustomerStmt temp = cust;                       //make a copy 
of the
variable you will be sending to generatePage
                String[] stmt_lines =
cust.Statement_Data.split("\n");        //split the statement
data into lines

                double num_pages = stmt_lines.length;   num_pages =
Math.ceil(num_pages/LINES_PER_PAGE);
                String page_names[] = new String[(int)num_pages];

                for(int i=0; i < num_pages; i++)
                {
                        temp.Statement_Data = "\n";

                        for(int j=0; j < LINES_PER_PAGE &&
(LINES_PER_PAGE*i+j) < stmt_lines.length ; j++)
                                temp.Statement_Data +=
stmt_lines[LINES_PER_PAGE*i+j] + "\n";

                        now = new Date();
                        page_names[i] = cust.AccountNum + "_" +
now.getTime() + "_pg" + i + ".pdf";
                        generatePage(templateFName, TEMP_DIR +
page_names[i] , temp);
                }

                now = new Date();
                outFName = TEMP_DIR + "/" + cust.AccountNum + "_" +
now.getTime() + "_full.pdf";
                mergePages(page_names,outFName);
                System.out.println(outFName + " " + (int)num_pages);

        }

        private void mergePages(String[] page_names, String
outFName)
        {
                try
                {
                        Document document = null;
                        PdfCopy  writer = null;

                        for(int i=0; i<page_names.length; i++)  //for every
page
                        {
                                PdfReader reader = new PdfReader(TEMP_DIR + "/" 
+
page_names[i]);

                                if(i == 0)      //first time around
                                {
                                        document = new
Document(reader.getPageSizeWithRotation(1));
                                        writer = new PdfCopy(document, new
FileOutputStream(outFName));
                                        document.open();
                                }

                                PdfImportedPage page;
                                page = writer.getImportedPage(reader, 1);       
//add
the page (only 1 page per pdf)
                                writer.addPage(page);

                                PRAcroForm form = reader.getAcroForm();
                                if (form != null)
                                        writer.copyAcroForm(reader);
                        }

                        document.close();
                }

                catch (Exception e) { e.printStackTrace();}
        }

        private void generatePage(String templateFName,
String outFName, CustomerStmt cust) throws
IOException, DocumentException
        {
                PdfReader reader = new PdfReader(originalReader);
                PdfStamper stamper = new PdfStamper(reader,new
FileOutputStream(outFName));
                AcroFields form = stamper.getAcroFields();

                //form.setFields(new FdfReader(dataFName));
                form.setFieldCache(cache);
                form.setField("address","\n" + cust.ADD1 + "\n" +
cust.ADD2 + "\n" + cust.ADD3 + "\n" + cust.ADD4);
                form.setField("account",cust.AccountNum);
                form.setField("ssn",cust.SSN);
                form.setField("page","11");
                form.setField("branch",cust.Branch);
                form.setField("statement",cust.Statement_Data);

                stamper.setFormFlattening(true);
                stamper.close();


        }

}




                
__________________________________ 
Do you Yahoo!? 
Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250


-------------------------------------------------------
This SF.Net email is sponsored by: IntelliVIEW -- Interactive Reporting
Tool for open source databases. Create drag-&-drop reports. Save time
by over 75%! Publish reports on the web. Export to DOC, XLS, RTF, etc.
Download a FREE copy at http://www.intelliview.com/go/osdn_nl
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to