iText Можете ли вы использовать PdfPTable с HtmlWriter

Я работаю над устаревшим Java-приложением. Приложение использует 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>

0 ответов

Другие вопросы по тегам