2012-04-10 92 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 2012-04-10 02:15:56

+0

不,它不會編譯。 – hncl 2012-04-10 02:17:02

+0

什麼是彙編錯誤? – Tommy 2012-04-10 02:17:36

回答

0

我想我知道你的問題,iTextSharp中的HeaderFooter屬性在版本5+中被刪除。 This answer應該可以幫助你順利完成任務。基本上,你將需要使用PageEvents類來添加頁眉和頁腳。

創建一個繼承自PdfPageEventHelper並實現其成員的類。你只需要OnStartPage用於頁眉,而OnEndPage用於頁腳。在PDF創建過程中,iTextSharp將針對PDF中的每個頁面啓動每種方法。

此外,here is a more thorough example(在C#中)。

+0

謝謝湯米,第二個鏈接很有用。 – hncl 2012-04-10 03:04:07