mrpresto - 2010-01-29

Writing a test footer with "onEndPage", it works just fine in the test driver I built but doesn't write anything in this code.  I can see it hit the EndOfPage event just fine, it just won't write to the file.  There must be some subtle thing I'm missing but I'll be darned if I know what it is.  Can anybody spot my error?

thanks in advance,
Mike
code to follow

package abc.vantageplus.reportbuilder;
import java.io.FileOutputStream;
import java.sql.Connection;
import org.apache.log4j.Logger;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
public abstract class ReportBuilderPdfBaseClass implements ReportBuilderInterface {
    ReportRequest myReportRequest;
    ReportProfile myReportProfile;
    Font myDataFont = new Font(Font.COURIER, 8);
    Font smallerDataFont = new Font(Font.COURIER, 6);
    Image img;
    Integer pageNum = 0;
    public ReportBuilderPdfBaseClass(ReportRequest reportRequest, ReportProfile reportProfile){
        this.myReportRequest = reportRequest;
        this.myReportProfile = reportProfile;
    }
    
    public void createReport(Connection conn) throws Exception {
        try {
            Document myDocument = getSetupDocument();
            PdfWriter thisPDF = PdfWriter.getInstance(myDocument, new FileOutputStream(myReportRequest.getFilename()));
            
            EndPageEvent eEvent = new EndPageEvent();
            thisPDF.setPageEvent(eEvent);
            myDocument.open();
            
            addHeaderPageToDocument(myDocument);
            myDocument.newPage();
            myReportRequest.incrementDataUnits();
            
            PdfContentByte canvas = thisPDF.getDirectContentUnder();
            canvas.saveState();
            img = Image.getInstance("images/Tanbar3.JPG");
            img.setAlignment(Image.UNDERLYING);
            img.setAbsolutePosition(80, 62);
            img.scaleAbsolute(640, 504);
            canvas.addImage(img);
            canvas.restoreState();
            addDataToDocument(conn, myDocument);
            myDocument.close();
        } catch (Exception ex) {
            System.err.println("createReport Exception: " + ex.getMessage());
        }
    }
    abstract void addDataToDocument(Connection conn, Document myDocument) throws Exception;
     private void addHeaderPageToDocument(Document myDocument) throws DocumentException {
        Paragraph myParagraph = new Paragraph();
        Chunk myChunk = new Chunk(REPORT_HEADER);
        myChunk.setUnderline(1.2f, -4f);
        myParagraph.add(myChunk);
        myParagraph.add(Chunk.NEWLINE);
        myParagraph.add(new Chunk(String.format("%s (%s)", myReportProfile.getReportTitle(), myReportProfile.getReportNumber())));
        myParagraph.add(Chunk.NEWLINE);myParagraph.add(Chunk.NEWLINE);
        myDocument.add(myParagraph);
        pageNum++;
    }
     
    private Document getSetupDocument() throws Exception {
        Document document;
        if (myReportProfile.isCustomReport()) {
            document = new Document(new Rectangle(1008, 612)); //Rotated legal size
        }
        else {
            document = new Document(new Rectangle(792, 622), 60,60,60,75); //Rotated letter size w/ margins L,R,T,B
        }
        PdfWriter.getInstance(document, new FileOutputStream(myReportRequest.getTempPathFileName()));
        document.addTitle(String.format("%s (%s)", myReportProfile.getReportTitle(), myReportProfile.getReportNumber()));
        document.addAuthor(REPORT_AUTHOR);
        document.addSubject(String.format("%s (%s)", myReportProfile.getReportTitle(), myReportProfile.getReportNumber()));
        return document;
    }
    void writeReportData(Document myDocument, String[] dataArray) throws DocumentException {
        Phrase myPhrase = new Phrase();
        Paragraph myParagraph = new Paragraph();
        myParagraph.setFont(myDataFont);
        myParagraph.setLeading(8);
        myParagraph.setIndentationLeft(20);
        img.setAlignment(Image.UNDERLYING);
        img.setAbsolutePosition(80, 62);
        img.scaleAbsolute(640, 504);
        myDocument.add(img);
        
        for (int idx = 0; idx < dataArray.length; idx++){
            Chunk myChunk = new Chunk(dataArray[idx]);
            myPhrase.add(myChunk);
            if (idx < dataArray.length - 1) {
                myPhrase.add(Chunk.NEWLINE);
            }   
        }
        myParagraph.add(myPhrase);
        myDocument.add(myParagraph);
        myReportRequest.incrementDataUnits();
        
        myDocument.newPage();
        pageNum++;
    }

    class EndPageEvent extends PdfPageEventHelper {

        public void onEndPage(PdfWriter writer, Document document) {
            System.out.println ("onEndPage " + pageNum);
            PdfContentByte canvas = writer.getDirectContent();
            canvas.saveState();
            canvas.setLineWidth(1f); 
            canvas.moveTo(90, 5);
            canvas.lineTo(195, 355);
            canvas.stroke();
            canvas.restoreState();
        }
    }
}