2525 */
2626public class PDFMultiPageTable implements PDFPageBandSection {
2727
28- private class CellText {
28+ /**
29+ * Holds information about a single cell display
30+ *
31+ * @author <a href="https://openlowcode.com/">Open Lowcode SAS</a>
32+ *
33+ */
34+ protected class CellText {
35+
36+ /**
37+ * A cell with text not displayed as number
38+ *
39+ * @param text the text content
40+ */
2941 public CellText (String text ) {
3042 this .text = text ;
3143 this .displayasNumber = false ;
3244 }
3345
46+ /**
47+ * @param text the text to display
48+ * @param displayasNumber true to display the cell as number
49+ */
3450 public CellText (String text , boolean displayasNumber ) {
3551 this .text = text ;
3652 this .displayasNumber = displayasNumber ;
@@ -39,20 +55,49 @@ public CellText(String text, boolean displayasNumber) {
3955 private boolean displayasNumber ;
4056 private String text ;
4157
58+ /**
59+ * @return true if the text should be displayed as number
60+ */
61+ public boolean isDisplayasNumber () {
62+ return displayasNumber ;
63+ }
64+
65+ /**
66+ * @return the text of the cell
67+ */
68+ public String getText () {
69+ return text ;
70+ }
71+
4272 }
4373
4474 @ SuppressWarnings ("unused" )
4575 private static Logger logger = Logger .getLogger (PDFPageTable .class .getName ());
4676 private float leftmargin ;
4777 private float rightmargin ;
48- private boolean [] richtextcolumn ;
4978 private float [] columnrelativewidth ;
5079 private String [] headertexts ;
5180 private ArrayList <CellText []> cellscontent ;
52- private float [] columnwidthinmm ;
81+ protected float [] columnwidthinmm ;
5382 private int columnnumber ;
5483 private int indexlastlineprinted ;
5584
85+ /**
86+ * @return the number of columns in the table
87+ */
88+ public int getColumnNumber () {
89+ return columnnumber ;
90+ }
91+
92+ /**
93+ * @param row index of row, starting by 0
94+ * @param column index of column, starting by 0
95+ * @return
96+ */
97+ public CellText getCellContent (int row , int column ) {
98+ return cellscontent .get (row )[column ];
99+ }
100+
56101 /**
57102 * Creates a table of columns with similar width taking the full page width
58103 *
@@ -120,19 +165,11 @@ private void constructorcontent(float leftmargin, float rightmargin, float[] col
120165 this .columnnumber = columnrelativewidth .length ;
121166 this .cellscontent = new ArrayList <CellText []>();
122167 this .indexlastlineprinted = -1 ;
123- this .richtextcolumn = new boolean [columnrelativewidth .length ];
124- for (int i = 0 ; i < richtextcolumn .length ; i ++)
125- richtextcolumn [i ] = false ;
126- }
127168
128- public void setColumnRichText (int columnindex ) {
129- if (columnindex < 0 )
130- throw new RuntimeException ("columnindex need to be positive" );
131- if (columnindex >= this .richtextcolumn .length )
132- throw new RuntimeException ("Index out of range " + columnindex + ">=" + this .richtextcolumn .length );
133- this .richtextcolumn [columnindex ] = true ;
134169 }
135170
171+
172+
136173 /**
137174 * @param headertexts the list of header texts (should be consistent with the
138175 * column number)
@@ -201,7 +238,8 @@ public void setCellContent(String cellcontent, int columnindex) {
201238 }
202239
203240 private float getHeadersHeight (float leftinmm , float rightinmm ) throws IOException {
204- if (this .columnwidthinmm ==null ) initColumnWidthInMm (leftinmm ,rightinmm );
241+ if (this .columnwidthinmm == null )
242+ initColumnWidthInMm (leftinmm , rightinmm );
205243 float maxheight = 0 ;
206244 for (int i = 0 ; i < this .columnnumber ; i ++) {
207245 float columnwidth = this .columnwidthinmm [i ];
@@ -225,27 +263,39 @@ private void initColumnWidthInMm(float leftinmm, float rightinmm) {
225263 this .columnwidthinmm [i ] = (rightinmm - leftinmm - this .leftmargin - this .rightmargin )
226264 * columnrelativewidth [i ] / totalrelativewidth ;
227265 }
228-
266+
229267 }
230268
231- protected float getLineHeight (float leftinmm , float rightinmm , int lineindex ) throws IOException {
269+ private float getLineHeight (float leftinmm , float rightinmm , int lineindex ) throws IOException {
232270 float maxheight = 0 ;
233-
271+
234272 for (int i = 0 ; i < this .columnnumber ; i ++) {
235273 float columnwidth = this .columnwidthinmm [i ];
236274 String text = this .cellscontent .get (lineindex )[i ].text ;
237- float cellheight = 0 ;
238-
239- cellheight = PDFPage
240- .calculateBoxAndMaybeWriteText (0 , 0 , columnwidth , text , false , null , PDFPage .TEXTTYPE_PLAIN )
241- .getHeightWithoutFinalSpacing ();
242-
275+ float cellheight = getCellHeight (columnwidth , lineindex , i );
243276 if (cellheight > maxheight )
244277 maxheight = cellheight ;
245278 }
246279 return maxheight ;
247280 }
248281
282+ /**
283+ * calculates the height of the cell. This method can be overloaded by
284+ * subclasses with specific layout (rich-text...)
285+ *
286+ * @param columnwidth width of the table column
287+ * @param lineindex line index starting with 0
288+ * @param columnindex column index starting with 0
289+ * @return the height of the cell
290+ * @throws IOException
291+ */
292+ protected float getCellHeight (float columnwidth , int lineindex , int columnindex ) throws IOException {
293+ String text = this .cellscontent .get (lineindex )[columnindex ].text ;
294+ return PDFPage .calculateBoxAndMaybeWriteText (0 , 0 , columnwidth , text , false , null , PDFPage .TEXTTYPE_PLAIN )
295+ .getHeightWithoutFinalSpacing ();
296+
297+ }
298+
249299 /**
250300 * @param currentpage
251301 * @param pagenumber
@@ -282,32 +332,50 @@ private float printHeader(PDFPage currentpage, float mmfromtopforsection, float
282332 * @return the new top for printing the next element
283333 * @throws IOException
284334 */
285- protected float printOneRow (PDFPage currentpage , int rowindex , float mmfromtopforsection , float leftinmm ,
335+ private float printOneRow (PDFPage currentpage , int rowindex , float mmfromtopforsection , float leftinmm ,
286336 float rightinmm ) throws IOException {
287-
337+
288338 float rowheight = this .getLineHeight (leftinmm , rightinmm , rowindex );
289339 float currentleft = leftinmm ;
290340 for (int i = 0 ; i < columnwidthinmm .length ; i ++) {
291341 float currentcolumnwidth = columnwidthinmm [i ];
292-
293342 currentpage .drawBoxWithWidthAndHeight (false , currentleft , mmfromtopforsection , currentcolumnwidth ,
294343 rowheight );
295- CellText currenttextcell = this .cellscontent .get (rowindex )[i ];
296- if (!currenttextcell .displayasNumber ) {
297- currentpage .drawTextInBox (currentleft , mmfromtopforsection , currentleft + currentcolumnwidth ,
298- this .cellscontent .get (rowindex )[i ].text , PDFPage .TEXTTYPE_PLAIN );
299- } else {
300- currentpage .drawFixWidthRightAlignedTextAt (currentleft + currentcolumnwidth , mmfromtopforsection , 0 , 0 ,
301- currenttextcell .text );
302- }
303-
344+ printOneCell (currentpage , rowindex , i , mmfromtopforsection , currentleft , currentcolumnwidth );
304345 currentleft += currentcolumnwidth ;
305346
306347 }
307348 // add spacing if last column
308349 return (mmfromtopforsection + rowheight );
309350 }
310351
352+ /**
353+ * This method prints the content of one row of a table. It can be overriden by
354+ * subclasses wishing to add their own formatting
355+ *
356+ * @param currentpage page to print in
357+ * @param rowindex index of the row (starting with 0)
358+ * @param columnindex index of the column (starting with 0)
359+ * @param mmfromtopforsection vertical position for printing as per pageband
360+ * mechanism
361+ * @param currentleft left of the cell
362+ * @param currentcolumnwidth with of the cell
363+ * @throws IOException if any exception is raised regarding printing of the
364+ * content
365+ */
366+ protected void printOneCell (PDFPage currentpage , int rowindex , int columnindex , float mmfromtopforsection ,
367+ float currentleft , float currentcolumnwidth ) throws IOException {
368+ CellText currenttextcell = this .cellscontent .get (rowindex )[columnindex ];
369+
370+ if (!currenttextcell .displayasNumber ) {
371+ currentpage .drawTextInBox (currentleft , mmfromtopforsection , currentleft + currentcolumnwidth ,
372+ currenttextcell .text , PDFPage .TEXTTYPE_PLAIN );
373+ } else {
374+ currentpage .drawFixWidthRightAlignedTextAt (currentleft + currentcolumnwidth , mmfromtopforsection , 0 , 0 ,
375+ currenttextcell .text );
376+ }
377+ }
378+
311379 @ Override
312380 public void print (PDFPageBand pageband , PDFPage currentpage , float mmfromtopforsection , float leftinmm ,
313381 float rightinmm ) throws IOException {
0 commit comments