我正在使用您的EasyTable库,它很棒,因为它比itext7更好。我只有一个问题。我正在尝试在单元格中插入多行段落,但这给我一个错误。这是我的课。
错误是:
线程“主”中的异常java.lang.NullPointerException
在org.vandeseer.easytable.drawing.cell.ParagraphCellDrawer.drawContent(ParagraphCellDrawer.java:52)处
在org.vandeseer.easytable.TableDrawer.lambda $ new $ 0(TableDrawer.java:53)
在org.vandeseer.easytable.TableDrawer.drawRow(TableDrawer.java:161)
在org.vandeseer.easytable.TableDrawer.drawWithFunction(TableDrawer.java:145)
在org.vandeseer.easytable.TableDrawer.lambda $ drawPage $ 1(TableDrawer.java:75)
在java.lang.Iterable.forEach(Iterable.java:75)
在org.vandeseer.easytable.TableDrawer.drawPage(TableDrawer.java:74)
在org.vandeseer.easytable.TableDrawer.draw(TableDrawer.java:70)
在it.baz.ProvaPDF.crea(ProvaPDF.java:66)
在it.baz.ProvaPDF.main(ProvaPDF.java:21)package it.baz;import org.apache.pdfbox.pdmodel.PDDocument;import org.apache.pdfbox.pdmodel.PDPage;import org.apache.pdfbox.pdmodel.PDPageContentStream;import org.apache.pdfbox.pdmodel.common.PDRectangle;import org.vandeseer.easytable.TableDrawer;import org.vandeseer.easytable.settings.HorizontalAlignment;import org.vandeseer.easytable.structure.Row;import org.vandeseer.easytable.structure.Table;import org.vandeseer.easytable.structure.cell.TextCell;import org.vandeseer.easytable.structure.cell.paragraph.*;import java.awt.*;import java.io.IOException;public class ProvaPDF { public static void main(String[] args) { ProvaPDF pdf = new ProvaPDF(); pdf.crea(); } private void crea() { try { PDDocument document = new PDDocument(); final PDPage page = new PDPage(PDRectangle.A4); document.addPage(page); PDPageContentStream contentStream = new PDPageContentStream(document, page); Table myTable = Table.builder() .addColumnsOfWidth(100, 100) .addRow(Row.builder() .add(TextCell.builder().text("One One").borderWidth(1).backgroundColor(Color.GRAY).build()) .add(TextCell.builder().text("One Two").borderWidth(1).backgroundColor(Color.LIGHT_GRAY).build()) .build()) .addRow(Row.builder() .add(TextCell.builder().text("Two One").borderWidth(1).textColor(Color.RED).build()) .add(TextCell.builder().text("Two Two").borderWidth(1).horizontalAlignment(HorizontalAlignment.RIGHT).build()) .build()) .addRow(Row.builder() .add(TextCell.builder().text("Two Two").borderWidth(1).horizontalAlignment(HorizontalAlignment.RIGHT).build()) .add(ParagraphCell.builder() .paragraph(ParagraphCell.Paragraph.builder() .append(StyledText.builder().text("This is some text in one font.").build()) .appendNewLine() .build()) .build()) .build()) .build(); TableDrawer tableDrawer = TableDrawer.builder() .contentStream(contentStream) .startX((page.getMediaBox().getUpperRightX() - 200) / 2) .startY(page.getMediaBox().getUpperRightY() - 20) .table(myTable) .build(); System.out.println(page.getMediaBox().getUpperRightY()); System.out.println(page.getMediaBox().getUpperRightX()); tableDrawer.draw(); contentStream.close(); document.save("example.pdf"); } catch (IOException e) { System.out.println(e.getMessage()); } }}
谢谢。
最佳答案
Vandaer建议此解决方案:
TableDrawer tableDrawer = TableDrawer.builder()
.contentStream(contentStream)
.page(page) // <-- This was missing
.startX((page.getMediaBox().getUpperRightX() - 200) / 2)
.startY(page.getMediaBox().getUpperRightY() - 20)
.table(myTable)
.build();