您的位置:首页 > 移动开发 > Objective-C

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

2009-04-14 21:56 741 查看
 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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐