2011-12-05 52 views
3

我使用PdfPTables爲我的PDF文檔創建了頁眉和頁腳。我在PdfPageEventHelper的onStartPage和onEndPage事件中分別指定了頁眉和頁腳。iText5中的頁眉重疊

我正面臨的問題是在我的文檔中添加段落。

當我創建一個新段落如下:

Paragraph content = new Paragraph("This is a test text"); 
try{ 
    pdfDocument.add(content); 
} catch (DocumentException e){ 
    e.printStackTrace(); 
} 

內容與報頭重疊。我需要的是在頁眉和頁腳之間設置段落。有人可以告訴我爲了將標題放在頁眉和頁腳之間而不是它們之間需要做什麼。

感謝

回答

1

您需要正確設置頁邊距:在底部20

Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom) 
+0

謝謝。但我在哪裏設置邊距?頁眉後添加了頁腳?或之前?另外,在將頁眉和頁腳添加到文檔後邊距是否會受到影響?似乎iText沒有相對定位的概念。 –

+0

這裏您可以在開始處設置頁邊距:Document document = new Document(PageSize.A4,* margin ... *); – VahidN

4

設置的空白超過所需。 例如一般來說,你不斷從底部marging是40

document.setMargins(50, 45, 50, 40); 

現在,請保持60

writer=PdfWriter.getInstance(document, out); 
document.setPageSize(PageSize.A4); 
document.setMargins(50, 45, 50, 60); 
document.setMarginMirroring(false); 

writer.setPageEvent(new HeaderAndFooter()); 
document.open(); 

現在的HeaderFooter PageEvent頁腳設置在document.bottom() - 20位。

public class HeaderAndFooter extends PdfPageEventHelper { 
    private Font footerFont; 
    public HeaderAndFooter() { 
     super(); 
     footerFont = getFontObj(BaseColor.LIGHT_GRAY, 15); 
     footerFont.setStyle(Font.ITALIC); 
    } 


    @Override 
    public void onEndPage(PdfWriter writer, Document document) { 
     PdfContentByte cb = writer.getDirectContent(); 
     ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, new Phrase(String.format("Page %d", writer.getPageNumber()),footerFont), (document.left() + document.right())/2 , document.bottom()-20, 0); 
    } 
} 

它將解決重疊的問題。它對我來說工作得很好。

-1

我知道它來不及回答這個問題,但希望如此,這將幫助別人,我也得到了同樣的問題:

HeaderFooter類的文件,還有一些類的兩個構造函數 1:

HeaderFooter header = new HeaderFooter(new Phrase(chunk), new Phrase("\n\n\n\n")); 

這裏new Phrase(chunk)可用於你的頭和其他短語與\n donw是重疊將移動文本,但有一個小問題,即它顯示在頁眉的頁碼。

爲了避免這個問題,我用2號構造:

HeaderFooter header = new HeaderFooter(new Phrase(75f, chunk), false); 

這是更連擊的選擇,因爲在這句話我有塊,我需要在標題顯示和一起加入的75點上邊距還說頂部的頁碼爲假(不),這解決了我的問題。