2013-06-20 116 views
4

我已創建使用iText的圖書館,現在我想添加頁腳在PDF的頁面,這一次我已經添加給代碼PDF文件動態如何使用iTextLibrary在動態pdf中添加頁眉和頁腳?

document.addHeader("My Header Title", "My Header Details"); 

但在我的PDF頁面中,此標題無法設置。什麼是我不知道的問題,如果您有任何關於它的想法,請分享您的想法。

+0

雖然你可能已經在android設備上遇到過這個問題,但你的問題並沒有包含任何特定於android的東西,接受的答案也沒有。因此,不需要[tag:android]標籤。 – mkl

回答

10

如果您使用的是當前版本的iText(即5.4.x版本爲現在)查看iText in Action — 2nd Edition中的樣本MovieHistory2,其中顯示瞭如何在創建標題時將標題(奇數頁和偶數頁的不同標題)添加到PDF中。

最重要的是PdfPageEventHelper實現

/** Inner class to add a header and a footer. */ 
class HeaderFooter extends PdfPageEventHelper { 
    /** Alternating phrase for the header. */ 
    Phrase[] header = new Phrase[2]; 
    /** Current page number (will be reset for every chapter). */ 
    int pagenumber; 

    /** 
    * Initialize one of the headers. 
    * @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(
    *  com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document) 
    */ 
    public void onOpenDocument(PdfWriter writer, Document document) { 
     header[0] = new Phrase("Movie history"); 
    } 

    /** 
    * Initialize one of the headers, based on the chapter title; 
    * reset the page number. 
    * @see com.itextpdf.text.pdf.PdfPageEventHelper#onChapter(
    *  com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document, float, 
    *  com.itextpdf.text.Paragraph) 
    */ 
    public void onChapter(PdfWriter writer, Document document, 
      float paragraphPosition, Paragraph title) { 
     header[1] = new Phrase(title.getContent()); 
     pagenumber = 1; 
    } 

    /** 
    * Increase the page number. 
    * @see com.itextpdf.text.pdf.PdfPageEventHelper#onStartPage(
    *  com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document) 
    */ 
    public void onStartPage(PdfWriter writer, Document document) { 
     pagenumber++; 
    } 

    /** 
    * Adds the header and the footer. 
    * @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(
    *  com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document) 
    */ 
    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, header[0], 
        rect.getRight(), rect.getTop(), 0); 
      break; 
     case 1: 
      ColumnText.showTextAligned(writer.getDirectContent(), 
        Element.ALIGN_LEFT, header[1], 
        rect.getLeft(), rect.getTop(), 0); 
      break; 
     } 
     ColumnText.showTextAligned(writer.getDirectContent(), 
       Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)), 
       (rect.getLeft() + rect.getRight())/2, rect.getBottom() - 18, 0); 
    } 
} 

被註冊這樣的:

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); 
    HeaderFooter event = new HeaderFooter(); 
    writer.setBoxSize("art", new Rectangle(36, 54, 559, 788)); 
    writer.setPageEvent(event); 

編輯:的要求,在評論中,onEndPage方法的簡單變種具有靜態標題而不是交替的:

public void onEndPage(PdfWriter writer, Document document) 
    { 
     Rectangle rect = writer.getBoxSize("art"); 
     ColumnText.showTextAligned(writer.getDirectContent(), 
       Element.ALIGN_RIGHT, new Phrase("My static header text"), 
       rect.getRight(), rect.getTop(), 0); 
     ColumnText.showTextAligned(writer.getDirectContent(), 
       Element.ALIGN_CENTER, new Phrase(String.format("page %d", pagenumber)), 
       (rect.getLeft() + rect.getRight())/2, rect.getBottom() - 18, 0); 
    } 
+0

謝謝你的回答,我添加頁面作爲頁腳。現在我想在頁面中添加標題。如果你有想法,請幫助我。謝謝... –

+0

我的答案中的iText示例代碼已經顯示瞭如何設置頁眉和頁腳。它依次顯示硬編碼文檔標題('「電影歷史」')和當前章節標題。如果你沒有在你的文檔中使用'Chapter',你可能想把這個'switch'語句歸結爲一個'ColumnText.showTextAligned.' – mkl

+0

你能否爲它添加代碼,謝謝.. –

-3

您可以添加這樣的

HeaderFooter header = new HeaderFooter(new Phrase("Add Header Part Here"), false); 
HeaderFooter footer = new HeaderFooter(new Phrase("Add Footer Here"), new Phrase(".")); 

document.setHeader(header); 
document.setFooter(footer); 
1

請先參考此問題的接受答案。
這個答案是非常有用的(它幫助我)。
萬一你是編程在C#中,這裏是相同的接受的答案,但在C#版本

/// <summary> 
/// Inner class to add a header and a footer. 
/// </summary> 
internal class HeaderFooter : PdfPageEventHelper 
{ 
    private Phrase[] header = new Phrase[2]; 
    private int pageNumber; 

    public override void OnOpenDocument(PdfWriter writer, Document document) 
    { 
     header[0] = new Phrase("Smares in Header"); 
    } 

    public override void OnChapter(PdfWriter writer, Document document, float paragraphPosition, Paragraph title) 
    { 
     header[1] = new Phrase(title.Content); 
     pageNumber = 1; 
    } 

    public override void OnStartPage(PdfWriter writer, Document document) 
    { 
     pageNumber++; 
    } 

    public override void OnEndPage(PdfWriter writer, Document document) 
    { 
     Rectangle rect = writer.GetBoxSize("art"); 
     switch (writer.PageNumber % 2) 
     { 
      case 0: 
       ColumnText.ShowTextAligned(writer.DirectContent, 
         Element.ALIGN_RIGHT, header[0], 
         rect.Right, rect.Top, 0); 
       break; 
      case 1: 
       ColumnText.ShowTextAligned(writer.DirectContent, 
         Element.ALIGN_LEFT, header[1], 
         rect.Left, rect.Top, 0); 
       break; 
     } 

     ColumnText.ShowTextAligned(writer.DirectContent, 
       Element.ALIGN_CENTER, new Phrase(String.Format("page {0}", pageNumber)), 
       (rect.Left + rect.Right)/2, rect.Bottom - 18, 0); 
    } 
} 

和事件的註冊將是:

using (MemoryStream ms = new MemoryStream()) 
{ 
    using (Document doc = new Document(PageSize.A4, -30, -30, 45, 45)) 
    { 
     using (PdfWriter writer = PdfWriter.GetInstance(doc, ms)) 
     { 
      HeaderFooter ev = new HeaderFooter(); 
      writer.SetBoxSize("art", new Rectangle(36, 54, 559, 788)); 
      writer.PageEvent = ev; 

      // continue your code here 
     } 
    } 
} 

注意:這僅僅是一個將接受的答案從java轉換爲C#。 但您可以根據您的需要自定義此設置,就像我使用它一樣。

相關問題