java - iText - avoid last row not to cut tables on page split to next page -


i working on itext 5 using java. have pages mutiple tables dynamic rows. in instances, table last row splitted next page folowing header. using setheaderrows() , setskipfirstheader() manage continuation of next page. last row has enough space fit on earlier page. fit last row in same page instead of next page.

for example, on page 1, last row splitted first row of next page. instead fit row in page 1 save 1 page blanks.

i tried using setextendlastrow(), not working. know how fix problem. attaching working sample code.

public class proposalitextsplitlastrow {  public static void main(string[] args) {     try {         document document = new document();         document.setpagesize(pagesize.letter);         document.setmargins(16, 14, 14, 14);         pdfwriter writer = pdfwriter.getinstance(document, new fileoutputstream("c:/splitlastrow.pdf"));         document.open();         document.setpagesize(pagesize.letter);         document.setmargins(16, 14, 42, 38);          (int m = 1; m < 20; m++) {              int row = 0;             pdfptable table = new pdfptable(1);             table.setspacingafter(0);             table.setspacingbefore(0);             table.setwidthpercentage(100);              table.setheaderrows(1);             table.setskipfirstheader(true);             add(table, "header row continued " + m, basecolor.light_gray, row++);             add(table, "header row normal " + m, basecolor.light_gray, row++);              add(table, "text row 1 ", basecolor.white, row++);             add(table, "text row 2 ", basecolor.white, row++);             add(table, "text row 3 ", basecolor.white, row++);              addpadding(table);              document.add(table);         }          document.close();     } catch (exception de) {         de.printstacktrace();     } }  private static void add(pdfptable table, string text, basecolor color, int row) {     pdfpcell pdfcellheader = new pdfpcell();     pdfcellheader.setbackgroundcolor(color);     pdfcellheader.addelement(new paragraph(new phrase(text)));     table.addcell(pdfcellheader); }  private static void addpadding(pdfptable table) {     pdfpcell cell = new pdfpcell();     cell.setfixedheight(2f);     cell.setborder(rectangle.no_border);     cell.setcolspan(table.getnumberofcolumns());     table.addcell(cell); } } 

i had execute example understand question. confused me talking header isn't header (the rows "header row normal" aren't header rows!) , reference setextendlastrow() didn't either (mentioning method doesn't make sense me; it's confusing).

this being said, solution problem no-brainer. i've rewritten main class:

public static void main(string[] args) {     try {         document document = new document();         document.setpagesize(pagesize.letter);         document.setmargins(16, 14, 14, 14);         pdfwriter writer = pdfwriter.getinstance(document,                 new fileoutputstream("splitlastrow.pdf"));         document.open();         document.setpagesize(pagesize.letter);         document.setmargins(16, 14, 42, 38);          (int m = 1; m < 20; m++) {              int row = 0;             pdfptable table = new pdfptable(1);             table.setspacingafter(0);             table.setspacingbefore(0);             table.settotalwidth(document.right() - document.left());             table.setlockedwidth(true);              table.setheaderrows(1);             table.setskipfirstheader(true);             add(table, "header row continued " + m, basecolor.light_gray, row++);             add(table, "header row normal " + m, basecolor.light_gray, row++);              add(table, "text row 1 ", basecolor.white, row++);             add(table, "text row 2 ", basecolor.white, row++);             add(table, "text row 3 ", basecolor.white, row++);              addpadding(table);             if (writer.getverticalposition(true) - table.getrowheight(0) - table.getrowheight(1) < document.bottom()) {                 document.newpage();             }             document.add(table);         }          document.close();     } catch (exception de) {         de.printstacktrace();     } } 

make sure define total width instead of width percentage, , lock width. documented (and common sense tells you), pdfptable object doesn't know actual width if define width percentage. goes without saying can't calculate height of table doesn't know it's actual width.

then use getverticalposition() method current position of cursor, , check if first 2 rows fit on page. if don't go new page before adding table. if want check if complete table fits, use gettotalheight() method instead of getrowheight() method.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -