2010-05-10 62 views
0

我正在使用iText來生成PDF和RTF。我知道用iText生成RTF並不流行,但我仍然需要使代碼適用於兩者。從技術上講,我可以有不同的代碼片段來處理每種類型,但它需要能夠被包含在同一個文件/類中。使用iText將標題添加到PDF和RTF

我可以使用代碼是這樣的:

String outputData = getFieldValue(myRecord, myFieldName); 
if (exportToPDF) { 
    iTextPdfPCell = new PdfPCell(pdfPTable.getDefaultCell()); 
    iTextPdfPCell.setPhrase(outputData); 
    pdfPTable.addCell(iTextPdfPCell); 
} 
if (exportToRTF) { 
    iTextCell = new RtfCell(outputData); 
    iTextTable.addCell(iTextCell); 
} 

我可以添加使用的HeaderFooter將出現兩個PDF上RTFS

Phrase headerPhrase = new Phrase ("This is a page header."); 
HeaderFooter header = new HeaderFooter(headerPhrase, false); 
iTextDoc.setHeader(header); 

麻煩的是當標題變得複雜的標題。我想添加一個包含表格和圖像的標題(圖像可能在表格內)。我看到了一個不同的論壇一些代碼,暗示這樣做:

Phrase headerPhrase = new Phrase(); 
headerPhrase.add(iTextTable); 
HeaderFooter header = new HeaderFooter(headerPhrase, false); 
iTextDoc.setHeader(header); 

雖然我的初步測試表明,這不會導致任何編譯錯誤和技術上的「作品」,它開始頭表在同樣的位置文件的正文,所以它們重疊。當我將頁邊距設置得更寬以允許頭部空間時,它只是將頭向下移動到正文中。

我在線閱讀的大部分信息(和手冊中)似乎都指向使用頁面事件,但這意味着我的類必須擴展PdfPageEventHelper。如果我這樣做,

1.有什麼類似的RTF?

2.如果存在,相同的類是否可以擴展'RtfPageEventHelper'(如果存在)和PdfPageEventHelper?我不能爲每種輸出類型編寫單獨的類

3.如果沒有,是否可以在HeaderFooter類中使用表/圖像,這對於PDF和RTF都適用?

4.(我甚至不想問這個)既然iText似乎已經讓RTF生成了,我是否應該使用別的東西來生成RTF(最好也適用於PDF)?

回答

0

HeaderFooter的頁眉或頁腳顯示在每個頁面上。如果你只需要一個文檔頭文件,只需在你的內容之前添加一個標準元素(段落/ etc)到頁面。

比如我和我的文檔這樣做:

Paragraph Header = new Paragraph("Document Header", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 0))); 

document.open(); 
document.add(Header); 
document.add(otherContent); 
document.close(); 
3

我做了這種格式。是用於RTF,但我認爲與PDF相同。只爲PdfHeader更改RtfHeader

document.open(); 
Paragraph head=new Paragraph("Head"); 
Paragraph foot=new Paragraph("Foot"); 
HeaderFoot header=new RtfHeaderFooter(head); 
HeaderFoot footer=new RtfHeaderFooter(foot); 
document.setHeader(header); 
document.setFooter(footer); 
document.close(); 

我等着幫忙。

相關問題