Skip to content

Commit 4eba8e3

Browse files
authored
closes #2 closes #3
1 parent a0ccb42 commit 4eba8e3

File tree

2 files changed

+122
-53
lines changed

2 files changed

+122
-53
lines changed

src/org/openlowcode/tools/pdf/PDFMultiPageTable.java

Lines changed: 103 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,28 @@
2525
*/
2626
public 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 {

src/org/openlowcode/tools/pdf/PDFPage.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,20 +1074,20 @@ public float getPageRight() {
10741074
}
10751075

10761076
/**
1077-
* @param left
1078-
* @param top
1079-
* @param right
1080-
* @param remainingheight
1081-
* @param text
1082-
* @param page
1083-
* @param texttype
1077+
* @param left left of the printing zone in mm
1078+
* @param top top of the prining zone in mm
1079+
* @param right right of the printing zone in mm
1080+
* @param remainingheight the maximum height to print
1081+
* @param text the full text to write
1082+
* @param page page to print in
1083+
* @param texttype text type as defined in PDFPage constant
10841084
* @param splitparagraph: true if this is the non-first part of
10851085
* a split paragraph. Typically, widget does not have to
10861086
* be printed
10871087
* @return
10881088
* @throws IOException
10891089
*/
1090-
static BoxTextContent writeAsMuchTextAsPossible(float left, float top, float right, float remainingheight,
1090+
public static BoxTextContent writeAsMuchTextAsPossible(float left, float top, float right, float remainingheight,
10911091
String text, PDFPage page, int texttype, boolean splitparagraph) throws IOException {
10921092
return calculateBoxAndMaybeWriteText(left, top, right, text, true, true, remainingheight, page, texttype,
10931093
splitparagraph);
@@ -1100,24 +1100,25 @@ static BoxTextContent writeAsMuchTextAsPossible(float left, float top, float rig
11001100
* @param remainingheight the maximum height to print
11011101
* @param text the full text to write
11021102
* @param page page to print in
1103+
* @param write true to write the text, false just to calculate
11031104
* @param texttype text type as defined in PDFPage constant
11041105
* @return the remaining text that could not be printed
11051106
* @throws IOException
11061107
*/
1107-
static BoxTextContent calculateBoxAndMaybeWriteText(float left, float top, float right, String text, boolean write,
1108+
public static BoxTextContent calculateBoxAndMaybeWriteText(float left, float top, float right, String text, boolean write,
11081109
PDFPage page, int texttype) throws IOException {
11091110
return calculateBoxAndMaybeWriteText(left, top, right, text, write, false, 0, page, texttype, false);
11101111
}
11111112

11121113
/**
1113-
* @param left
1114-
* @param top
1115-
* @param right
1116-
* @param text
1117-
* @param write
1118-
* @param partial
1119-
* @param maxheight
1120-
* @param page
1114+
* @param left left of the printing zone in mm
1115+
* @param top top of the prining zone in mm
1116+
* @param right right of the printing zone in mm
1117+
* @param text the full text to write
1118+
* @param write true to write the text, false just to calculate
1119+
* @param partial true to write only partial content within the maximum height limit specified
1120+
* @param maxheight max height to print partial content
1121+
* @param page page to print in
11211122
* @param texttype one of the constants prefixed by 'TEXTTYPE_' in the
11221123
* class
11231124
* @param splitparagraph : true if this is the non-first part of a section split
@@ -1126,7 +1127,7 @@ static BoxTextContent calculateBoxAndMaybeWriteText(float left, float top, float
11261127
* @return
11271128
* @throws IOException
11281129
*/
1129-
static BoxTextContent calculateBoxAndMaybeWriteText(float left, float top, float right, String text, boolean write,
1130+
public static BoxTextContent calculateBoxAndMaybeWriteText(float left, float top, float right, String text, boolean write,
11301131
boolean partial, float maxheight, PDFPage page, int texttype, boolean splitparagraph) throws IOException {
11311132
boolean currentsplitparagraph = splitparagraph;
11321133
if (write)

0 commit comments

Comments
 (0)