Bruno,
It is somewhat difficult to separate the code from the application. Most of
the content is generated dynamically from database queries and lots of
supporting objects. I have included the code (sorry it isn't exactly short)
that deals directly with iText so you can see the sequence in which calls
are made. At the moment I have commented out the newPage() calls and
inserted chapters instead. The result is the same.
You mentioned that newPage() is ignored if it is called after a blank page.
Is there a simple way to force the current page to be written and closed
before calling newPage()?
Thanks,
Kim
package redflag.util;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.awt.Color;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.*;
import redflag.*;
import redflag.bean.*;
import redflag.chart.*;
import redflag.data.*;
import java.sql.*;
import com.klg.jclass.schart.*;
public class TrendsSummaryReport_iText {
private final ResourceBundle msgs =
ResourceBundle.getBundle("resources.application");
private final Font baseFont = new Font(Font.HELVETICA,12,Font.NORMAL);
private final Font titleFont = new Font(Font.HELVETICA,24,Font.BOLD);
private final Font boldFont = new Font(Font.HELVETICA,12,Font.BOLD);
private final Font headingFont = new Font(Font.HELVETICA,20,Font.BOLD);
private final Font sectionFont = new Font(Font.HELVETICA,18,Font.BOLD);
private final Font footerFont = new Font(Font.HELVETICA,8,Font.ITALIC);
private final Font headerFont = new Font(Font.HELVETICA,8,Font.NORMAL);
private final Font dataFont = new Font(Font.HELVETICA,10,Font.NORMAL);
public ByteArrayOutputStream generatePDFDocumentBytes(Connection conn,
Company company, ArrayList list) throws
DocumentException {
Document doc = new Document();
ByteArrayOutputStream baosPDF = new ByteArrayOutputStream();
PdfWriter docWriter = null;
Paragraph p = null;
Phrase phrase = null;
Chapter chapter = null;
Graphic g = null;
int num = 1;
try {
docWriter = PdfWriter.getInstance(doc, baosPDF);
doc.addAuthor("redFLAG Analytics, LLC");
doc.addCreationDate();
doc.addProducer();
doc.addCreator("redFLAG Analytics, LLC");
doc.addTitle("redFLAG Report for " + company.getName());
doc.addKeywords("pdf, redFLAG, " + company.getName());
doc.setPageSize(PageSize.LETTER);
HeaderFooter header = new HeaderFooter(new Phrase(""),false);
doc.setHeader(header);
header.setBorder(HeaderFooter.NO_BORDER);
HeaderFooter footer = new HeaderFooter(new Phrase("",footerFont),
true);
footer.setAlignment(HeaderFooter.ALIGN_CENTER);
footer.setBorder(Rectangle.NO_BORDER);
doc.setFooter(footer);
doc.open();
p = new Paragraph("redFLAG Report for " +
company.getName(),titleFont);
p.setAlignment(Paragraph.ALIGN_CENTER);
doc.add(p);
header = new HeaderFooter(new Phrase("redFLAG Report for " +
company.getName(),headerFont),false);
header.setAlignment(HeaderFooter.ALIGN_LEFT);
header.setBorder(Rectangle.NO_BORDER);
doc.setHeader(header);
p = new Paragraph("redFLAG Summary Table",headingFont);
chapter = new Chapter(p,num);
chapter.setNumberDepth(0);
chapter.add(new Paragraph("The company's financial data for the last 5
or " +
"fewer valid reporting periods, has shown evidence of potential " +
"problems in the following areas ordered from most to least number
of " +
"potential problems:",baseFont));
chapter.add(createRedflagSummaryTable(list));
doc.add(chapter);
Iterator i = list.iterator();
Redflags redflags = null;
ArrayList annlCharts = null;
ArrayList qtrlyCharts = null;
ArrayList annlMsgs = null;
ArrayList qtrlyMsgs = null;
num++;
p = new Paragraph("redFLAGs by Category",headingFont);
chapter = new Chapter(p,num);
chapter.setNumberDepth(0);
int secNum = 0;
Section sect = null;
while (i.hasNext()) {
secNum++;
redflags = (Redflags)(i.next());
p = new
Paragraph(msgs.getString(redflags.getTitleKey()),sectionFont);
sect = chapter.addSection(p,secNum);
sect.setNumberDepth(0);
sect.add(createRedflagMessagesTable(redflags));
}
doc.add(chapter);
Iterator j = list.iterator();
p = new Paragraph("redFLAG Graphs",headingFont);
num++;
chapter = new Chapter(p,num);
chapter.setNumberDepth(0);
secNum = 0;
while (j.hasNext()) {
//newpage = doc.newPage();
secNum++;
redflags = (Redflags)(j.next());
//phrase = new Phrase(10,msgs.getString(redflags.getTitleKey()) +
"\n\n",headingFont);
p = new
Paragraph(msgs.getString(redflags.getTitleKey()),sectionFont);
//p.add(phrase);
p.setAlignment(Paragraph.ALIGN_LEFT);
sect = chapter.addSection(p,secNum);
sect.setNumberDepth(0);
//g = new Graphic();
//g.setHorizontalLine(3,100);
//doc.add(g);
p = new Paragraph("\n");
sect.add(p);
annlMsgs = redflags.getAnnlMsgKeys();
qtrlyMsgs = redflags.getQtrlyMsgKeys();
annlCharts = redflags.getAnnlCharts();
qtrlyCharts = redflags.getQtrlyCharts();
String chartName = null;
String chartClass = null;
for (int k = 0; k < annlMsgs.size(); k++) {
chartClass = (String)(annlCharts.get(k));
chartName = msgs.getString("title.annual." +
chartClass.substring(4,chartClass.indexOf("Chart")));
sect.add(createChart(conn, company, (String)(annlMsgs.get(k)),
chartClass, chartName));
doc.newPage();
}
for (int l = 0; l < qtrlyMsgs.size(); l++) {
chartClass = (String)(qtrlyCharts.get(l));
chartName = msgs.getString("title.qtrly." +
chartClass.substring(5,chartClass.indexOf("Chart")));
sect.add(createChart(conn, company, (String)(qtrlyMsgs.get(l)),
chartClass, chartName));
doc.newPage();
}
}
doc.add(chapter);
}
catch (DocumentException dex){
baosPDF.reset();
throw dex;
}
finally {
if (doc != null) {
doc.close();
}
if (docWriter != null) {
docWriter.close();
}
return baosPDF;
}
}
private Table createRedflagMessagesTable(Redflags redflags) throws
DocumentException {
Table tab = null;
try {
tab = new Table(1);
tab.setCellsFitPage(true);
tab.setTableFitsPage(true);
tab.setBorderWidth(1.0f);
tab.setPadding(2);
tab.setSpacing(2);
Cell cell = null;
cell = new Cell(new
Phrase(msgs.getString(redflags.getTitleKey()),boldFont));
cell.setHeader(true);
cell.setBackgroundColor(Color.GRAY);
tab.addCell(cell);
ArrayList annlRedflags = redflags.getAnnlMsgKeys();
Iterator i = annlRedflags.iterator();
while (i.hasNext()) {
cell = new Cell(new Phrase("Annual: " +
msgs.getString((String)i.next()),dataFont));
tab.addCell(cell);
}
ArrayList qtrlyRedflags = redflags.getQtrlyMsgKeys();
Iterator j = qtrlyRedflags.iterator();
while (j.hasNext()) {
cell = new Cell(new Phrase("Quarterly: " +
msgs.getString((String)j.next()),dataFont));
tab.addCell(cell);
}
}
catch (BadElementException e) {
throw e;
}
return tab;
}
private Table createRedflagSummaryTable(ArrayList list) throws
DocumentException {
Table tab = null;
try {
tab = new Table(3);
tab.setBorderWidth(1.0f);
tab.setPadding(5);
tab.setSpacing(5);
Cell cell = new Cell(new
Phrase(msgs.getString("title.redflagStats"),boldFont));
cell.setHeader(true);
cell.setColspan(3);
cell.setBackgroundColor(Color.GRAY);
tab.addCell(cell);
cell = new Cell(new Phrase("Redflag Category",boldFont));
cell.setBackgroundColor(Color.GRAY);
tab.addCell(cell);
cell = new Cell(new Phrase("Quarterly Redflags", boldFont));
cell.setBackgroundColor(Color.GRAY);
tab.addCell(cell);
cell = new Cell(new Phrase("Annual Redflags",boldFont));
cell.setBackgroundColor(Color.GRAY);
tab.addCell(cell);
tab.endHeaders();
Redflags redflags = null;
for (int i = 0; i < list.size(); i++) {
redflags = (Redflags)(list.get(i));
cell = new Cell(new
Phrase(msgs.getString(redflags.getTitleKey()),dataFont));
tab.addCell(cell);
cell = new Cell(new
Phrase(String.valueOf(redflags.getQtrlyCount()),dataFont));
tab.addCell(cell);
cell = new Cell(new
Phrase(String.valueOf(redflags.getAnnlCount()),dataFont));
tab.addCell(cell);
}
}
catch (BadElementException e) {
throw e;
}
return tab;
}
private com.lowagie.text.Image createChart(Connection conn, Company
company, String msg,
String chartClass, String chartName) throws
DocumentException {
com.lowagie.text.Image img = null;
try {
Chart c = (Chart)(Class.forName("redflag.chart." +
chartClass).newInstance());
RedflagChart rchart = c.execute(conn,
company,chartName,msgs.getString(msg),1100,600,20,18);
JCServerChart chart = rchart.getChart();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
java.awt.Image chartImage =
chart.snapshot(BufferedImage.TYPE_3BYTE_BGR);
img = com.lowagie.text.Image.getInstance(chartImage,null);
img.setAlignment(com.lowagie.text.Image.ALIGN_CENTER);
img.setBorderWidth(2);
img.setBorderColor(Color.BLACK);
img.scaleAbsolute(550,300);
}
catch (Exception e) {
throw new DocumentException(e.getMessage());
}
finally {
return img;
}
}
}
-----Original Message-----
From: Bruno [mailto:[EMAIL PROTECTED]
Sent: Tuesday, July 08, 2003 2:23 AM
To: Kim
Cc: [EMAIL PROTECTED]
Subject: Re: [iText-questions] newPage() and images
Quoting Kim <[EMAIL PROTECTED]>:
> I have tried to use newPage() to cause a new page to be generated
> prior to inserting a heading and related charts, but the method seems to
be
> ignored and the new heading will end up on the bottom or middle of a page.
I know newPage() is ignored if you call it after a 'blank' page.
But if you have added something (anything!) to the page newPage()
should not be ignored. Can you post a small (standalone) example
that reproduces the problem?
br,
Bruno
-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
http://aspnet.click-url.com/go/psa00100006ave/direct;at.asp_061203_01/01
_______________________________________________
iText-questions mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/itext-questions