added cellPadding support for tables

This commit is contained in:
jomu
2016-06-07 10:50:21 +00:00
parent e1c7f37037
commit 90ef289d50
3 changed files with 58 additions and 20 deletions

View File

@ -15,7 +15,7 @@ import org.slf4j.LoggerFactory;
* @author jomu
*/
public class TableContent extends Content {
private static final Logger LOGGER = LoggerFactory.getLogger(TableContent.class);
@Expose
@ -78,7 +78,6 @@ public class TableContent extends Content {
@Override
protected Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException {
float margin = 0F;
cos.beginText();
PDFont hFont = document.getFont(headerFont.getFontName());
@ -94,10 +93,22 @@ public class TableContent extends Content {
cos.setFont(hFont, headerFont.getFontSize());
cos.newLineAtOffset(x, y);
for (int i = 0; i < header.size(); i++) {
cos.showText(header.getHeader(i).getText());
float textWdith = (standardFont.getStringWidth(header.getHeader(i).getText()) / 1000F) * document.getStandardFont().getFontSize();
LOGGER.info ("Text width for {} = {}", header.getHeader(i).getText(), textWdith);
cos.newLineAtOffset(header.getColumnSize(i), 0);
Text currentHeader = header.getHeader(i);
float cellPadding = header.getCellPadding(i);
float startX;
if (currentHeader.getAlign() == TextAlignment.RIGHT) {
float textWdith = (standardFont.getStringWidth(currentHeader.getText()) / 1000F) * document.getStandardFont().getFontSize();
float width = header.getColumnSize(i) - 2 * cellPadding;
startX = width - textWdith;
cos.newLineAtOffset(startX, 0);
} else {
startX = 0;
}
cos.showText(currentHeader.getText());
cos.newLineAtOffset(header.getColumnSize(i) - startX, 0);
}
if (data.isEmpty()) {
currentY -= headerFont.getFontSize() - headerFont.getPadding();
@ -110,13 +121,15 @@ public class TableContent extends Content {
currentY += yOffset;
for (int colNo = 0; colNo < currentRow.getColumnCount(); colNo++) {
Text currentColText = currentRow.getColumnValue(colNo);
float cellPadding = header.getCellPadding(colNo);
float startX;
// FIXME duplication with header and textContent object
if (currentColText.getAlign() == TextAlignment.RIGHT) {
float textWdith = (standardFont.getStringWidth(currentColText.getText()) / 1000F) * document.getStandardFont().getFontSize();
float width = header.getColumnSize(colNo) - 2 * margin;
float width = header.getColumnSize(colNo) - 2 * cellPadding;
startX = width - textWdith;
LOGGER.info ("Text width for {} = {}", currentColText.getText(), textWdith);
LOGGER.info("Text width for {} = {}", currentColText.getText(), textWdith);
cos.newLineAtOffset(startX, 0);
} else {
startX = 0;

View File

@ -14,32 +14,51 @@ public class TableHeader {
private final List<Text> headers;
@Expose
private final List<Integer> colSizes;
@Expose
private final List<Float> cellPadding;
public TableHeader() {
headers = new ArrayList<>();
colSizes = new ArrayList<>();
cellPadding = new ArrayList<>();
}
public TableHeader add(Text header, int colSize) {
headers.add(header);
colSizes.add(colSize);
cellPadding.add(null);
return this;
}
public TableHeader add(String headerText, int colSize) {
headers.add(new Text(headerText));
colSizes.add(colSize);
cellPadding.add(null);
return this;
}
public TableHeader add (String headerText, int colSize, TextAlignment align) {
headers.add (new Text (headerText, align));
colSizes.add (colSize);
public TableHeader add(String headerText, int colSize, Float padding) {
headers.add(new Text(headerText));
colSizes.add(colSize);
cellPadding.add(padding);
return this;
}
/* *** getter *** */
}
public TableHeader add(String headerText, int colSize, TextAlignment align) {
headers.add(new Text(headerText, align));
colSizes.add(colSize);
cellPadding.add(null);
return this;
}
public TableHeader add(String headerText, int colSize, TextAlignment align, float padding) {
headers.add(new Text(headerText, align));
colSizes.add(colSize);
cellPadding.add(padding);
return this;
}
/* *** getter *** */
public int size() {
return headers.size();
}
@ -51,7 +70,13 @@ public class TableHeader {
public Text getHeader(int pos) {
return headers.get(pos);
}
public Float getCellPadding(int pos) {
if (cellPadding.get(pos) == null) {
return 0F;
} else {
return cellPadding.get(pos);
}
}
}

View File

@ -54,14 +54,14 @@ public class PDFDocumentTest {
TextContent invoiceInfoInformation = new TextContent(doc, 40, 442, "Sehr geehrter Anzeigenkunde, ")
.addLine()
.addLine()
.addLine("Wir danken für den Auftrag und bitten um Erledigung der folgenden Anzeigenabrechnung", TextAlignment.RIGHT)
.addLine("Wir danken für den Auftrag und bitten um Erledigung der folgenden Anzeigenabrechnung")
.addLine();
doc.addContent(invoiceInfoInformation);
TableContent invoiceLines = new TableContent(doc, doc.getFontByAlias("bold"));
invoiceLines.getHeaders()
.add("Menge", 60)
.add("Beschreibung", 300)
.add("Menge", 50, 10F)
.add("Beschreibung", 300, 10F)
.add(new Text("Einzelpreis", TextAlignment.RIGHT), 80)
.add(new Text("Summe", TextAlignment.RIGHT), 80);
invoiceLines.addTextLine(