我正在尝试使用iText
创建PDF。
我的目标是提出一份试卷。我只能添加PDF的问题和答案。
然后我尝试通过添加页眉/页脚添加页码,水印等来修改它。现在它给了我NullPointerException
我无法弄清楚出了什么问题。
我遇到错误
CreateAssignment.java 类中的引起:java.lang.NullPointerException 在 edu.ijse.gdse41.ams.other.HeaderFooter.onEndPage(HeaderFooter.java:57) at com.itextpdf.text.pdf.PdfDocument.newPage(PdfDocument.java:902) 在com.itextpdf.text.pdf.PdfDocument.close(PdfDocument.java:837)at com.itextpdf.text.Document.close(Document.java:416)at edu.ijse.gdse41.ams.view.CreateAssignmentController.createPDF(CreateAssignmentController.java:644) 在 edu.ijse.gdse41.ams.view.CreateAssignmentController.proceedBtnClicked(CreateAssignmentController.java:292) ......还有58个
createPDF()
方法
private void createPDF(ArrayList<Assignment_QuesDTO> questionPaper) throws DocumentException, BadElementException, IOException {
try {
OutputStream outputStream = null;
Document doc = new Document();
outputStream = new FileOutputStream(new File("C:\\Users\\Dell\\Documents\\NetBeansProjects\\AssignmentManagementSystem\\src\\PDF\\mypdf.pdf"));
PdfWriter writer = PdfWriter.getInstance(doc, outputStream);
PdfPageEventHelper eventHelper = new HeaderFooter(doc);
writer.setPageEvent(eventHelper);
doc.open();
Font fontTitle = new Font(Font.getFamily("TIMES_ROMAN"), 15);
Paragraph title = new Paragraph("ABC", fontTitle);
title.setAlignment(Paragraph.ALIGN_CENTER);
doc.add(title);
for (int i = 0; i < 3; i++) {
doc.add(Chunk.NEWLINE);
}
Paragraph subTitle = new Paragraph(questionPaper.get(1).getAssignment().getAssignName());
subTitle.setAlignment(Paragraph.ALIGN_CENTER);
Paragraph subTitle2 = new Paragraph(questionPaper.get(1).getAssignment().getDate());
subTitle2.setAlignment(Paragraph.ALIGN_CENTER);
doc.add(subTitle);
doc.add(subTitle2);
List orderedList = new List(List.ORDERED);
for (Assignment_QuesDTO questionPaper1 : questionPaper) {
Paragraph question = new Paragraph(questionPaper1.getQuestion().getQues());
question.setAlignment(Paragraph.ALIGN_JUSTIFIED);
orderedList.add(question);
List desc = new List(List.UNORDERED);
desc.setIndentationLeft(36);
desc.setListSymbol(new Chunk(" "));
desc.add(new Phrase("\t\t" + questionPaper1.getQuestion().getQuesDesc()));
orderedList.add(desc);
orderedList.add(Chunk.NEWLINE);
List answers = new List(List.ORDERED,List.ALPHABETICAL);
answers.setIndentationLeft(72);
for (AnswerDTO answer : questionPaper1.getQuestion().getAnswers()) {
answers.add(" " + answer.getAnswer());
}
orderedList.add(answers);
orderedList.add(Chunk.NEWLINE);
}
doc.add(orderedList);
doc.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(CreateAssignmentController.class.getName()).log(Level.SEVERE, null, ex);
}
}
HeaderFooter.java类
public class HeaderFooter extends PdfPageEventHelper {
Phrase[] header = new Phrase[2];
int pageNum;
Image watermark;
public HeaderFooter(Document doc) throws BadElementException, IOException {
this.watermark = Image.getInstance("C:\\Users\\Dell\\Documents\\NetBeansProjects\\AssignmentManagementSystem\\src\\edu\\ijse\\gdse41\\ams\\resources\\images\\watermark.png");
watermark.rotate();
watermark.scaleToFit(doc.getPageSize());
watermark.setRotationDegrees(30);
}
@Override
public void onChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) {
header[1] = new Phrase(title.getContent());
pageNum = 1;
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
try {
Rectangle rect = writer.getBoxSize("art");
switch (writer.getPageNumber() % 2) {
case 0:
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, header[0], rect.getRight(), rect.getTop(), 0);
break;
case 1:
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, header[1], rect.getLeft(), rect.getTop(), 0);
break;
}
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, new Phrase(String.format("Page %d", pageNum)), (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
PdfContentByte content = writer.getDirectContent();
content.addImage(watermark);
} catch (DocumentException ex) {
Logger.getLogger(HeaderFooter.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void onStartPage(PdfWriter writer, Document document) {
pageNum++;
}
@Override
public void onOpenDocument(PdfWriter writer, Document document) {
header[0] = new Phrase("ABC");
}
}
答案 0 :(得分:0)
在您的页面事件中,您假设您正在创建的PDF具有/ArtBox
边界:
Rectangle rect = writer.getBoxSize("art");
但是,当我查看创建PDF的代码时,我看不到你在任何地方创建这样的页面边界。这意味着rect
为空,而rect.getRight()
,rect.getTop()
等方法会抛出NullPointerException
。