我想使用iText创建4个pdf文件。在运行代码时,前两个代码已成功创建。 remaning two抛出异常“ExceptionConverter:java.io.IOException:文档没有页面。”
以下是pdf创建部分的代码:
public static Boolean createPDFFile(String filename, int headerType, List<DremelitemDetails> itemDetails) {
try {
path = new File(".").getCanonicalPath();
column = 0;
// create the pdf file
props.load(new FileInputStream("Properties\\eng.properties"));
COLUMNS = new float[][] {
{ MARGIN_LEFT, MARGIN_BOTTOM, (PageSize.LETTER.rotate().getWidth() - MARGIN_ENTER_COLUMNS) / 2,
PageSize.LETTER.rotate().getTop(MARGIN_TOP) },
{ (PageSize.LETTER.rotate().getWidth() + MARGIN_ENTER_COLUMNS) / 2, MARGIN_BOTTOM,
PageSize.LETTER.rotate().getRight(MARGIN_RIGHT), PageSize.LETTER.rotate().getTop(MARGIN_TOP) } };
String fontPath = "font\\arialuni.TTF";
unicodeFont = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
mmPDFDocument = new Document(PageSize.LETTER.rotate(), MARGIN_LEFT, MARGIN_RIGHT, MARGIN_TOP, MARGIN_BOTTOM);
pdfWriter = PdfWriter.getInstance(mmPDFDocument, new FileOutputStream(filename));
mmPDFDocument.setPageSize(PageSize.LETTER.rotate());
/*String[] footer = { props.getProperty("header"), "fdftfdbfbug",
"\u00a9" + props.getProperty("footer1"), props.getProperty("footer2")};*/
String[] footer = { "SUMMARY OF PROJECTS", "Home Décor",
"\u00a92011 Robert Bosch Tool Corporation. All Rigths Reserved.",
"Web content and services - on demand, on your printer www.eprintcentre.com" };
pdfWriter.setPageEvent(new HeaderFooter(mmPDFDocument.right() - mmPDFDocument.left(), footer, path, headerType, unicodeFont));
mmPDFDocument.open();
COLUMNS[0][3] = mmPDFDocument.top();
COLUMNS[1][3] = mmPDFDocument.top();
pdfColumn = new ColumnText(pdfWriter.getDirectContent());
pdfColumn.setSimpleColumn(COLUMNS[column][0], COLUMNS[column][1], COLUMNS[column][2], COLUMNS[column][3]);
pdfColumn.setSpaceCharRatio(PdfWriter.NO_SPACE_CHAR_RATIO);
/* For generating Pdf */
addItemDetails(itemDetails);
mmPDFDocument.close();
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException-------"+e);
closePDFFile();
return false;
} catch (DocumentException e) {
System.out.println("DocumentException-------"+e);
closePDFFile();
return false;
} catch (Exception e) {
System.out.println("Exception-------"+e);
return false;
}
return true;
}
public static void main(String[] args) {
DremelData dremelData = new DremelData();
DremelPDFBulid dremelPDFBulid = new DremelPDFBulid();
Document doc = dremelData.readXml("C:\\eng.xml");
ArrayList<DremelitemDetails> homeDeco = dremelData.parseNode(1, 11, doc);
ArrayList<DremelitemDetails> artsAndCrafts = dremelData.parseNode(12, 24, doc);
ArrayList<DremelitemDetails> seasonalIdeas = dremelData.parseNode(25, 36, doc);
ArrayList<DremelitemDetails> DIYRestoration = dremelData.parseNode(37, 49, doc);
try {
dremelPDFBulid.createPDFFile("c:\\sam1.pdf", 0, homeDeco);
dremelPDFBulid.createPDFFile("c:\\sam2.pdf", 1, artsAndCrafts);
dremelPDFBulid.createPDFFile("c:\\sam3.pdf", 2, seasonalIdeas);
dremelPDFBulid.createPDFFile("c:\\sam4.pdf", 3, DIYRestoration);
} catch (Exception e) {
System.out.println("Error in readXml : " + e.toString());
}
}
在上面的代码中,sam3和samp4 pdf的内容为空,显示“文档没有页面”。例外