Hi All,
I'm brand new to the list and new to iText as well. I have been using it for about 1 week.
I am working on project that requires a complex html report layout that also need to provide output in multiple formats (pdf, rtf, xls, html (printable)). I am creating the reports in a servlet and setting the content type and creating the writer, such as:
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
HttpSession session = request.getSession();
Parameters parameters = (Parameters)session.getAttribute("parameters");
if (parameters==null) {
parameters = new Parameters();
}
parameters.parseParameters(request);
session.setAttribute("parameters", parameters);
if (parameters.getYearMonth()==null || parameters.getYearMonth().trim().equals("")) {
response.sendRedirect("index.jsp");
return;
}
//gather data and add to session
ArrayList tgmTopsheetRecords = new TopsheetDao().getTotalGrossMarginRecords(parameters);
session.setAttribute("tgmTopsheetRecords", tgmTopsheetRecords);
//determine output
String output = request.getParameter("output");
if (output!=null && !output.trim().equals("")) {
//Document document = new Document(PageSize.LETTER.rotate()); //ie landscape
Document document = new Document(PageSize.LETTER); //ie landscape
//Document document = new Document();
if (output.equals("print")) {
response.setContentType("text/html");
HtmlWriter.getInstance(document, response.getOutputStream());
} else if (output.equals("pdf")) {
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
} else if (output.equals("rtf")) {
response.setContentType("application/rtf");
RtfWriter.getInstance(document, response.getOutputStream());
} else if (output.equals("xls")) {
response.setContentType("application/vnd.ms-excel");
HtmlWriter.getInstance(document, response.getOutputStream());
}
document.open();
document.add(new Paragraph("Hello World"));
//total gross margin table
Font headerFont = new Font(Font.TIMES_ROMAN,8,Font.BOLD);
Font normalFont = new Font(Font.TIMES_ROMAN,8,Font.NORMAL);
Color bgColor = new Color(232,232,232);
Color white = Color.WHITE;
Color red = Color.PINK;
Color tmpColor = Color.WHITE;
beans.topsheet.Topsheet topsheet = null;
Table table = new Table(14);
float f = .062f;
float[] widths = {.24f,.01f,f,f,f,f,f,f,f,f,f,f,f,f};
table.setWidths(widths);
table.setWidth(98.0f);
table.setPadding(2);
table.setSpacing(0);
//table.setSpaceBetweenCells(0);
/* start header */
table.addCell(makeCell("Total Gross Margin", headerFont, bgColor, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM, -1, -1, true));
table.addCell(makeCell(" "));
table.addCell(makeCell("Month", headerFont, bgColor, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, -1, 2, false));
table.addCell(makeCell("in %", headerFont, bgColor, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, -1, 2, false));
table.addCell(makeCell("YTD", headerFont, bgColor, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, -1, 2, false));
table.addCell(makeCell("in %", headerFont, bgColor, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, -1, 2, false));
table.addCell(makeCell("Month", headerFont, bgColor, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 4, -1, false));
table.addCell(makeCell("YTD", headerFont, bgColor, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 4, -1, false));
table.addCell(makeCell("(in $000)"));
table.addCell(makeCell(" "));
table.addCell(makeCell("vs. plan", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("% Var", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("vs. PY", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("% Var", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("vs. Plan", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("% Var", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("vs. PY", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("% Var", headerFont, bgColor, Element.ALIGN_CENTER));
/* end header */
for (int r=0; r<tgmTopsheetRecords.size(); r++) {
topsheet = (beans.topsheet.Topsheet)tgmTopsheetRecords.get(r);
table.addCell(makeCell(topsheet.getFullGroup()));
table.addCell(makeCell(" "));
table.addCell(makeCell(Util.formatNumber(topsheet.getTransAmt()), null, null, Element.ALIGN_RIGHT));
table.addCell(makeCell(Util.formatPercent(topsheet.getMonthPercentOfSales(),1), null, null, Element.ALIGN_RIGHT));
table.addCell(makeCell(Util.formatNumber(topsheet.getYTDTransAmt()), null, null, Element.ALIGN_RIGHT));
table.addCell(makeCell(Util.formatPercent(topsheet.getYTDPercentOfSales(),1), null, null, Element.ALIGN_RIGHT));
tmpColor = topsheet.getMonthPlanVariance()<0 ? red : white;
table.addCell(makeCell(Util.formatNumber(topsheet.getMonthPlanVariance()), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getMonthVariancePercent()<0 ? red : white;
table.addCell(makeCell(Util.formatPercent(topsheet.getMonthVariancePercent(),1), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getPMthPlanVariance()<0 ? red : white;
table.addCell(makeCell(Util.formatNumber(topsheet.getPMthPlanVariance()), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getPMthVariancePercent()<0 ? red : white;
table.addCell(makeCell(Util.formatPercent(topsheet.getPMthVariancePercent(),1), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getYTDPlanVariance()<0 ? red : white;
table.addCell(makeCell(Util.formatNumber(topsheet.getYTDPlanVariance()), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getYTDVariancePercent()<0 ? red : white;
table.addCell(makeCell(Util.formatPercent(topsheet.getYTDVariancePercent(),1), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getPYTDPlanVariance()<0 ? red : white;
table.addCell(makeCell(Util.formatNumber(topsheet.getPYTDPlanVariance()), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getPYTDVariancePercent()<0 ? red : white;
table.addCell(makeCell(Util.formatPercent(topsheet.getPYTDVariancePercent(),1), null, tmpColor, Element.ALIGN_RIGHT));
}
document.add(table);
document.close();
} else {
request.getRequestDispatcher("topsheet.jsp").forward(request,response);
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
private Cell makeCell(String text) throws Exception {
return this.makeCell(text, null, null, -1, -1, -1, -1, false);
}
private Cell makeCell(String text, Font font) throws Exception {
return this.makeCell(text, font, null, -1, -1, -1, -1, false);
}
private Cell makeCell(String text, Font font, Color bgcolor) throws Exception {
return this.makeCell(text, font, bgcolor, -1, -1, -1, -1, false);
}
private Cell makeCell(String text, Font font, Color bgcolor, int halign) throws Exception {
return this.makeCell(text, font, bgcolor, halign, -1, -1, -1, false);
}
private Cell makeCell(String text, Font font, Color bgcolor, int halign, int valign) throws Exception {
return this.makeCell(text, font, bgcolor, halign, valign, -1, -1, false);
}
private Cell makeCell(String text, Font font, Color bgcolor, int halign, int valign, int colspan, int rowspan, boolean noWrap) throws Exception {
Chunk chunk = null;
Font defaultFont = new Font(Font.TIMES_ROMAN,8,Font.NORMAL);
if (font!=null) {
defaultFont = font;
}
chunk = new Chunk(text);
chunk.setFont(defaultFont);
Cell rv = new Cell(chunk);
if (bgcolor!=null) {
rv.setBackgroundColor(bgcolor);
}
if (halign!=-1) {
rv.setHorizontalAlignment(halign);
} else {
rv.setHorizontalAlignment(Element.ALIGN_LEFT);
}
if (valign!=-1) {
rv.setVerticalAlignment(valign);
} else {
rv.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (colspan!=-1) {
rv.setColspan(colspan);
}
if (rowspan!=-1) {
rv.setRowspan(rowspan);
}
//System.out.println("1: "+rv.cellWidth());
//rv.setWidth(String.valueOf(chunk.getWidthPoint()));
//System.out.println("2: "+rv.cellWidth());
//rv.setNoWrap(noWrap);
return rv;
}
The main part of the output is a table. I am just using the Table and Cell classes. which works great for all output options except for the PDF. The PDF cells are really tall or the row height is too high. This is made more obvious by the fact that I have to make the font really small since there are so many columns. After some experimentation and searching through the list archives, I realized that there is neither a function to set the rowheight nor set the line spacing. The text in Cell when written with a PdfWriter is doouble spacing the text. Someone wrote in last year with the same issue which unfortunately went unanswered.
I have also noticed that when issues come up related to the tables in a pdf, the user is encourged to use the PdfPTable and PdfPCell classes. Well, I'd rather not do that. I'd rather write one complex series of tables, that can be written out to the different output types insteaf of one set of code for each output type (that would be 4 different sets of code).
Is there anything I can do in the Table or Cell classes to fix the double spacing in the Cells? If that is not possible, maybe someone could offer me some advice on how I could organize this to avoid 4 sets of code if I must use a PdfPTable.
Also let me say that I am not knocking iText. iText is awesome! I haven't run across a single product that can output to so many different formats!
Thanks in advance,
Thomas
Thomas C. Moorer, Jr.
email: [EMAIL PROTECTED]
I'm brand new to the list and new to iText as well. I have been using it for about 1 week.
I am working on project that requires a complex html report layout that also need to provide output in multiple formats (pdf, rtf, xls, html (printable)). I am creating the reports in a servlet and setting the content type and creating the writer, such as:
/** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
HttpSession session = request.getSession();
Parameters parameters = (Parameters)session.getAttribute("parameters");
if (parameters==null) {
parameters = new Parameters();
}
parameters.parseParameters(request);
session.setAttribute("parameters", parameters);
if (parameters.getYearMonth()==null || parameters.getYearMonth().trim().equals("")) {
response.sendRedirect("index.jsp");
return;
}
//gather data and add to session
ArrayList tgmTopsheetRecords = new TopsheetDao().getTotalGrossMarginRecords(parameters);
session.setAttribute("tgmTopsheetRecords", tgmTopsheetRecords);
//determine output
String output = request.getParameter("output");
if (output!=null && !output.trim().equals("")) {
//Document document = new Document(PageSize.LETTER.rotate()); //ie landscape
Document document = new Document(PageSize.LETTER); //ie landscape
//Document document = new Document();
if (output.equals("print")) {
response.setContentType("text/html");
HtmlWriter.getInstance(document, response.getOutputStream());
} else if (output.equals("pdf")) {
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
} else if (output.equals("rtf")) {
response.setContentType("application/rtf");
RtfWriter.getInstance(document, response.getOutputStream());
} else if (output.equals("xls")) {
response.setContentType("application/vnd.ms-excel");
HtmlWriter.getInstance(document, response.getOutputStream());
}
document.open();
document.add(new Paragraph("Hello World"));
//total gross margin table
Font headerFont = new Font(Font.TIMES_ROMAN,8,Font.BOLD);
Font normalFont = new Font(Font.TIMES_ROMAN,8,Font.NORMAL);
Color bgColor = new Color(232,232,232);
Color white = Color.WHITE;
Color red = Color.PINK;
Color tmpColor = Color.WHITE;
beans.topsheet.Topsheet topsheet = null;
Table table = new Table(14);
float f = .062f;
float[] widths = {.24f,.01f,f,f,f,f,f,f,f,f,f,f,f,f};
table.setWidths(widths);
table.setWidth(98.0f);
table.setPadding(2);
table.setSpacing(0);
//table.setSpaceBetweenCells(0);
/* start header */
table.addCell(makeCell("Total Gross Margin", headerFont, bgColor, Element.ALIGN_LEFT, Element.ALIGN_BOTTOM, -1, -1, true));
table.addCell(makeCell(" "));
table.addCell(makeCell("Month", headerFont, bgColor, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, -1, 2, false));
table.addCell(makeCell("in %", headerFont, bgColor, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, -1, 2, false));
table.addCell(makeCell("YTD", headerFont, bgColor, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, -1, 2, false));
table.addCell(makeCell("in %", headerFont, bgColor, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, -1, 2, false));
table.addCell(makeCell("Month", headerFont, bgColor, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 4, -1, false));
table.addCell(makeCell("YTD", headerFont, bgColor, Element.ALIGN_CENTER, Element.ALIGN_MIDDLE, 4, -1, false));
table.addCell(makeCell("(in $000)"));
table.addCell(makeCell(" "));
table.addCell(makeCell("vs. plan", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("% Var", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("vs. PY", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("% Var", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("vs. Plan", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("% Var", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("vs. PY", headerFont, bgColor, Element.ALIGN_CENTER));
table.addCell(makeCell("% Var", headerFont, bgColor, Element.ALIGN_CENTER));
/* end header */
for (int r=0; r<tgmTopsheetRecords.size(); r++) {
topsheet = (beans.topsheet.Topsheet)tgmTopsheetRecords.get(r);
table.addCell(makeCell(topsheet.getFullGroup()));
table.addCell(makeCell(" "));
table.addCell(makeCell(Util.formatNumber(topsheet.getTransAmt()), null, null, Element.ALIGN_RIGHT));
table.addCell(makeCell(Util.formatPercent(topsheet.getMonthPercentOfSales(),1), null, null, Element.ALIGN_RIGHT));
table.addCell(makeCell(Util.formatNumber(topsheet.getYTDTransAmt()), null, null, Element.ALIGN_RIGHT));
table.addCell(makeCell(Util.formatPercent(topsheet.getYTDPercentOfSales(),1), null, null, Element.ALIGN_RIGHT));
tmpColor = topsheet.getMonthPlanVariance()<0 ? red : white;
table.addCell(makeCell(Util.formatNumber(topsheet.getMonthPlanVariance()), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getMonthVariancePercent()<0 ? red : white;
table.addCell(makeCell(Util.formatPercent(topsheet.getMonthVariancePercent(),1), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getPMthPlanVariance()<0 ? red : white;
table.addCell(makeCell(Util.formatNumber(topsheet.getPMthPlanVariance()), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getPMthVariancePercent()<0 ? red : white;
table.addCell(makeCell(Util.formatPercent(topsheet.getPMthVariancePercent(),1), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getYTDPlanVariance()<0 ? red : white;
table.addCell(makeCell(Util.formatNumber(topsheet.getYTDPlanVariance()), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getYTDVariancePercent()<0 ? red : white;
table.addCell(makeCell(Util.formatPercent(topsheet.getYTDVariancePercent(),1), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getPYTDPlanVariance()<0 ? red : white;
table.addCell(makeCell(Util.formatNumber(topsheet.getPYTDPlanVariance()), null, tmpColor, Element.ALIGN_RIGHT));
tmpColor = topsheet.getPYTDVariancePercent()<0 ? red : white;
table.addCell(makeCell(Util.formatPercent(topsheet.getPYTDVariancePercent(),1), null, tmpColor, Element.ALIGN_RIGHT));
}
document.add(table);
document.close();
} else {
request.getRequestDispatcher("topsheet.jsp").forward(request,response);
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
private Cell makeCell(String text) throws Exception {
return this.makeCell(text, null, null, -1, -1, -1, -1, false);
}
private Cell makeCell(String text, Font font) throws Exception {
return this.makeCell(text, font, null, -1, -1, -1, -1, false);
}
private Cell makeCell(String text, Font font, Color bgcolor) throws Exception {
return this.makeCell(text, font, bgcolor, -1, -1, -1, -1, false);
}
private Cell makeCell(String text, Font font, Color bgcolor, int halign) throws Exception {
return this.makeCell(text, font, bgcolor, halign, -1, -1, -1, false);
}
private Cell makeCell(String text, Font font, Color bgcolor, int halign, int valign) throws Exception {
return this.makeCell(text, font, bgcolor, halign, valign, -1, -1, false);
}
private Cell makeCell(String text, Font font, Color bgcolor, int halign, int valign, int colspan, int rowspan, boolean noWrap) throws Exception {
Chunk chunk = null;
Font defaultFont = new Font(Font.TIMES_ROMAN,8,Font.NORMAL);
if (font!=null) {
defaultFont = font;
}
chunk = new Chunk(text);
chunk.setFont(defaultFont);
Cell rv = new Cell(chunk);
if (bgcolor!=null) {
rv.setBackgroundColor(bgcolor);
}
if (halign!=-1) {
rv.setHorizontalAlignment(halign);
} else {
rv.setHorizontalAlignment(Element.ALIGN_LEFT);
}
if (valign!=-1) {
rv.setVerticalAlignment(valign);
} else {
rv.setVerticalAlignment(Element.ALIGN_MIDDLE);
}
if (colspan!=-1) {
rv.setColspan(colspan);
}
if (rowspan!=-1) {
rv.setRowspan(rowspan);
}
//System.out.println("1: "+rv.cellWidth());
//rv.setWidth(String.valueOf(chunk.getWidthPoint()));
//System.out.println("2: "+rv.cellWidth());
//rv.setNoWrap(noWrap);
return rv;
}
The main part of the output is a table. I am just using the Table and Cell classes. which works great for all output options except for the PDF. The PDF cells are really tall or the row height is too high. This is made more obvious by the fact that I have to make the font really small since there are so many columns. After some experimentation and searching through the list archives, I realized that there is neither a function to set the rowheight nor set the line spacing. The text in Cell when written with a PdfWriter is doouble spacing the text. Someone wrote in last year with the same issue which unfortunately went unanswered.
I have also noticed that when issues come up related to the tables in a pdf, the user is encourged to use the PdfPTable and PdfPCell classes. Well, I'd rather not do that. I'd rather write one complex series of tables, that can be written out to the different output types insteaf of one set of code for each output type (that would be 4 different sets of code).
Is there anything I can do in the Table or Cell classes to fix the double spacing in the Cells? If that is not possible, maybe someone could offer me some advice on how I could organize this to avoid 4 sets of code if I must use a PdfPTable.
Also let me say that I am not knocking iText. iText is awesome! I haven't run across a single product that can output to so many different formats!
Thanks in advance,
Thomas
email: [EMAIL PROTECTED]
------------------------------------------------------------------------- Using Tomcat but need to do more? Need to support web services, security? Get stuff done quickly with pre-integrated technology to make your job easier Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________ iText-questions mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/itext-questions
