c# – 将页眉和页脚图像添加到PDF:页眉图像不显示,页脚按比例放大
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 将页眉和页脚图像添加到PDF:页眉图像不显示,页脚按比例放大,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2678字,纯文字阅读大概需要4分钟。
内容图文
![c# – 将页眉和页脚图像添加到PDF:页眉图像不显示,页脚按比例放大](/upload/InfoBanner/zyjiaocheng/791/53400e035a4546b6a4ff724f48ba9e52.jpg)
我试图使用libary ITextSharp创建一个简单的.pdf.我正在制作一个.pdf标题&页脚中有图像,页眉边距为300px&页脚边距为664px.
我的问题:我的代码由于某种原因没有插入标题图像,并且由于某种原因,页脚图像被放大/放大.
你能告诉我我的代码有什么问题吗?标题图像应该扩展A4页面的整个宽度.高度为300px.页脚图像应该扩展页面的整个宽度&高度为664px.两张图片都不需要调整大小,它们已经是页面的整个宽度.正确的高度.
public class itsEventsHandler : PdfPageEventHelper
{
PdfTemplate total;
BaseFont helv;
// I am following a tutorial & they said that if I want to create headers/footers when each page is created
// that I should override the OnEndPage() not the OnStartPage() is that correct?
public override void OnEndPage(PdfWriter writer, Document document)
{
// Post: When each new page is created, add a header & footer image to the page. And set the top margin to 370px
// and the bottom margin to 664px.
// Result: The function executes but the pdf's header image isn't visible & the footer looks resized(scaled up in size).
//Footer Image
iTextSharp.text.Image imgfoot = iTextSharp.text.Image.GetInstance(resolvePath("~/images/pdf/bottomBorder.jpg"));
//Header Image
iTextSharp.text.Image imghead = iTextSharp.text.Image.GetInstance(resolvePath("~/images/pdf/topBorder.jpg"));
imgfoot.SetAbsolutePosition(0, 0);
imghead.SetAbsolutePosition(0, 0);
PdfContentByte cbhead = writer.DirectContent;
PdfTemplate tp = cbhead.CreateTemplate(2480, 370); // units are in pixels but I'm not sure if thats the correct units
tp.AddImage(imghead);
PdfContentByte cbfoot = writer.DirectContent;
PdfTemplate tpl = cbfoot.CreateTemplate(2480, 664);
tpl.AddImage(imgfoot);
cbhead.AddTemplate(tp, 0, 0);
cbfoot.AddTemplate(tpl, 0, 0);
helv = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
/*PdfContentByte cb = writer.DirectContent;
cbfoot.SaveState();
document.SetMargins(35, 35, 100, 82);
cb.RestoreState();*/
//document.NewPage();
base.OnStartPage(writer, document);
}
public override void OnOpenDocument(PdfWriter writer, Document document)
{
total = writer.DirectContent.CreateTemplate(100, 100);
total.BoundingBox = new iTextSharp.text.Rectangle(-20, -20, 100, 100);
helv = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
}
}
// My code to create the pdf
// Create a Document object
var document = new Document(PageSize.A4, 50, 50, 370, 664);
var output = new MemoryStream();
var writer = PdfWriter.GetInstance(document, output);
writer.PageEvent = new itsEventsHandler();
// Open the Document for writing
document.Open();
// add some paragrahs
document.Close();
解决方法:
添加模板时,请检查其位置.例如:
cbhead.AddTemplate(tp, 0, 715);
cbfoot.AddTemplate(tpl, 0, 0);
希望这可以帮助!
内容总结
以上是互联网集市为您收集整理的c# – 将页眉和页脚图像添加到PDF:页眉图像不显示,页脚按比例放大全部内容,希望文章能够帮你解决c# – 将页眉和页脚图像添加到PDF:页眉图像不显示,页脚按比例放大所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。