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.
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.
相关文章推荐
- header("Content-type: image/png"); 显示图片出错
- ImageView显示视频的第一帧图片&&VideoView视频播放
- 黄聪: PHP WkHtmlToPdf/WkHtmlToImage 将网页直接转换成pdf和图片
- iText7 生成 PDF 文件,解决各种操作系统中文问题,控制指定位置显示图片、文字、图文混排
- [Android]异步加载图片,内存缓存,文件缓存,imageview显示图片时增加淡入淡出动画
- Python+ImageMagick实现多图片合并为一个pdf文件
- 同一个ImageView显示不同的图片--->level-list
- Control Study->AdRotator广告控件显示自定义配置文件中广告以及与DataGrid控件结合使用显示图片(示例代码下载)
- Android ListView自定义分割线 header 和footer设置没有页眉和页脚
- iTextSharp add Image to Old PDF
- OpenOffice将MS docx转换成pdf文件偶数页眉不显示问题解决办法
- android:scaleType="matrix"布局文件加载图片时候的显示方式
- 学习使用资源文件[3] - 用 Image 显示资源中的图片
- 同一个ImageView根据xml文件来显示不同的图片--level-list
- <link rel="icon" type="image/vnd.microsoft.icon" href="浏览器地址栏里显示的图片.ico"/>
- android:scaleType="matrix"布局文件载入图片时候的显示方式
- 打印Excel文件时如何不显示页眉和页脚
- R图片保存到PDF文件中顺利显示中文的方法
- pdf文件中怎样添加页眉页脚
- Add header and footer to some file