Generating header/footer with flying saucer (xHTMLRenderer) and iText

后端 未结 3 1222
野的像风
野的像风 2020-12-08 10:57

I realize this question has been asked before (I looked at all the solutions and tried them all) but I am still trying to generate a pdf document with a header and footer th

相关标签:
3条回答
  • 2020-12-08 11:08

    Here is a working example:

    package com.sg2net.test;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    
    import org.xhtmlrenderer.pdf.ITextRenderer;
    
    import com.lowagie.text.DocumentException;
    
    public class XHTMLRenderer8 {
    
        /**
         * @author Giovanni Cuccu
         */
        public static void main(String[] args) throws FileNotFoundException, DocumentException {
            ITextRenderer renderer = new ITextRenderer();
            String content="<html><head><style>\n" +
              "div.header {\n" +
              "display: block; text-align: center;\n" + 
              "position: running(header);}\n" +
              "div.footer {\n" +
              "display: block; text-align: center;\n" + 
              "position: running(footer);}\n" +
              "div.content {page-break-after: always;}" +
              "@page { @top-center { content: element(header) }}\n " +
              "@page { @bottom-center { content: element(footer) }}\n" +
              "</style></head>\n" +
              "<body><div class='header'>Header</div><div class='footer'>Footer</div><div class='content'>Page1</div><div>Page2</div></body></html>";
            renderer.setDocumentFromString(content);
            renderer.layout();
            renderer.createPDF(new FileOutputStream("test.pdf"));
    
        }
    
    }
    

    This is using the following XHTML document

    <html>
    <head>
    <style>
    div.header {
        display: block; text-align: center; 
        position: running(header);
    }
    div.footer {
        display: block; text-align: center;
        position: running(footer);
    }
    div.content {page-break-after: always;}
    @page {
         @top-center { content: element(header) }
    }
    @page { 
        @bottom-center { content: element(footer) }
    }
    </style>
    </head>
    <body>
        <div class='header'>Header</div>
        <div class='footer'>Footer</div>
        <div class='content'>Page1</div>
        <div>Page2</div>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-12-08 11:21

    After researching and testing a lot, I just came up with a solution that really works.

    You can control:

    - header height by editing margin-top;

    - footer height by editing margin-bottom;

    - content width by editing div.content width.

    Page numbers are displayed at footer.

    See code below:

    <html>
    <head>
    <style>
    
    @page{
    
        @bottom-left {                 
            content: element(footer);  
            vertical-align: top;
            padding-top: 10px;
    /*      border: solid red;    */
        }
    
        @top-right {
            content: element(header); 
            vertical-align: bottom;
            padding-bottom: 10px;
    /*          border: solid green;   */
        }
    
        size: A4 portrait;
        margin-top:5.5cm; 
        margin-left:3cm; 
        margin-right:2cm; 
        margin-bottom:3.3cm; 
    }
    
    div.header {
        display: block;                     
        position: running(header);
        border-bottom: 1px solid black;
    }
    
    div.footer {
        margin-top: 0.5cm;
        display: block;
        position: running(footer);
        border-top: 1px solid black; 
    }
    
    div.content {
    /*  border: solid purple;  */
        display: block;
        width: 15.4cm; 
        text-align: justify;
    }
    
    #pagenumber:before {
        content: counter(page);
    }
    
    #pagecount:before {
        content: counter(pages);
    }
    
    </style>
    </head>
    <body>
    
        <div class="header">
            This is the header that will repeat on every page at top
        </div>
    
        <div class="footer" >
            <p>This is the footer that will repeat on every page at bottom</p>
            <p>Page <span id="pagenumber"></span> of <span id="pagecount"></span></p>/
        </div>
    
        <div class="content">
            <p>This is the content</p><p>This is the content</p><p>This is the content</p>
            <p>This is the content</p><p>This is the content</p><p>This is the content</p>
            <p>This is the content</p><p>This is the content</p><p>This is the content</p>
            <p>This is the content</p><p>This is the content</p><p>This is the content</p>
            <p>This is the content</p><p>This is the content</p><p>This is the content</p>
            <p>This is the content</p><p>This is the content</p><p>This is the content</p>
            <p>This is the content</p><p>This is the content</p><p>This is the content</p>
            <p>This is the content</p><p>This is the content</p><p>This is the content</p>
            <p>This is the content</p><p>This is the content</p><p>This is the content</p>
            <p>This is the content</p><p>This is the content</p><p>This is the content</p>
            <p>This is the content</p><p>This is the content</p><p>This is the content</p>
            <p>This is the content</p><p>This is the content</p><p>This is the content</p>
            <p>This is the content</p><p>This is the content</p><p>This is the content</p>
        </div>
    
    </body>
    </html>
    

    Hope it helps!!!

    0 讨论(0)
  • 2020-12-08 11:27

    for itext using java checkout this

    public class HeaderAndFooter extends PdfPageEventHelper {
    
    public void onEndPage (PdfWriter writer, Document document) {
        Rectangle rect = writer.getBoxSize("art");
        switch(writer.getPageNumber() % 2) {
        case 0:
            ColumnText.showTextAligned(writer.getDirectContent(),
                    Element.ALIGN_RIGHT, new Phrase("even header"),
                    rect.getBorderWidthRight(), rect.getBorderWidthTop(), 0);
            break;
        case 1:
            ColumnText.showTextAligned(writer.getDirectContent(),
                    Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())),
                    300f, 62f, 0);
            break;
        }
        ColumnText.showTextAligned(writer.getDirectContent(),
                Element.ALIGN_CENTER, new Phrase(String.format("%d", writer.getPageNumber())),
                (2f + 4f) / 2, 2f - 18, 0);
    }
    }
    

    using below one in ur pdfwriter

    HeaderAndFooter event = new HeaderAndFooter();
            writer.setPageEvent(event);
    
    0 讨论(0)
提交回复
热议问题