c# - itext7 not creating new pdf-page automatically -
when add multiple tables itext7 pdf , number of tables more fits in 1 page following exception gets thrown.
exception: "object reference not set instance of object." source: "itext.layout" stacktrace: "at itext.layout.renderer.tablerenderer.layout(layoutcontext layoutcontext) @ itext.layout.renderer.rootrenderer.addchild(irenderer renderer) @ itext.layout.rootelement
1.add[t2](blockelement
1 element) @ itext.layout.document.add[t](blockelement`1 element) @ itextsharp7_test.controllers.pdfcontroller.replicatebug(string pdffile) in c:\users\me\documents\visual studio 2015\projects\app_test\itextsharp7_test\controllers\pdfcontroller.cs:line 443"
in provided example exception gets thrown when 6th table added.
the way have been able handle issue assuming exception thrown adding more data pdf fits in 1 page , add new page within try catch block, solution ugly , not in long term.
example:
public void replicatebug(string pdffile) { itext.kernel.pdf.pdfwriter writer = null; itext.kernel.pdf.pdfdocument pdf = null; itext.layout.document document = null; try { writer = new itext.kernel.pdf.pdfwriter(pdffile); pdf = new itext.kernel.pdf.pdfdocument(writer); document = new itext.layout.document(pdf, itext.kernel.geom.pagesize.a4); document.add(new itext.layout.element.paragraph("*** products ***")); itext.kernel.pdf.canvas.draw.dashedline dashedline = new itext.kernel.pdf.canvas.draw.dashedline(); document.add(new itext.layout.element.lineseparator(dashedline)); itext.layout.element.table table = null; (int = 0; < 10; ++i) { itext.layout.element.cell cell = null; table = new itext.layout.element.table(2); table.setmargintop(10); cell = new itext.layout.element.cell().add("-- product --"); table.addcell(cell); cell = new itext.layout.element.cell(3, 1).add("image"); cell.sethorizontalalignment(itext.layout.properties.horizontalalignment.center); cell.setverticalalignment(itext.layout.properties.verticalalignment.middle); table.addcell(cell); cell = new itext.layout.element.cell().add("product text." + environment.newline + "welcome"); cell.setheight(75); table.addcell(cell); cell = new itext.layout.element.cell().add((i + 1).tostring().padleft(10, '0')); table.addcell(cell); try { document.add(table); document.add(new itext.layout.element.lineseparator(dashedline)); // default linewidth 1 } catch { // note: after adding 5 tables pdf exception thrown of following kind. // exception: "object reference not set instance of object." // source: "itext.layout" // stacktrace: "at itext.layout.renderer.tablerenderer.layout(layoutcontext layoutcontext) // @ itext.layout.renderer.rootrenderer.addchild(irenderer renderer) // @ itext.layout.rootelement`1.add[t2](blockelement`1 element) // @ itext.layout.document.add[t](blockelement`1 element) // @ itextsharp7_test.controllers.pdfcontroller.replicatebug(string pdffile) in c:\\users\\me\\documents\\visual studio 2015\\projects\\app_test\\itextsharp7_test\\controllers\\pdfcontroller.cs:line 443" } } document.close(); pdf.close(); writer.close(); } catch { if (document != null) { document.close(); } if (pdf != null) { pdf.close(); } if (writer != null) { writer.close(); } throw; } }
thanks in advance
this issues has been fixed in 7.0.2
(and 7.0.2-snapshot
, known 7.0.1.1
in .net
version). snapshot nuget package can downloaded artifactory. 7.0.2
release version available on nuget in nearest weeks.
Comments
Post a Comment