我尝试将空格(" ")
的空白单元格添加到我的pdf文件中。即
PdfPCell blankCell = ContentHandler.getNormalCell(" ", language,fontsize);
它会抛出此错误
Message: java.lang.NullPointerException
com.itextpdf.text.DocumentException: java.lang.NullPointerException
at com.itextpdf.text.pdf.PdfDocument.add(PdfDocument.java:727)
at com.itextpdf.text.Document.add(Document.java:282)
然后我从细胞中移除了空间..即
PdfPCell blankCell = ContentHandler.getNormalCell("", language,fontsize);
我以这种方式解决了我的问题,但是pdf很糟糕。
有人可以帮助我吗,除了我正在做的事情之外,有没有可用的解决方案
编辑:
public static PdfPCell getNormalCell(String string, String language, float size) throws DocumentException, IOException {
// TODO Auto-generated method stub
Font f = new Font();
if(string!=null && !"".equals(string)){
f = getFontForThisLanguage(language);
}
if(size==-1) //Using a condition to make color RED as per need in view report
{
f.setColor(BaseColor.RED);
}
f.setSize(size);
Chunk chunk = new Chunk(new String(string.getBytes(), "UTF-8"),f);
PdfPCell pdfCell1 = new PdfPCell(new Phrase(string, f));
pdfCell1.setHorizontalAlignment(Element.ALIGN_LEFT);
return pdfCell1;
}
答案 0 :(得分:0)
首先回答您的问题:我创建了一个名为CellMethod的示例,我可以完美地将" "
添加到PdfPCell
而不会导致NullPointerException
。这是我的代码:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(2);
table.addCell("Winansi");
table.addCell(getNormalCell("Test", null, 12));
table.addCell("Winansi");
table.addCell(getNormalCell("Test", null, -12));
table.addCell("Greek");
table.addCell(getNormalCell("\u039d\u03cd\u03c6\u03b5\u03c2", "greek", 12));
table.addCell("Czech");
table.addCell(getNormalCell("\u010c,\u0106,\u0160,\u017d,\u0110", "czech", 12));
table.addCell("Test");
table.addCell(getNormalCell(" ", null, 12));
table.addCell("Test");
table.addCell(getNormalCell(" ", "greek", 12));
table.addCell("Test");
table.addCell(getNormalCell(" ", "czech", 12));
document.add(table);
document.close();
}
public static PdfPCell getNormalCell(String string, String language, float size)
throws DocumentException, IOException {
if(string != null && "".equals(string)){
return new PdfPCell();
}
Font f = getFontForThisLanguage(language);
if(size < 0) {
f.setColor(BaseColor.RED);
size = -size;
}
f.setSize(size);
PdfPCell cell = new PdfPCell(new Phrase(string, f));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
return cell;
}
public static Font getFontForThisLanguage(String language) {
if ("czech".equals(language)) {
return FontFactory.getFont(FONT, "Cp1250", true);
}
if ("greek".equals(language)) {
return FontFactory.getFont(FONT, "Cp1253", true);
}
return FontFactory.getFont(FONT, null, true);
}
可在此处找到生成的PDF:cell_method.pdf
现在关于评论。就好像你不想接受你的代码写得不好。相反,您声称iText存在问题。这是一个坏工匠,责备他的工具。
您尝试改进方法,但让我们看看您的方法并添加一些注释:
public static PdfPCell getNormalCell(String string, String language, float size)
throws DocumentException, IOException {
// The next line will create an instance of Helvetica, 12pt.
Font f = new Font();
// I don't understand this check. Why is it important to check string here?
if(string!=null && !"".equals(string)){
// are you sure you didn't want to check if language is not null?
f = getFontForThisLanguage(language);
// we have no idea what getFontForThisLanguage is doing
// what if language is null, does it throw an exception?
}
// This is ridiculous. See the next comment to find out why
if(size==-1) //Using a condition to make color RED as per need in view report
{
f.setColor(BaseColor.RED);
}
// In some situations, you are setting the font size to -1, that doesn't make sense
f.setSize(size);
// This line doesn't make sense either.
// (1.) string should already be in unicode.
// why are you getting the bytes and creating a UTF-8 string?
// (2.) why are you creating this chunk object? you never use it
Chunk chunk = new Chunk(new String(string.getBytes(), "UTF-8"),f);
// See: you're creating a Phrase with string, you don't use chunk!
PdfPCell pdfCell1 = new PdfPCell(new Phrase(string, f));
pdfCell1.setHorizontalAlignment(Element.ALIGN_LEFT);
return pdfCell1;
}
请与我的方法版本进行比较。