2015-12-29 204 views
0

我有一張標題和一張表格供內容使用。我的內容表太長,並且不適合單個頁面。所以我想在每頁後重復我的標題表。我怎樣才能做到這一點?請幫忙。如何在所有頁面上提供重複標題 - iText pdf

這裏是我的代碼

File tempFile = File.createTempFile("progress", "tmp"); 
Document document = new Document(PageSize.A4); 
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(tempFile));    
document.setMargins(6, 6, 36, 36); 
document.setMarginMirroring(true); 
document.open(); 
PdfPTable titleTable = new PdfPTable(new float[] { 18f }); 
// Image img = Image.getInstance(uploadFolder + "/logo.jpg"); 
Font titleFont = new Font(getBaseFont(), 12f, Font.BOLD); 
Font tHeadFont = new Font(getBaseFont(), 10f, Font.BOLD); 
Font contentFont = new Font(getBaseFont(), 10f); 
// img.scalePercent(50); 
titleTable.setWidthPercentage(100); 
titleTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER); 
titleTable.getDefaultCell().setHorizontalAlignment(PdfPCell.ALIGN_CENTER); 
// titleTable.addCell(new Paragraph(new Chunk(img, 5, -5))); 
document.add(gutter()); 
titleTable.addCell(new Paragraph("Title", titleFont));   
document.add(titleTable); 
document.add(gutter()); 
document.add(gutter()); 

PdfPTable comTable = new PdfPTable(new float[] { 6f, 4f, 2f, 3f, 6f, 3f }); 
PdfPCell cell1 = new PdfPCell(new Paragraph("Module", tHeadFont)); 
cell1.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE); 
cell1.setPaddingBottom(4); 
comTable.addCell(cell1); 
PdfPCell cell2 = new PdfPCell(new Paragraph("Name", tHeadFont)); 
cell2.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); 
cell2.setPaddingBottom(4); 
comTable.addCell(cell2); 
PdfPCell cell3 = new PdfPCell(new Paragraph("Serial", tHeadFont)); 
cell3.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE); 
cell3.setPaddingBottom(4); 
comTable.addCell(cell3); 
PdfPCell cell4 = new PdfPCell(new Paragraph("Lesson", tHeadFont)); 
cell4.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell4.setVerticalAlignment(Element.ALIGN_MIDDLE); 
cell4.setPaddingBottom(4); 
comTable.addCell(cell4); 
PdfPCell cell5 = new PdfPCell(new Paragraph("Topic", tHeadFont)); 
cell5.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell5.setVerticalAlignment(Element.ALIGN_MIDDLE); 
cell5.setPaddingBottom(4); 
comTable.addCell(cell5); 
PdfPCell cell6 = new PdfPCell(new Paragraph("No.of Lessons", tHeadFont)); 
cell6.setHorizontalAlignment(Element.ALIGN_CENTER); 
cell6.setVerticalAlignment(Element.ALIGN_MIDDLE); 
cell6.setPaddingBottom(4); 
comTable.addCell(cell6); 

下面有一些重複的顯示內容是動態的,所以我不能告訴多少頁怎麼會在那裏。 我想在所有頁面上重複titleTable。

+0

請提供您的密碼。 –

+0

@TareqMahmood請檢查 – Usr1123

+0

謝謝,現在有人可以幫助你更好。 (不是我,我不是'java'傢伙)。 –

回答

2

您有不同的選擇。

方法1:定義表

而不是使用兩個表的,你可以使用一個表,並定義一個或多個標題行標題行。這是在回答說明瞭以下幾個問題:

您也可以從下面的例子一些啓示:

方法2:定義一個標頭的頁面

的瀏覽StackOverflow上的其他問題時,你可能已經發現了,你可以使用頁面事件來創建標題。不到一天前,我在這裏所說的基本原則:Header and Footer in ITextSharp

更多的例子,請參閱:

但你真正想要的是答案的問題:

總之,你需要一個頁面事件是這樣的:

public class FooterTable extends PdfPageEventHelper { 
    protected PdfPTable footer; 
    public FooterTable(PdfPTable footer) { 
     this.footer = footer; 
    } 
    public void onEndPage(PdfWriter writer, Document document) { 
     footer.writeSelectedRows(0, -1, 36, 64, writer.getDirectContent()); 
    } 
} 

Howe版本,而不是FooterTable,你應該調用這個類HeaderTable,而不是使用硬編碼的座標,你應該使用這樣的事情:

float x = document.left(36); 
float y = document.top(10); 
footer.writeSelectedRows(0, -1, x, y, writer.getDirectContent()); 

當定義xy,我用document對象作爲只讀對象以獲得左邊座標,邊距爲36,頂部座標邊距爲50.重要的是要認識到10的邊距可能不足以滿足表格的要求:表頭可能會碰到內容表。如果是這種情況,則需要計算標題表的高度並更改文檔的頂部邊距,以便標題表和內容表不重疊。

我有一個問題給你:我們重新設計了iText網站,我們在感恩節發佈了它。我們現在注意到,我們沒有像以前那樣獲得更多訪問次數。考慮到您需要的所有信息都可以在在線文檔中找到,並且考慮到您仍然需要提出這個問題,我們想知道網站有什麼問題。我們可以做些什麼來改進內容?爲了找到您的問題的答案,我使用了搜索框和標籤,如header。什麼可能是導致人們遠離我們網站的原因?我們現在是否有太多內容?

相關問題