2017-09-15 137 views
1

我使用flyingsaucer 9.1.1和itext 2.17來生成pdf文件。僅在最後一頁顯示頁腳

我怎樣才能顯示頁腳(可能是一個div,表或img元素)只在最後一頁,但沒有在其他的?

+1

iText的2.1.7是我曾經寫過的API,並[不應再被使用(https://developers.itextpdf.com/question/versions-older-than-5)。其中一個原因是你不再使用它的原因之一是它的年齡。 iText 2.1.7的日期從2009年開始。您還在使用飛碟。這是一箇舊的第三方庫,在舊的iText版本上編寫。你應該考慮使用[iText 7 + pdfHTML](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml)。瀏覽本教程,您會注意到[pdfHTML](https://itextpdf.com/itext7/pdfHTML)中有更多對CSS的支持。 –

+1

至於你的問題,你可以使用'IEventHandler'創建一個顯示自定義行爲的頁腳,如果你決定做正確的事情並升級到iText 7。[教程](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-4-creating-reports-using-pdfhtml)有一個自定義頁眉或頁腳可以如何創建的例子。您可能還想閱讀[此評論](https://stackoverflow.com/questions/46238645/utf-8-encoding-characters-not-working-with-spring-mvc-lowagie-itext#comment79441200_46238645) –

回答

3

不幸的是,CSS定義了page:first僞元素,但沒有定義page:last

您仍有可能利用飛碟的bug,這使得頁腳僅在聲明後纔出現。因此,如果您將頁腳div置於HTML的末尾,它將只出現在最後一頁上。

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<style type="text/css"> 
    .newpage{page-break-before:always} 

    @page{@bottom-center {content: element(footer)}} 
    #footer {position: running(footer);}  
} 
</style> 
</head> 
    <body> 
     <div>Page 1 content</div> 
     <div class="newpage">Page 2 content</div> 
     <div class="newpage">Page 3 content</div> 
     <div id="footer">Footer on last page</div> 
    </body> 
</html> 

它也可以,如果你想在每一頁上一個頁腳,具有不同內容的最後一頁。您只需在HTML中定義頁腳div兩次。

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<style type="text/css"> 
    .newpage{page-break-before:always} 

    @page{ 
     @bottom-center {content: element(footer)}} 
     #footer {position: running(footer);}  
    } 
</style> 
</head> 
    <body> 
     <div id="footer">Footer for all pages except last</div> 
     <div>Page 1 content</div> 
     <div class="newpage">Page 2 content</div> 
     <div class="newpage">Page 3 content</div> 
     <div id="footer">Footer on last page</div> 
    </body> 
</html>