added cellPadding support for tables
This commit is contained in:
@ -78,7 +78,6 @@ public class TableContent extends Content {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException {
|
protected Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException {
|
||||||
float margin = 0F;
|
|
||||||
cos.beginText();
|
cos.beginText();
|
||||||
|
|
||||||
PDFont hFont = document.getFont(headerFont.getFontName());
|
PDFont hFont = document.getFont(headerFont.getFontName());
|
||||||
@ -94,10 +93,22 @@ public class TableContent extends Content {
|
|||||||
cos.setFont(hFont, headerFont.getFontSize());
|
cos.setFont(hFont, headerFont.getFontSize());
|
||||||
cos.newLineAtOffset(x, y);
|
cos.newLineAtOffset(x, y);
|
||||||
for (int i = 0; i < header.size(); i++) {
|
for (int i = 0; i < header.size(); i++) {
|
||||||
cos.showText(header.getHeader(i).getText());
|
Text currentHeader = header.getHeader(i);
|
||||||
float textWdith = (standardFont.getStringWidth(header.getHeader(i).getText()) / 1000F) * document.getStandardFont().getFontSize();
|
float cellPadding = header.getCellPadding(i);
|
||||||
LOGGER.info ("Text width for {} = {}", header.getHeader(i).getText(), textWdith);
|
|
||||||
cos.newLineAtOffset(header.getColumnSize(i), 0);
|
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()) {
|
if (data.isEmpty()) {
|
||||||
currentY -= headerFont.getFontSize() - headerFont.getPadding();
|
currentY -= headerFont.getFontSize() - headerFont.getPadding();
|
||||||
@ -110,13 +121,15 @@ public class TableContent extends Content {
|
|||||||
currentY += yOffset;
|
currentY += yOffset;
|
||||||
for (int colNo = 0; colNo < currentRow.getColumnCount(); colNo++) {
|
for (int colNo = 0; colNo < currentRow.getColumnCount(); colNo++) {
|
||||||
Text currentColText = currentRow.getColumnValue(colNo);
|
Text currentColText = currentRow.getColumnValue(colNo);
|
||||||
|
float cellPadding = header.getCellPadding(colNo);
|
||||||
float startX;
|
float startX;
|
||||||
if (currentColText.getAlign() == TextAlignment.RIGHT) {
|
|
||||||
|
|
||||||
|
// FIXME duplication with header and textContent object
|
||||||
|
if (currentColText.getAlign() == TextAlignment.RIGHT) {
|
||||||
float textWdith = (standardFont.getStringWidth(currentColText.getText()) / 1000F) * document.getStandardFont().getFontSize();
|
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;
|
startX = width - textWdith;
|
||||||
LOGGER.info ("Text width for {} = {}", currentColText.getText(), textWdith);
|
LOGGER.info("Text width for {} = {}", currentColText.getText(), textWdith);
|
||||||
cos.newLineAtOffset(startX, 0);
|
cos.newLineAtOffset(startX, 0);
|
||||||
} else {
|
} else {
|
||||||
startX = 0;
|
startX = 0;
|
||||||
|
|||||||
@ -14,32 +14,51 @@ public class TableHeader {
|
|||||||
private final List<Text> headers;
|
private final List<Text> headers;
|
||||||
@Expose
|
@Expose
|
||||||
private final List<Integer> colSizes;
|
private final List<Integer> colSizes;
|
||||||
|
@Expose
|
||||||
|
private final List<Float> cellPadding;
|
||||||
|
|
||||||
public TableHeader() {
|
public TableHeader() {
|
||||||
headers = new ArrayList<>();
|
headers = new ArrayList<>();
|
||||||
colSizes = new ArrayList<>();
|
colSizes = new ArrayList<>();
|
||||||
|
cellPadding = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public TableHeader add(Text header, int colSize) {
|
public TableHeader add(Text header, int colSize) {
|
||||||
headers.add(header);
|
headers.add(header);
|
||||||
colSizes.add(colSize);
|
colSizes.add(colSize);
|
||||||
|
cellPadding.add(null);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TableHeader add(String headerText, int colSize) {
|
public TableHeader add(String headerText, int colSize) {
|
||||||
headers.add(new Text(headerText));
|
headers.add(new Text(headerText));
|
||||||
colSizes.add(colSize);
|
colSizes.add(colSize);
|
||||||
|
cellPadding.add(null);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TableHeader add (String headerText, int colSize, TextAlignment align) {
|
public TableHeader add(String headerText, int colSize, Float padding) {
|
||||||
headers.add (new Text (headerText, align));
|
headers.add(new Text(headerText));
|
||||||
colSizes.add (colSize);
|
colSizes.add(colSize);
|
||||||
|
cellPadding.add(padding);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* *** getter *** */
|
/* *** getter *** */
|
||||||
|
|
||||||
public int size() {
|
public int size() {
|
||||||
return headers.size();
|
return headers.size();
|
||||||
}
|
}
|
||||||
@ -52,6 +71,12 @@ public class TableHeader {
|
|||||||
return headers.get(pos);
|
return headers.get(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Float getCellPadding(int pos) {
|
||||||
|
if (cellPadding.get(pos) == null) {
|
||||||
|
return 0F;
|
||||||
|
} else {
|
||||||
|
return cellPadding.get(pos);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -54,14 +54,14 @@ public class PDFDocumentTest {
|
|||||||
TextContent invoiceInfoInformation = new TextContent(doc, 40, 442, "Sehr geehrter Anzeigenkunde, ")
|
TextContent invoiceInfoInformation = new TextContent(doc, 40, 442, "Sehr geehrter Anzeigenkunde, ")
|
||||||
.addLine()
|
.addLine()
|
||||||
.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();
|
.addLine();
|
||||||
doc.addContent(invoiceInfoInformation);
|
doc.addContent(invoiceInfoInformation);
|
||||||
|
|
||||||
TableContent invoiceLines = new TableContent(doc, doc.getFontByAlias("bold"));
|
TableContent invoiceLines = new TableContent(doc, doc.getFontByAlias("bold"));
|
||||||
invoiceLines.getHeaders()
|
invoiceLines.getHeaders()
|
||||||
.add("Menge", 60)
|
.add("Menge", 50, 10F)
|
||||||
.add("Beschreibung", 300)
|
.add("Beschreibung", 300, 10F)
|
||||||
.add(new Text("Einzelpreis", TextAlignment.RIGHT), 80)
|
.add(new Text("Einzelpreis", TextAlignment.RIGHT), 80)
|
||||||
.add(new Text("Summe", TextAlignment.RIGHT), 80);
|
.add(new Text("Summe", TextAlignment.RIGHT), 80);
|
||||||
invoiceLines.addTextLine(
|
invoiceLines.addTextLine(
|
||||||
|
|||||||
Reference in New Issue
Block a user