1

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

助言がありますか!!前もって感謝します。

4

1 に答える 1

0

私はあなたの問題を知っていると思います。iTextSharpのHeaderFooterプロパティはバージョン5以降で削除されました。 この答えはあなたをあなたの道に導くのに役立つはずです。基本的に、ヘッダーとフッターを追加するには、PageEventsクラスを使用する必要があります。

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

さらに、これはより完全な例です(C#で)。

于 2012-04-10T02:19:47.887 に答える