c# - Adding Header and Footer Images to PDF: Header image doesn't show, Footer is scaled up -
i attempting create simple .pdf using libary itextsharp. making .pdf header & footer have images in them , header margin 300px & footer margin 664px.
my problem: code doesn't insert header image reason , footer image blown up/scaled in size reason.
can tell me whats wrong code. header image should extend whole width of a4 page & 300px in height. footer image should extend whole width of page & 664px in height. both images dont need resized whole width of page & correct heights.
public class itseventshandler : pdfpageeventhelper { pdftemplate total; basefont helv; // following tutorial & said if want create headers/footers when each page created // should override onendpage() not onstartpage() correct? public override void onendpage(pdfwriter writer, document document) { // post: when each new page created, add header & footer image page. , set top margin 370px // , bottom margin 664px. // result: function executes pdf's header image isn't visible & footer looks resized(scaled 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 in pixels i'm not sure if thats 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); } } // code create pdf // create 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 document writing document.open(); // add paragrahs document.close();
when adding templates check location. example:
cbhead.addtemplate(tp, 0, 715); cbfoot.addtemplate(tpl, 0, 0);
hope helps!
Comments
Post a Comment