【问题标题】:MVC iTextSharp Header and Footer c#MVC iTextSharp 页眉和页脚 c#
【发布时间】:2012-04-10 02:13:13
【问题描述】:

我正在使用 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;
        }

任何建议!提前致谢。

【问题讨论】:

  • 我认为除了“它不再有效”之外,您还需要更加具体。它不编译吗? PDF 看起来有问题吗?只是想帮助您获得更好的答案。
  • 什么是编译错误?

标签: c# asp.net-mvc itextsharp


【解决方案1】:

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

创建一个继承自 PdfPageEventHelper 的类并实现其成员。您只需要 OnStartPage 为您的页眉和 OnEndPage 为您的页脚。在 PDF 创建期间,iTextSharp 将为 PDF 中的每一页触发这些方法中的每一个。

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

【讨论】:

  • 谢谢 Tommy,第二个链接很有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-04
  • 2011-05-07
  • 2016-04-30
  • 1970-01-01
  • 1970-01-01
  • 2020-07-23
  • 2013-09-30
相关资源
最近更新 更多