itext - How can you eliminate white-space in multiple columns using iTextSharp? -


i'd add paragraph of text pages in 2 columns. understand multicolumntext has been eliminated. know can create column 1, write it, , if there more text create column 2 , write it. if there still more text, go next page , repeat.

however end either:

  1. a long chunk of text orphaned in left column.
  2. a full left column , partially used right column.

how can format content in 2 columns while reducing white space, such compressing columns end 2 full columns of equal length?

thanks!

you should add column twice, once in simulation mode , once real.

i have adapted columntextparagraphs example show meant simulation mode. take @ columntextparagraphs2 example:

enter image description here

we add column in simulation mode obtain total height needed column. done in following method:

public float getnecessaryheight(columntext ct) throws documentexception {     ct.setsimplecolumn(new rectangle(0, 0, column_width, -500000));     ct.go(true);     return -ct.getyline(); } 

we use height when add left , right column:

rectangle left; float top = columns[0].gettop(); float middle = (columns[0].getleft() + columns[1].getright()) / 2; float columnheight; int status = columntext.start_column; while (columntext.hasmoretext(status)) {     if (checkheight(height)) {         columnheight = columns[0].getheight();         left = columns[0];     }     else {         columnheight = (height / 2) + error_margin;         left = new rectangle(             columns[0].getleft(),             columns[0].gettop() - columnheight,             columns[0].getright(),             columns[0].gettop()         );     }     // left half     ct.setsimplecolumn(left);     ct.go();     height -= columns[0].gettop() - ct.getyline();     // separator     canvas.moveto(middle, top - columnheight);     canvas.lineto(middle, top);     canvas.stroke();     // right half     ct.setsimplecolumn(columns[1]);     status = ct.go();     height -= columns[1].gettop() - ct.getyline();     // new page     document.newpage(); } 

this how check height:

public boolean checkheight(float height) {     height -= columns[0].getheight() + columns[1].getheight() + error_margin;     return height > 0; } 

as can see, add full columns long height of both columns smaller remaining height. when columns added, adjust remaining height. height lower height of columns, adapt height of first column.

note work error_margin because dividing 2 leads situation second column has 1 line more first column. better when it's other way around.

this full example @ request:

/**  * example written bruno lowagie in answer to:  * http://stackoverflow.com/questions/29378407/how-can-you-eliminate-white-space-in-multiple-columns-using-itextsharp  */ package sandbox.objects;  import java.io.file; import java.io.fileoutputstream; import java.io.ioexception;  import com.itextpdf.text.document; import com.itextpdf.text.documentexception; import com.itextpdf.text.paragraph; import com.itextpdf.text.rectangle; import com.itextpdf.text.pdf.columntext; import com.itextpdf.text.pdf.pdfcontentbyte; import com.itextpdf.text.pdf.pdfwriter;  public class columntextparagraphs2 {      public static final string dest = "results/objects/column_paragraphs2.pdf";      public static final string text = "this long paragraph added on , on again prove point.";     public static final float column_width = 254;     public static final float error_margin = 16;     public static final rectangle[] columns = {         new rectangle(36, 36, 36 + column_width, 806),         new rectangle(305, 36, 305 + column_width, 806)     };      public static void main(string[] args) throws ioexception, documentexception {         file file = new file(dest);         file.getparentfile().mkdirs();         new columntextparagraphs2().createpdf(dest);     }      public void createpdf(string dest) throws ioexception, documentexception {         // step 1         document document = new document();         // step 2         pdfwriter writer = pdfwriter.getinstance(document, new fileoutputstream(dest));         // step 3         document.open();         // step 4         pdfcontentbyte canvas = writer.getdirectcontent();         columntext ct = new columntext(canvas);         addcontent(ct);         float height = getnecessaryheight(ct);         addcontent(ct);         rectangle left;         float top = columns[0].gettop();         float middle = (columns[0].getleft() + columns[1].getright()) / 2;         float columnheight;         int status = columntext.start_column;         while (columntext.hasmoretext(status)) {             if (checkheight(height)) {                 columnheight = columns[0].getheight();                 left = columns[0];             }             else {                 columnheight = (height / 2) + error_margin;                 left = new rectangle(                     columns[0].getleft(),                     columns[0].gettop() - columnheight,                     columns[0].getright(),                     columns[0].gettop()                 );            }             // left half             ct.setsimplecolumn(left);             ct.go();             height -= columns[0].gettop() - ct.getyline();             // separator             canvas.moveto(middle, top - columnheight);             canvas.lineto(middle, top);             canvas.stroke();             // right half             ct.setsimplecolumn(columns[1]);             status = ct.go();             height -= columns[1].gettop() - ct.getyline();             // new page             document.newpage();         }         // step 5         document.close();     }      public void addcontent(columntext ct) {         (int = 0; < 35; i++) {             ct.addelement(new paragraph(string.format("paragraph %s: %s", i, text)));         }     }      public float getnecessaryheight(columntext ct) throws documentexception {         ct.setsimplecolumn(new rectangle(0, 0, column_width, -500000));         ct.go(true);         return -ct.getyline();     }      public boolean checkheight(float height) {         height -= columns[0].getheight() + columns[1].getheight() + error_margin;         return height > 0;     } } 

Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -