使用Java iText

时间:2019-04-10 19:41:43

标签: java itext

我有一个使用iText 5.5.13的简单项目。我需要两个HTML代码,一个用于正文,另一个用于页脚。我能够在项目主体中放入HTML代码。我在分隔正文和页脚中的代码时遇到了麻烦。

project.java

package project;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;


public class project {
    public static String html_body = "<p>My HTML Body here</p>";
    public static String html_footer = "<p>My HTML Footer here</p>";

    public static String dest = "/tmp/report.pdf";

    public static void createPdf(String html_body, String html_footer, String dest) throws IOException, DocumentException {
        Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50);

        PdfWriter.getInstance(document, new FileOutputStream(new File(dest)));
        document.open();

        HTMLWorker htmlWorker = new HTMLWorker(document);
        htmlWorker.parse(new StringReader(html_body));

        // How do I include HTML footer here?

        document.close();
    }

    public static void main(String[] args) throws IOException, DocumentException {
        createPdf(html_body, html_footer, dest);
    }
}

0 个答案:

没有答案