我使用以下代码:
protected void buildPdfDocument(Map<String, Object> model,
Document document, PdfWriter writer,
HttpServletRequest request,HttpServletResponse response)
throws Exception {
try {
ArrayList<OrderDetails> list = (ArrayList<OrderDetails>)model.get("orderDetailsList");
document.setMargins(-24, -24, 135,60);
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));
HeaderFooter event = new HeaderFooter();
writer.setPageEvent((PdfPageEvent) event);
PdfContentByte cb = writer.getDirectContent();
PdfPTable upperTable = new PdfPTable(3);
upperTable.setTotalWidth(515);
upperTable.getDefaultCell().setPadding(6);
upperTable.setWidthPercentage(100.0f);
for(OrderDetails orderDetails : list) {
document.open();
Font f3 = new Font(FontFamily.TIMES_ROMAN, 12.0f, Font.BOLD, BaseColor.GREEN);
Font f1 = new Font(FontFamily.TIMES_ROMAN, 12.0f, Font.BOLD, BaseColor.WHITE);
Font f2 = new Font(FontFamily.TIMES_ROMAN, 12.0f, Font.BOLD, BaseColor.BLACK);
PdfPTable table1 = new PdfPTable(4);
Font font = FontFactory.getFont(FontFactory.HELVETICA);
font.setColor(BaseColor.BLACK);
// define table header cell
PdfPCell cell2 = new PdfPCell();
cell2.setBackgroundColor(BaseColor.WHITE);
cell2.setPadding(5);
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
table1.getDefaultCell().setVerticalAlignment(Element.ALIGN_CENTER);
// write table header
cell2.setPhrase(new Phrase("Sno", font));
table1.addCell(cell2);
cell2.setPhrase(new Phrase("ISBN", font));
table1.addCell(cell2);
cell2.setPhrase(new Phrase("Quantity", font));
table1.addCell(cell2);
cell2.setPhrase(new Phrase("Status", font));
table1.addCell(cell2);
Date strDate=orderDetails.getDataInserted();
DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String createdDate = formatter.format(strDate);
upperTable.setWidths(new float[] {33,33,33});
upperTable.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
upperTable.getDefaultCell().setBackgroundColor(BaseColor.BLACK);
upperTable.addCell(new Phrase(" Bhavish Order Id :"+orderDetails.getBhavishOrderId(),f1));
upperTable.addCell(new Phrase("Client Name:"+orderDetails.getClientName(),f1));
upperTable.addCell(new Phrase(" Order Date:"+orderDetails.getOrderDate(),f1));
upperTable.writeSelectedRows(0, -1,40,734,cb );
upperTable.flushContent();
upperTable = new PdfPTable(2);
upperTable.setTotalWidth(515);
upperTable.getDefaultCell().setPadding(6);
upperTable.setWidths(new float[] {70,30});
PdfPCell cell1 = upperTable.getDefaultCell();
cell1.setBorder(Rectangle.NO_BORDER);
Barcode128 code128 = new Barcode128();
code128.setBarHeight(10);
code128.setSize(5);
code128.setCode("0123456789");
upperTable.addCell(new Phrase("Batch ID :"+orderDetails.getBatchId(),f2));
upperTable.getDefaultCell().setRowspan(2);
upperTable.addCell(code128.createImageWithBarcode(cb, null, null));
upperTable.getDefaultCell().setRowspan(1);
upperTable.addCell(new Phrase("Epz/Nepz :"+orderDetails.getEpzNepz(),f2));
document.add(upperTable);
upperTable.flushContent();
upperTable = new PdfPTable(3);
upperTable.setWidths(new float[] {40,30,30});
upperTable.setTotalWidth(515);
upperTable.getDefaultCell().setPadding(6);
upperTable.getDefaultCell().setBackgroundColor(BaseColor.GRAY);
upperTable.addCell(new Phrase(" Order Details - :",f1));
upperTable.addCell(new Phrase("Type:"+orderDetails.getOrderType(),f1));
upperTable.addCell(new Phrase(" On :"+createdDate,f1));
document.add(upperTable);
upperTable.flushContent();
PdfPTable table = new PdfPTable(6);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
table.setWidths(new float[] {19,2,29,20,2,29});
table.setSpacingBefore(5);
table.getDefaultCell().setPadding(6);
PdfPCell cell = table.getDefaultCell();
cell.setBorder(Rectangle.NO_BORDER);
cell.setHorizontalAlignment(Element.ALIGN_MIDDLE);
table.addCell(new Phrase("Client Name :",f2));
table.addCell(new Phrase(":"));
table.addCell(new Phrase(orderDetails.getClientName()));
table.addCell(new Phrase("Urgent/Normal :",f2));
table.addCell(new Phrase(":"));
table.addCell(new Phrase(orderDetails.getNormalUrgent()));
table.addCell(new Phrase("Order Number :",f2));
table.addCell(new Phrase(":"));
table.addCell(new Phrase(orderDetails.getOrderNumber()));
Set<OrderIsbnDetails> list1 = orderDetails.getOrderIsbnDetails();
int ListISBN=list1.size();
table.addCell(new Phrase("Total ISBN :",f2));
table.addCell(new Phrase(":"));
table.addCell(new Phrase(String.valueOf(ListISBN)));
document.add(table);
table.flushContent();
int i=0;
for (OrderIsbnDetails orderIsbnDetails : list1) {
i=i+1;
table1.setWidthPercentage(70.0f);
table1.setWidths(new float[] {0.2f, 1.0f, 0.4f, 1.0f});
table1.setSpacingBefore(0);
cell2.setPhrase(new Phrase(String.valueOf(i),font));
cell2.setVerticalAlignment (Element.ALIGN_CENTER);
table1.addCell(cell2);
cell2.setPhrase(new Phrase(orderIsbnDetails.getIsbn(),font));
System.out.println("The ISBN List "+orderIsbnDetails.getIsbn());
cell2.setVerticalAlignment (Element.ALIGN_CENTER);
table1.addCell(cell2);
cell2.setPhrase(new Phrase(String.valueOf(orderIsbnDetails.getQuantity()),font));
System.out.println("The ISBN List "+String.valueOf(orderIsbnDetails.getQuantity()));
cell2.setVerticalAlignment (Element.ALIGN_CENTER);
table1.addCell(cell2);
cell2.setPhrase(new Phrase(orderIsbnDetails.getIsbnstatus(),font));
System.out.println("The ISBN Status"+orderIsbnDetails.getIsbnstatus());
cell2.setVerticalAlignment (Element.ALIGN_CENTER);
table1.addCell(cell2);
document.add(table1);
table1.flushContent();
}
document.close();
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("To Check the error In Order Detail PDF"+e);
}
}
它会抛出以下错误:
该文件已经关闭。 您无法添加任何元素。
答案 0 :(得分:2)
请正确格式化您的代码,并且您会发现您违反了&#34;以5个步骤创建文档&#34;原理。这就是iText的工作原理:
// step 1: create the document
Document document = new Document();
// step 2: create a writer that
// a. listens to the document
// b. writes to an outputstream
PdfWriter.getInstance(document, os);
// step 3: open the document
document.open();
// step 4: add as much content you want
document.add(...);
// step 5: close the document
document.close();
在你的代码中,你有这个:
// step 1: create the document
Document document = new Document();
// step 2: create a writer that
// a. listens to the document
// b. writes to an outputstream
PdfWriter.getInstance(document, os);
for (some iterator) {
// step 3: open the document
document.open();
// step 4: add as much content you want
document.add(...);
// step 5: close the document
document.close();
}
代码一旦完成循环,文档就会关闭,您无法再添加任何新内容。你需要改变你的循环:
// step 1: create the document
Document document = new Document();
// step 2: create a writer that
// a. listens to the document
// b. writes to an outputstream
PdfWriter.getInstance(document, os);
// step 3: open the document
document.open();
// step 4: add as much content you want
for (some iterator) {
document.add(...);
}
// step 5: close the document
document.close();
现在您仍然可以添加任意数量的内容,但是您只关闭了一次文档。
更新:我尝试在您的位置格式化您的代码,但由于存在一些不平衡的{
和}
实例,因此很难做到。在您的代码段中,我也没有看到您在任何地方打开文档。我确信这发生在外面 buildPdfDocument
方法。也许document
对象也在此方法之外关闭。如果是这样,您可以删除方法中的document.close()
。总而言之,我建议你写清洁代码。我看到很多我不喜欢的东西。如果您是我的员工,我请您编写更清晰的代码。
答案 1 :(得分:0)
我看不到你在哪里初始化文档对象,但在你的方法中这样做,所以每次你输入你的方法时,都会生成一个新的文档实例,你可以覆盖旧的pdf版本文件 - 因为这就是你想要实现的目标,我猜。似乎无法再次重新打开已创建的 Document 实例。
public class FormularCreator {
private static Document document;
public static void createFormular() throws IOException, DocumentException {
document = new Document(PageSize.A4, 70, 50, 50, 50);
以及您的其余代码..