我正在开发一个遗留的Java Web应用程序。该应用程序使用iText 2.0.1。他们不想升级iText版本,所以我必须继续使用这个版本。
我需要输出到pdf或html。我正在创建一个PdfPTable。我创建了第二个PdfPTable并添加了一些PdfPCell。我将第二个表添加到第一个表中。
pdf输出效果很好。 Html输出为空。
这是我的测试代码。
public void createOutput(String outputType) {
Document document = null;
DocWriter docWriter = null;
FileOutputStream outputFO = null;
File pdfFile = new File("output.pdf");
File htmlFile = new File("output.html");
try {
document = new Document(PageSize.LETTER.rotate(), 2, 2, 5, 2);
if (outputType.equals("pdf")) {
outputFO = new FileOutputStream(pdfFile);
docWriter = PdfWriter.getInstance(document, outputFO);
pdfContentByte = new PdfContentByte((PdfWriter) docWriter);
}
else {
outputFO = new FileOutputStream(htmlFile);
docWriter = HtmlWriter.getInstance(document, outputFO);
}
document.open();
PdfPTable outputTable = new PdfPTable(1);
PdfPTable innerTable = new PdfPTable(5);
float widths[] = { 156f, 156f, 156f, 156f, 156f };
outputTable.setTotalWidth(780);
outputTable.setLockedWidth(true);
innerTable.setTotalWidth(widths);
innerTable.setLockedWidth(true);
// Add some PdfPCell objects to the inner table.
// Add inner table to output Table
outputTable.addCell(innerTable);
document.add(outputTable);
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
finally {
if (docWriter != null && !docWriter.isCloseStream()) {
try {
docWriter.close();
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
if (document != null && document.isOpen()) {
try {
document.close();
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
try {
if (outputFO != null) {
outputFO.close();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
这是html文件的来源:
<html>
<head>
<!-- iText 2.0.1 (by lowagie.com) -->
<!-- CreationDate: Thu Sep 03 21:00:47 GMT 2015 -->
</head>
<body leftmargin="2.0" rightmargin="2.0" topmargin="5.0" bottommargin="2.0">
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
<div style="page-break-before: always; " />
</body>
</html>