Hi all,
I am using iText.jar to generate PDFs and facing a strnge issue. If the data
has two or more than two riws then PDF gets generated correctly ,but if it
has single row then it gives error saying "File does not start with '%PDF-'
".
I am using following code :-
Here nlCols and nlRows are columns and rows for the table. _caption is the
caption of the PDFTable
*
if*(nlCols != *null* && nlCols.getLength() > 0) {
PdfPTable table = *new* PdfPTable(nlCols.getLength());
table.setSpacingBefore(_spacingbefore);
table.setSpacingAfter(_spacingafter);
table.setWidthPercentage((*float*)widthPercentage);
Color borderColor = LHUtilFunctions.colorFromString(_bordercolor);
table.getDefaultCell().setBackgroundColor(LHUtilFunctions.colorFromString(_backgroundcolor));
table.getDefaultCell().setBorderWidth(_borderwidth);
table.getDefaultCell().setBorderColor(borderColor);
table.getDefaultCell().setBorder(0);
table.getDefaultCell().setPadding(_cellpadding);
com.lowagie.text.Font headerfont = PDFFontUtilities.GetFont(_headerfontname,
BaseFont.IDENTITY_H, *true*, _headerfontsize, _headerfontstyle,
_headertextcolor);
Color headercolor = LHUtilFunctions.colorFromString(_headerbackgroundcolor);
*if* (!_caption.equals(LHConstants.EMPTY_STRING)) {
Paragraph p = *new* Paragraph(_caption, headerfont);
PdfPCell titlecell = *new* PdfPCell(p);
titlecell.setHorizontalAlignment(com.lowagie.text.Element.ALIGN_CENTER);
titlecell.setColspan(nlCols.getLength());
titlecell.setBackgroundColor(headercolor);
titlecell.setBorderWidth(_borderwidth);
titlecell.setBorderColor(borderColor);
titlecell.setPadding(_cellpadding);
table.setHeaderRows(1);
table.addCell(titlecell);
}
ArrayList alColumnTypes = *new* ArrayList();
*for* (*int* i = 0, nCols = nlCols.getLength(); i < nCols; i++) {
org.w3c.dom.Element elemCol = (org.w3c.dom.Element) nlCols.item(i);
Paragraph p =
*new*Paragraph(elemCol.getAttribute(LHXmlConstants.COLUMN_NAME_ATTRIBUTE),
headerfont);
alColumnTypes.add(*new*Integer(DataTypes.getDataTypeIndex(DataTypes.convertSQLDataType(DataTypes.parseInteger(elemCol.getAttribute(LHXmlConstants.COLUMN_SQLDATATYPE_ATTRIBUTE)).intValue()))));
PdfPCell headercell = *new* PdfPCell(p);
headercell.setBackgroundColor(headercolor);
headercell.setBorderWidth(_borderwidth);
headercell.setBorderColor(borderColor);
headercell.setHorizontalAlignment(_alignment);
headercell.setPadding(_cellpadding);
table.setHeaderRows(2);
table.addCell(headercell);
}
com.lowagie.text.Font font = PDFFontUtilities.GetFont(_textfontname,
BaseFont.IDENTITY_H, *true*, _textfontsize, _textfontstyle, _textcolor);
*int* nRow = 0;
Color color1 = LHUtilFunctions.colorFromString(_backgroundcolor);
Color color2 = LHUtilFunctions.colorFromString(_alternatebackgroundcolor);
*boolean* hasFormat = *false*;
*if* (numberFormat != *null* && numberFormat.length() > 0) {
hasFormat = *true*;
}
*for* (*int* i = 0, nRows = nlRows.getLength(); i < nRows; i++) {
org.w3c.dom.Node elemRow = nlRows.item(i);
++nRow;
*int* nCol = 0;
org.w3c.dom.Node elemCol = elemRow.getFirstChild();
*while* (elemCol != *null*) {
String sValue = XMLHandler.getNodeValue(elemCol);
String sOutput = LHConstants.EMPTY_STRING;
*switch* (((Integer) alColumnTypes.get(nCol)).intValue()) {
*case* DataTypes.DATA_TYPE_INDEX_INTEGER:
sOutput = DataTypes.objectToString(DataTypes.parseInteger(sValue));
*break*;
*case* DataTypes.DATA_TYPE_INDEX_BOOLEAN:
sOutput = DataTypes.objectToString(DataTypes.parseBoolean(sValue));
*break*;
*case* DataTypes.DATA_TYPE_INDEX_DOUBLE: {
*double* val = DataTypes.parseRawDouble(sValue);
*if* (hasFormat) {
*if* (Double.isNaN(val)) {
sOutput = LHConstants.INVALID_VALUE_STRING;
} *else* {
sOutput = format.format(val);
}
} *else* {
sOutput = DataTypes.objectToString(*new* Double(val));
}
*break*;
}
*case* DataTypes.DATA_TYPE_INDEX_DATETIME:
sOutput =
TimeUtil.calendarToStringFormatted(DataTypes.parseDateTime(sValue),
("MM-dd-yyyy
HH:mm:ss"));
*break*;
*default*:
sOutput = sValue;
*break*;
}
Paragraph p = *new* Paragraph(sOutput, font);
PdfPCell tablecell = *new* PdfPCell(p);
tablecell.setBorderWidth(_borderwidth);
tablecell.setBorderColor(borderColor);
tablecell.setPadding(_cellpadding);
++nCol;
*if* ((nRow % 2) == 0) {
tablecell.setBackgroundColor(color1);
} *else* {
tablecell.setBackgroundColor(color2);
}
table.addCell(tablecell);
elemCol = elemCol.getNextSibling();
}
Also , I have noticed that issue comes only if there is no caption. If I
pass the caption PDF gets generated even if there is only one row data.
Can anyone help me here.
Thanks and Regards,
Reena
------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://www.itextpdf.com/book/
Check the site with examples before you ask questions:
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/