add image(s) to header & footer(在PDF文件的页眉页脚显示图片)

 language:C#

ide:VS2008

iTextSharp version:3.0

newest iTextSharp:4.1.2 download

step:

 1. create a new Class extends iTextSharp.text.pdf.PdfPageEventHelper and overwrite the method named OnStartPage

    code:

    public class PdfHandler : iTextSharp.text.pdf.PdfPageEventHelper
            {
public string headImage;

public string footerImage;              

public override void OnStartPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
                {
                    iTextSharp.text.Image imgfoot = iTextSharp.text.Image.GetInstance(footerImage);
                    iTextSharp.text.Image imghead = iTextSharp.text.Image.GetInstance(headImage);

                    imgfoot.ScaleToFit(590, 225);
                    imghead.ScaleToFit(170, 42);
                    imgfoot.SetAbsolutePosition(0, 0);
                    imghead.SetAbsolutePosition(30, 0);

                    PdfContentByte cbhead = writer.DirectContent;
                    PdfTemplate tp = cbhead.CreateTemplate(180, 42);
                    tp.AddImage(imghead);

                    PdfContentByte cbfoot = writer.DirectContent;
                    PdfTemplate tpl = cbfoot.CreateTemplate(590, 225);
                    tpl.AddImage(imgfoot);

                    cbhead.AddTemplate(tp, 0, 765);
                    cbfoot.AddTemplate(tpl, 0, 10);

                    Phrase headPhraseImg = new Phrase(cbhead + "", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 7, iTextSharp.text.Font.NORMAL));
                    Phrase footPhraseImg = new Phrase(cbfoot + "", FontFactory.GetFont(FontFactory.TIMES_ROMAN, 7, iTextSharp.text.Font.NORMAL));

                    HeaderFooter header = new HeaderFooter(headPhraseImg, true);
                    HeaderFooter footer = new HeaderFooter(footPhraseImg, true);
                   
                    base.OnStartPage(writer, document);
                }
            }

2. open the pdf document
        code:
            Document document = new Document(iTextSharp.text.PageSize.A4, 10, 10, 10, 180);
            string fileName = string.Format("{0}.pdf", Guid.NewGuid().ToString());
            string rptName = "d:/" + fileName;
            PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileStream(rptName , FileMode.Create));
            document.Open();

3. call the method: OnStartPage

    code:PdfHandler myEvents = new PdfHandler();
            writer.PageEvent = myEvents;
            myEvents.dir = dir;
            myEvents.OnStartPage(writer, document);

4. save you pdf file

    code:

     document.Close();

summarize: If you want to make you code better, you can construct you own data object and transfer it to the class which extends PdfPageEventHelper and then input it into pdf header and footer by the method of OnStartPage. You must be to add the header/footer content into document in the OnStartPage method otherwise the infomation will only show on the first pdf page! What's more we can add image to header by set document.header with a headerfooter which contains image(s). But we can add image the footer by the same way as mention just before. So I found something interesting that we can add image to the header by set document .header, at the same time we can add image to the footer by the other way I have showed seriously here.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值