iTextSharp - Don't display page numbers if only one page -
i have pdf generated using itextsharp displays "page 1/4" etc. thing in footer. works fine , implemented using pdftemplate
set in onendpage()
method total number of pages added in onclosedocument()
method.
what remove if there 1 page in document. i've tried in onclosedocument()
method, doesn't remove template:
public override void onclosedocument(pdfwriter writer, document document) { base.onclosedocument(writer, document); if (writer.pagenumber >= 3) { template.begintext(); template.setfontandsize(f_cn, cocservice.footerfont.size); template.settextmatrix(0, 0); template.showtext("" + (writer.pagenumber - 1)); template.endtext(); } else { template.reset(); } }
just reference, here's relevant code onendpage()
method:
public override void onendpage(pdfwriter writer, document document) { base.onendpage(writer, document); int pagen = writer.pagenumber; string text = "page " + pagen.tostring() + "/"; float len = f_cn.getwidthpoint(text, cocservice.footerfont.size); itextsharp.text.rectangle pagesize = document.pagesize; cb.setrgbcolorfill(100, 100, 100); cb.begintext(); cb.setfontandsize(f_cn, cocservice.footerfont.size); cb.settextmatrix(document.leftmargin+520, pagesize.getbottom(document.bottommargin)+33); cb.showtext(text); cb.endtext(); cb.addtemplate(template, document.leftmargin + 520 + len, pagesize.getbottom(document.bottommargin)+33); }
you writing text content stream in onendpage()
method, , there's no way remove after fact because stream may have been sent outputstream
.
the best way avoid having page numbers on document if there's 1 page, add page numbers in second go. take @ twopasses example, says "second pass". imo, elegant way solve problem. ask reader
number of pages. if it's 1, nothing. if it's more, add page x of y.
by way: c# port of examples, go http://tinyurl.com/itextsharpiia2c06
Comments
Post a Comment