pdf itext页脚和页眉未正确对齐

时间:2018-10-19 11:22:14

标签: html itext

我正在尝试将页眉和页脚添加到pdf中。我能够以pdf格式获取内容,但问题是内容的对齐方式。任何人都可以验证我的以下代码。页脚的内容与图像重叠。

public class TestPdf {
public static final String DEST = "D://html_header_footer.pdf";

public static final String HEADER = "<table cellspacing=\"0\" cellpadding=\"0\" style=\"width:100%;border:1px solid red;margin:10px;\"><tr><td><img  src=\"C:\\Users\\459495\\Desktop\\header.png\" alt=\"AMHI\"/></td></tr></table>";



public static final String FOOTER = "<html><table  cellspacing=\"0\" cellpadding=\"0\" style=\"width:100%;border:1px solid red;\">\r\n" + 
        "<tr><td colspan=\"2\" style=\"padding:0px 0px 0px 35px;background:green;\"><br/>This is a computer generated letter and does not require signature.</td></tr><tr style =\"width:20%;border:1px solid red;background:black;\"><td style =\"width:20%;border:1px solid red;\"><br/><img width=\"261\" height=\"82\"src=\"C://Users/459495/Desktop/footer.png\" alt=\"AMHI\"/></td><td valign=\"top\" style=\"width:80%;font-family:Helvetica;font-size:9px;padding:10px;border:1px solid red;\"><br/><b>Apollo Munich Health Insurance Co. Ltd.</b> <br/>Central Processing Center, 2nd & 3rd Floor, iLABS Centre, Plot No. 404-405, Udyog Vihar, Phase-III, Gurgaon - 122 016,  <br/>Haryana <b>Corp. Office:</b> 1st Floor, SCF -19, Sector - 14, Gurgaon - 122 001, Haryana. <b>Regd. Office:</b> Apollo Hospitals Complex,<br/>8-2-293/82/J III/DH/900 Jubilee Hills, Hyderabad, Telangana - 500033, India. • <b>Tel : </b>+91 124 4584333 • <b>Fax :</b> +91 124 4584111<br/>• <b>Website:</b> www.apollomunichinsurance.com • <b>Email:</b> <a href=\"mailTo:customerservice@apollomunichinsurance.com\">customerservice@apollomunichinsurance.com</a> • <b>IRDAI Reg. No.:</b>- 131<br/>• <b>CIN:</b> U66030TG2006PLC051760</td></tr></table>\r\n" + 
        "</html>";
public class HeaderFooter extends PdfPageEventHelper {

    protected ElementList header;
    protected ElementList footer;
    public HeaderFooter() throws IOException {
        header = XMLWorkerHelper.parseToElementList(HEADER, null);
        footer = XMLWorkerHelper.parseToElementList(FOOTER, null);
    }
    @Override
    public void onEndPage(PdfWriter writer, Document document) {
        try {
            //document.setMargins(300, 300, 300, 300);
            ColumnText ct = new ColumnText(writer.getDirectContent());
           // ct.setSimpleColumn(new Rectangle(0, 840,500, 400));
            ct.setSimpleColumn(new Rectangle(0, 800,600, 0));
            for (Element e : header) {
                ct.addElement(e);
            } 
            ct.go();
            ct.setSimpleColumn(new Rectangle(0,110,600,0));
            for (Element e : footer) {
                ct.addElement(e);
            }
            ct.go();
        } catch (DocumentException de) {
            throw new ExceptionConverter(de);
        }
    }
}

public static void main(String[] args) throws IOException, DocumentException {
    File file = new File(DEST);
    file.getParentFile().mkdirs();
    new TestPdf().createPdf(DEST);
}

public void createPdf(String filename) throws IOException, DocumentException {
    // step 1

    Document document = new Document(PageSize.LETTER, 20, 20, 60, 160);//left/right/top/down

    // step 2
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
    writer.setPageEvent(new HeaderFooter());
    // step 3
    document.open();
    // step 4
  //  for (int i = 0; i < 10; i++)
        document.add(new Paragraph("Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!\n"
                + "Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!\n"
                + "Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!\n"
                + "Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!\n"
                + "Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!\n"
                + "Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!\n"
                + "Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!\n"
                + "Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!\n"
                + "Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!\n"
                + "Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!\n"
                + "Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!\n"
                + "Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!\n"
                + "Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!Hello World!"));
   // document.newPage();
/*    document.add(new Paragraph("Hello World!"));
    document.newPage();
    document.add(new Paragraph("Hello World!"));*/
    // step 5
    document.close();
}

}

0 个答案:

没有答案