2011-07-14 7 views
1

私は自分のPDFページのページ番号を表示しようとしていますが、最初のページと最後のページに表示したくありません。iTextSharp PageNumberただし最初と最後のページにはありません

私はこのコードを使用します。

public class PDFPage : iTextSharp.text.pdf.PdfPageEventHelper 
{ 
    //I create a font object to use within my footer 
    protected iTextSharp.text.Font footer 
    { 
     get 
     { 
      // create a basecolor to use for the footer font, if needed. 
      iTextSharp.text.Color grey = new iTextSharp.text.Color(40, 40, 40); 
      Font font = FontFactory.GetFont("Arial", 16, iTextSharp.text.Font.BOLD, grey); 
      return font; 
     } 
    } 

    //override the OnPageEnd event handler to add our footer 
    public override void OnEndPage(PdfWriter writer, Document doc) 
    { 
     if (doc.PageNumber > 1) 
     { 
      //I use a PdfPtable with 2 columns to position my footer where I want it 
      PdfPTable footerTbl = new PdfPTable(2); 

      //set the width of the table to be the same as the document 
      footerTbl.TotalWidth = doc.PageSize.Width; 

      //Center the table on the page 
      footerTbl.HorizontalAlignment = Element.ALIGN_CENTER; 

      //Create a paragraph that contains the footer text 
      Paragraph para = new Paragraph(" ", footer); 

      //add a carriage return 
      para.Add(Environment.NewLine); 
      para.Add(" "); 

      //create a cell instance to hold the text 
      PdfPCell cell = new PdfPCell(para); 

      //set cell border to 0 
      cell.Border = 0; 

      //add some padding to bring away from the edge 
      cell.PaddingLeft = 10; 

      //add cell to table 
      footerTbl.AddCell(cell); 

      //create new instance of Paragraph for 2nd cell text 
      para = new Paragraph(" " + doc.PageNumber, footer); 

      //create new instance of cell to hold the text 
      cell = new PdfPCell(para); 

      //align the text to the right of the cell 
      cell.HorizontalAlignment = Element.ALIGN_LEFT; 
      //set border to 0 
      cell.Border = 0; 

      // add some padding to take away from the edge of the page 
      cell.PaddingRight = 10; 

      //add the cell to the table 
      footerTbl.AddCell(cell); 

      //write the rows out to the PDF output stream. 
      footerTbl.WriteSelectedRows(0, -1, 0, (doc.BottomMargin + 25), writer.DirectContent); 
     } 
    } 

} 

ありがとうございました!

+1

実際に質問されていません。このコードの使い方がわからないことはあなたの質問ですか?それとも、このコードはあなたのために働いていないのですか?詳細を教えてください。 –

+0

こんにちはクリス、コードは魅力的なように働いていますが、私の問題は自分のPDFの最後のページにページ番号を隠す方法がわからないことです。 –

答えて

2

ライター。ページ番号-1)が最後のページです。それもチェックしてください。

+0

ああ素晴らしいですね。私はそれを試み、結果を報告するつもりです。ありがとうございました! –

0

public class PageEventHelper : PdfPageEventHelper 
 
    { 
 
     PdfContentByte cb; 
 
     PdfTemplate template; 
 
     private int TotalNumber = 0; 
 

 
     
 
     //for remeber lastpage 
 
     public void SetNumber(int num) 
 
     { 
 
      TotalNumber = num; 
 
     } 
 

 
     public override void OnOpenDocument(PdfWriter writer, Document document) 
 
     { 
 
      cb = writer.DirectContent; 
 
      template = cb.CreateTemplate(50, 50); 
 
     } 
 

 
     public override void OnEndPage(PdfWriter writer, Document document) 
 
     { 
 
      base.OnEndPage(writer, document); 
 
      cb = writer.DirectContent; 
 
      template = cb.CreateTemplate(50, 50); 
 

 
      BaseFont font = BaseFont.CreateFont(); 
 
      int pageN = writer.PageNumber; 
 
      String text = pageN.ToString(); 
 
      float len = font.GetWidthPoint(text, 9); 
 
      
 
      iTextSharp.text.Rectangle pageSize = document.PageSize; 
 

 
      // cb.SetRGBColorFill(100, 100, 100); 
 
      
 
      ; 
 
      cb.BeginText(); 
 
      cb.SetFontAndSize(font, 9); 
 
      cb.SetTextMatrix(document.LeftMargin, pageSize.GetBottom(document.BottomMargin)); 
 
      //cb.ShowText(text); 
 
      
 
      if (pageN > 1 && TotalNumber==0) 
 
      { 
 
       cb.ShowTextAligned(Element.ALIGN_CENTER, (pageN - 6).ToString() , 300f, 10f, 0); 
 
      } 
 

 
      cb.EndText(); 
 

 
      cb.AddTemplate(template, document.LeftMargin + len, pageSize.GetBottom(document.BottomMargin)); 
 
     } 
 
}

そして、どのようにコードを使用しますか?

doc.Open(); 
 
      //PdfWriter writer = PdfWriter.GetInstance(doc, stream); 
 
      PageEventHelper pageEventHelper = new PageEventHelper(); 
 
      writer.PageEvent = pageEventHelper;

とdoc.closedの前に() は、関数SetNumber()あなたが他の番号の代わりにライターを使用することができます

pageEventHelper.SetNumber(writer.PageNumber); 
 
      doc.Close();

を使用してください。 PageNumberではなく0です。

希望があなたを助けよう!

関連する問題