added method chaining for header definition

This commit is contained in:
jomu
2016-05-20 00:25:26 +00:00
parent 8aa33a0262
commit 6262d07196
3 changed files with 80 additions and 32 deletions

View File

@ -19,32 +19,24 @@ public class TableContent extends Content {
private final Font headerFont; private final Font headerFont;
@Expose @Expose
private List<Text> header; private TableHeader header;
@Expose
private List<Integer> colSizes = null;
@Expose @Expose
private List<List<Text>> data = null; private List<List<Text>> data = null;
public TableContent (PDFDocument doc, Font hf) { public TableContent (PDFDocument doc, Font hf) {
super (doc); super (doc);
this.header = new TableHeader();
this.headerFont = hf; this.headerFont = hf;
this.data = new ArrayList<>();
} }
public TableContent(PDFDocument doc, Font hf, int x, int y) { public TableContent(PDFDocument doc, Font hf, int x, int y) {
super(doc, x, y); super(doc, x, y);
this.header = new TableHeader();
this.headerFont = hf; this.headerFont = hf;
} this.data = new ArrayList<>();
public void setHeader(List<String> headers, List<Integer> cols) {
header = new ArrayList<>();
headers.stream().forEach((cellText) -> {
header.add(new Text(cellText));
});
colSizes = new ArrayList<>();
cols.stream().forEach((col) -> {
colSizes.add(col);
});
} }
public void addLine(String... values) { public void addLine(String... values) {
@ -52,9 +44,6 @@ public class TableContent extends Content {
} }
public void addLine(List<String> values) { public void addLine(List<String> values) {
if (data == null) {
data = new ArrayList<>();
}
List<Text> newLine = new ArrayList<>(); List<Text> newLine = new ArrayList<>();
values.stream().forEach((cellText) -> { values.stream().forEach((cellText) -> {
newLine.add(new Text(cellText)); newLine.add(new Text(cellText));
@ -69,8 +58,8 @@ public class TableContent extends Content {
PDFont hFont = document.getFont(headerFont.getFontName()); PDFont hFont = document.getFont(headerFont.getFontName());
PDFont standardFont = document.getFont(document.getStandardFont().getFontName()); PDFont standardFont = document.getFont(document.getStandardFont().getFontName());
int xOffSet = 0; int xOffSet = 0;
for (int i = 0; i < colSizes.size(); i++) { for (int i = 0; i < getHeaders().size(); i++) {
xOffSet -= colSizes.get(i); xOffSet -= header.getColumnSize(i);
} }
int yOffset = document.getStandardFont().getFontSize() * -1 - document.getStandardFont().getPadding(); int yOffset = document.getStandardFont().getFontSize() * -1 - document.getStandardFont().getPadding();
int currentX = x; int currentX = x;
@ -79,8 +68,11 @@ 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.get(i).getText()); cos.showText(header.getHeader(i).getText());
cos.newLineAtOffset(colSizes.get(i), 0); cos.newLineAtOffset(header.getColumnSize(i), 0);
}
if (data.size() == 0) {
currentY -= headerFont.getFontSize() - headerFont.getPadding();
} }
cos.setFont(standardFont, document.getStandardFont().getFontSize()); cos.setFont(standardFont, document.getStandardFont().getFontSize());
@ -90,7 +82,7 @@ public class TableContent extends Content {
currentY += yOffset; currentY += yOffset;
for (int colNo = 0; colNo < currentRow.size(); colNo++) { for (int colNo = 0; colNo < currentRow.size(); colNo++) {
cos.showText(currentRow.get(colNo).getText()); cos.showText(currentRow.get(colNo).getText());
cos.newLineAtOffset(colSizes.get(colNo), 0); cos.newLineAtOffset(header.getColumnSize(colNo), 0);
} }
} }
cos.endText(); cos.endText();
@ -103,4 +95,7 @@ public class TableContent extends Content {
return headerFont; return headerFont;
} }
public TableHeader getHeaders() {
return header;
}
} }

View File

@ -0,0 +1,48 @@
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author joern.muehlencord
*/
public class TableHeader {
@Expose
private final List<Text> headers;
@Expose
private final List<Integer> colSizes;
public TableHeader() {
headers = new ArrayList<>();
colSizes = new ArrayList<>();
}
public TableHeader add(Text header, int colSize) {
headers.add(header);
colSizes.add(colSize);
return this;
}
public TableHeader add(String headerText, int colSize) {
headers.add(new Text(headerText));
colSizes.add(colSize);
return this;
}
public int size() {
return headers.size();
}
public int getColumnSize(int pos) {
return colSizes.get(pos);
}
public Text getHeader(int pos) {
return headers.get(pos);
}
}

View File

@ -54,13 +54,9 @@ public class PDFDocumentTest {
doc.addContent(informationContent); doc.addContent(informationContent);
TableContent informationContent2 = new TableContent (doc, doc.getStandardFont()); TableContent informationContent2 = new TableContent (doc, doc.getStandardFont());
List<String> headers = new ArrayList<>(); informationContent2.getHeaders()
headers.add("Kunden-Nr"); .add("Kunden-Nr", 100)
headers.add("${customerNumber}"); .add("${customerNumber}", 100);
List<Integer> colSize = new ArrayList<>();
colSize.add (100);
colSize.add (100);
informationContent2.setHeader(headers, colSize);
informationContent2.addLine ("Rechnungs-Nr.:", "${invoiceNumber}"); informationContent2.addLine ("Rechnungs-Nr.:", "${invoiceNumber}");
informationContent2.addLine ("Ausgabe: ", "Dezember"); informationContent2.addLine ("Ausgabe: ", "Dezember");
informationContent2.addLine ("Rechnungsdatum:", "${invoiceDate?date}"); informationContent2.addLine ("Rechnungsdatum:", "${invoiceDate?date}");
@ -69,9 +65,18 @@ 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"); .addLine("Wir danken für den Auftrag und bitten um Erledigung der folgenden Anzeigenabrechnung")
.addLine();
doc.addContent(invoiceInfoInformation); doc.addContent(invoiceInfoInformation);
TableContent invoiceLines = new TableContent(doc, doc.getFontByAlias("bold"));
invoiceLines.getHeaders()
.add ("Menge", 100)
.add ("Beschreibung", 100)
.add ("Einzelpreis", 100)
.add ("Summe", 100);
doc.addContent(invoiceLines);
TextContent test = new TextContent (doc) TextContent test = new TextContent (doc)
.addLine("Das ist ein Test"); .addLine("Das ist ein Test");
doc.addContent (test); doc.addContent (test);