2012-04-10 16 views
1

MVC2を使用していますが、ヘッダーとフッターはiTextSharp 4.1.6で正常に動作しましたが、5.2では正しく動作しませんでした。ここに私のコード:MVC iTextSharpヘッダーとフッターc#

public FileStreamResult GridPDF() 
      { 
       MemoryStream workStream = new MemoryStream(); 

       //the document 
       Document document = new Document(); 


       PdfWriter.GetInstance(document, workStream);//fs); 


       document.Open(); 


       iTextSharp.text.Font font5 = iTextSharp.text.FontFactory.GetFont("Arial", 10); 
       iTextSharp.text.Font font6 = iTextSharp.text.FontFactory.GetFont("Arial", 18); 
          //HeaderFooter header = new HeaderFooter(new Phrase(BPheader, FontFactory.GetFont("Arial", 8, Font.BOLD)), false); 
      //header.Border = Rectangle.BOTTOM_BORDER; 
      ////header.GrayFill=(Color.GRAY); 
      //document.Header = header; 

      //HeaderFooter footer = new HeaderFooter(new Phrase("Page: ", FontFactory.GetFont("Arial", 8, Font.ITALIC)), true); 
      //footer.Border = Rectangle.TOP_BORDER; 
      //document.Footer = footer; 
       PdfPTable tableh = new PdfPTable(1); 
       PdfPCell cellh = new PdfPCell(new Phrase("", FontFactory.GetFont("Arial", 10))); 
       cellh.Colspan = 1; 
       tableh.HorizontalAlignment = 0; 
       tableh.WidthPercentage = 100; 
       cellh.BorderWidth = 3; 
       cellh.Padding = 0; 
       Image image = Image.GetInstance(Server.MapPath("~/Content/images/logo_small.png")); 
       // image.Alignment = 6; // iTextSharp.text.Image.ALIGN_RIGHT; 
       image.ScalePercent(40f); // change it's size 
       image.SetAbsolutePosition(500, 750); 
       document.Add(image); 

       Paragraph p = new Paragraph("Certificate", font6); 
       p.Alignment = 1; 
       document.Add(p); 
       tableh.DefaultCell.Border = Rectangle.TOP_BORDER; 
       tableh.DefaultCell.Border = Rectangle.BOTTOM_BORDER; 
       tableh.AddCell(cellh); 

       //close the document 
       document.Close(); 
       //prepare output stream 
       byte[] byteInfo = workStream.ToArray(); 
       SendPdfToBrowser(byteInfo); 
       r 

eturn null; 
     } 

どのような提案!前もって感謝します。

+0

私はあなたが「それはもう機能しています」以外の具体的なものにする必要があると思います。それはコンパイルされませんか? PDFが間違って見えますか?ちょうどあなたがいくつかのより良い答えを得るのを手伝っています – Tommy

+0

いいえ、コンパイルされません。 – hncl

+0

コンパイルエラーとは何ですか? – Tommy

答えて

0

iTextSharpのHeaderFooterプロパティがバージョン5以降で削除されました。 This answerは、あなたを手伝ってくれるはずです。基本的に、ヘッダーとフッターを追加するには、PageEventsクラスを使用する必要があります。

PdfPageEventHelperから継承し、そのメンバーを実装するクラスを作成します。ヘッダーにはOnStartPage、フッターにはOnEndPageが必要です。 PDFの作成中に、iTextSharpはPDF内のすべてのページに対してこれらのメソッドを起動します。

さらに、here is a more thorough example(in C#)。

+0

ありがとうTommy、2番目のリンクは便利です。 – hncl

関連する問題