首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用飞碟(xHTMLRenderer)和iText生成页眉/页脚

使用飞碟(xHTMLRenderer)和iText生成页眉/页脚
EN

Stack Overflow用户
提问于 2011-11-25 07:24:06
回答 2查看 24.9K关注 0票数 28

我意识到这个问题以前已经被问过了(我看了所有的解决方案,并尝试了所有的解决方案),但我仍然试图生成一个在每一页上都重复的具有页眉和页脚的pdf文档。

我正在使用flying saucer R8和iText2.0.8,我尝试了许多不同的方法来让它工作,但到目前为止都没有用。我测试过的一些方法是https://gist.github.com/626264,使用运行元素和边框http://pigeonholdings.com/projects/flyingsaucer/R8/doc/guide/users-guide-R8.html#xil_40 (css3功能),一个不适用于r8 http://today.java.net/pub/a/today/2007/06/26/generating-pdfs-with-flying-saucer-and-itext.html#page-specific-features的飞碟r7指南,以及许多其他方法都不适用于我。

我的页眉div包含另外两个带有图像的div,而我的页脚只是用来给页面编号的。html被放入一个名为buf的StringBuffer中。

代码语言:javascript
复制
buf.append("<head>");
    buf.append("<title>blabla</title> ");
    buf.append("<style type='text/css' media='print'>  ");
    buf.append("@page { size:8.5in 11in; padding:1em; @bottom-left { content: element(footer); } } ");
    buf.append("#footer { font-size: 90%; font-style: italic;  position: running(footer); top: 0; left: 0; }");
    buf.append("#pagenumber:before { content: counter(page); } ");
    buf.append("#pagecount:before { content: counter(pages); } ");
buf.append("</style></head>");
buf.append("<body>");
 buf.append("<div class='header' style='clear:both;'>");
    buf.append("<div id='moneyLogo' style='float:left'>"); 
    buf.append("<img src='logo.jpg' alt='Some alt text' />");
    buf.append("</div>");
    buf.append("<div id='canLogo' style='float:right'>");
    buf.append("<img src='someImg.gif' alt='alt text' />");
    buf.append("</div>");
    buf.append("<h3 style='text-align:center; clear:both;'>alt text</h3>");
    buf.append("<div style='text-align:center;'>");
    buf.append("Some texy text");
    buf.append("<br />"););
    buf.append("</div>");
    buf.append("</div><br /><br />");
buf.append("<div id='footer'>  Page <span id='pagenumber'/> of <span id='pagecount'/> </div>");

    buf.append("</body>");
    buf.append("</html>");

我的pdf生成得很好,只是页眉只出现在第一页,页脚只出现在最后一页的底部。当我将html放入w3c验证器中时,结果没有问题,但当我使用他们的CSS验证器时,它告诉我@page { size:8.5in 11in; padding:1em; @bottom-left { content: element(footer); } }行中的解析错误。

据我所知,从我正在阅读的所有指南来看,这是很好的。我还听说W3C CSS验证器对于CSS3规范是不完整的,所以我认为是验证器错了。

如果有人能给我一些关于去哪里寻找或想法的建议,那将使我的一周变得有趣:)

附注:必须使用飞碟R8和/或iText 2.0.8

EN

回答 2

Stack Overflow用户

发布于 2015-10-24 01:52:04

经过大量的研究和测试,我想出了一个真正有效的解决方案。

您可以控制:

页眉高度,编辑页边距-顶部;

页脚高度,编辑页边距-底部;

通过编辑div.content width来设置内容宽度。

页码显示在页脚。

请看下面的代码:

代码语言:javascript
复制
<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>

希望它能有所帮助!

票数 20
EN

Stack Overflow用户

发布于 2011-11-25 14:23:55

对于使用java的itext,请签出以下内容

代码语言:javascript
复制
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);
}
}

在您的pdfwriter中使用下面的一个

代码语言:javascript
复制
HeaderAndFooter event = new HeaderAndFooter();
        writer.setPageEvent(event);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8263416

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档