2013-05-13 43 views
2

我有一個使用iTextSharp生成的PDF,在頁腳中顯示「Page 1/4」等東西。這一切都可以正常工作,並且使用方法中設置的PdfTemplate執行,並使用​​方法中添加的頁面總數。iTextSharp - 如果只有一頁不顯示頁碼

我想要做的是刪除這個,如果只有一個頁面在文檔中。我在我的​​方法試過,但它實際上並沒有刪除模板:

 public override void OnCloseDocument(PdfWriter writer, Document document) 
     { 
      base.OnCloseDocument(writer, document); 

      if (writer.PageNumber >= 3) 
      { 
       template.BeginText(); 
       template.SetFontAndSize(f_cn, CocService.footerFont.Size); 
       template.SetTextMatrix(0, 0); 
       template.ShowText("" + (writer.PageNumber - 1)); 
       template.EndText(); 
      } 
      else 
      { 
       template.Reset(); 
      } 
     } 

僅供參考,這裏是從OnEndPage()方法的相關代碼:

 public override void OnEndPage(PdfWriter writer, Document document) 
     { 
      base.OnEndPage(writer, document); 

      int pageN = writer.PageNumber; 
      String text = "Page " + pageN.ToString() + "/"; 

      float len = f_cn.GetWidthPoint(text, CocService.footerFont.Size); 
      iTextSharp.text.Rectangle pageSize = document.PageSize; 
      cb.SetRGBColorFill(100, 100, 100); 
      cb.BeginText(); 
      cb.SetFontAndSize(f_cn, CocService.footerFont.Size); 
      cb.SetTextMatrix(document.LeftMargin+520, pageSize.GetBottom(document.BottomMargin)+33); 
      cb.ShowText(text); 
      cb.EndText(); 
      cb.AddTemplate(template, document.LeftMargin + 520 + len, pageSize.GetBottom(document.BottomMargin)+33); 
     } 

回答

1

您已經在onEndPage()方法中向內容流寫入文本,並且無法在事實之後刪除,因爲該流可能已經發送到OutputStream

如果只有一個頁面,避免在文檔中出現頁碼的最佳方法是在第二頁中添加頁碼。看一看TwoPasses的例子,特別是在說「SECOND PASS」的地方。國際海事組織,這是解決你的問題最優雅的方式。請詢問reader的頁數。如果它是1,則什麼也不做。如果更多,請添加Y的第X頁。

順便說一句:對的例子C#端口,去http://tinyurl.com/itextsharpIIA2C06

+0

這是一個很好的答案,雖然不是我最終與去(看我自己的答案下面)。優雅一如既往,是一個意見問題。 – 2013-05-15 01:03:42

+0

錯誤404:C#示例未找到 – fubo 2014-07-31 13:48:32

+0

我將更新URL:http://tinyurl.com/itextsharpIIA2C06 – 2014-07-31 15:55:29

3

我結束了在增加一個獨立的PdfTemplate對象的每一頁,然後將所有的頁號(或者讓他們去解決方案總共)在​​方法。我認爲這也很優雅。這裏的基本代碼:

// Inner class for dealing with the page numbering 
    class CocPdfEventHelper : PdfPageEventHelper 
    { 
     List<PdfTemplate> pageNumberTemplates = new List<PdfTemplate>(); 

     public override void OnEndPage(PdfWriter writer, Document document) 
     { 
      base.OnEndPage(writer, document); 

      // Add a unique (empty) template for each page here 
      PdfTemplate t = writer.DirectContent.CreateTemplate(180, 50); 
      pageNumberTemplates.Add(t); 
      writer.DirectContent.AddTemplate(t, document.LeftMargin + 520, document.PageSize.GetBottom(document.BottomMargin) + 33); 
     } 

     public override void OnCloseDocument(PdfWriter writer, Document document) 
     { 
      base.OnCloseDocument(writer, document); 

      // Only bother if there is more than 1 page 
      if (writer.PageNumber >= 3) 
      { 
       int count = 1; 
       foreach (PdfTemplate template in pageNumberTemplates) 
       { 
        template.BeginText(); 
        template.SetFontAndSize(f_cn, CocService.footerFont.Size); 
        template.SetRGBColorFill(100, 100, 100); 
        template.SetTextMatrix(0,0); 
        template.ShowText("Page " + count + " of " + (writer.PageNumber - 1)); 
        template.EndText(); 
        count++; 
       } 
      } 
     } 
    }