C# MVC iTextSharp页眉和页脚c

C# MVC iTextSharp页眉和页脚c,c#,asp.net-mvc,itextsharp,C#,Asp.net Mvc,Itextsharp,我使用的是MVC2,页眉和页脚在iTextSharp 4.1.6中运行良好,但在5.2中没有。这是我的密码: public FileStreamResult GridPDF() { MemoryStream workStream = new MemoryStream(); //the document Document document = new Document(

我使用的是MVC2,页眉和页脚在iTextSharp 4.1.6中运行良好,但在5.2中没有。这是我的密码:

    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;
        }

任何建议!!提前感谢。

我想我知道您的问题,iTextSharp中的HeaderFooter属性在版本5+中被删除。应该能帮你上路。基本上,您需要使用PageEvents类来添加页眉和页脚

创建从PdfPageEventHelper继承的类并实现其成员。您只需要在开始页上显示页眉,在结束页上显示页脚。在PDF创建过程中,iTextSharp将为PDF中的每个页面触发这些方法


此外,在C.

中,我认为您需要更具体,而不是“它不再工作”。它不编译吗?PDF看起来有问题吗?只是想帮你得到一些更好的答案。编译错误是什么?谢谢Tommy,第二个链接很有用。