completed first version of table based content
This commit is contained in:
@ -3,6 +3,7 @@ package de.muehlencord.shared.pdf;
|
||||
import com.google.gson.annotations.Expose;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||
@ -25,30 +26,35 @@ public class TableContent extends Content {
|
||||
@Expose
|
||||
private List<List<Text>> data = null;
|
||||
|
||||
|
||||
public TableContent(PDFDocument doc, Font hf, int x, int y) {
|
||||
super(doc, x, y);
|
||||
this.headerFont = hf;
|
||||
}
|
||||
|
||||
public void setHeader (List<String> headers, List<Integer> cols) {
|
||||
public void setHeader(List<String> headers, List<Integer> cols) {
|
||||
header = new ArrayList<>();
|
||||
headers.stream().forEach((cellText) -> {
|
||||
header.add (new Text (cellText));
|
||||
header.add(new Text(cellText));
|
||||
});
|
||||
colSizes = new ArrayList<>();
|
||||
cols.stream().forEach((col) -> {
|
||||
colSizes.add (col);
|
||||
colSizes.add(col);
|
||||
});
|
||||
}
|
||||
|
||||
public void addLine (List<String> values) {
|
||||
public void addLine(String... values) {
|
||||
addLine(Arrays.asList(values));
|
||||
}
|
||||
|
||||
public void addLine(List<String> values) {
|
||||
if (data == null) {
|
||||
data = new ArrayList<>();
|
||||
}
|
||||
List<Text> newLine = new ArrayList<>();
|
||||
values.stream().forEach((cellText) -> {
|
||||
header.add (new Text (cellText));
|
||||
newLine.add(new Text(cellText));
|
||||
});
|
||||
data.add(newLine);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -56,17 +62,31 @@ public class TableContent extends Content {
|
||||
cos.beginText();
|
||||
|
||||
PDFont hFont = document.getFont(headerFont.getFontName());
|
||||
PDFont standardFont = document.getFont(document.getStandardFont().getFontName());
|
||||
int xOffSet = 0;
|
||||
for (int i = 0; i < colSizes.size(); i++) {
|
||||
xOffSet -= colSizes.get(i);
|
||||
}
|
||||
int yOffset = document.getStandardFont().getFontSize() * -1 - document.getStandardFont().getPadding();
|
||||
|
||||
cos.setFont(hFont, headerFont.getFontSize());
|
||||
cos.newLineAtOffset(x, y);
|
||||
for (int i = 0; i < header.size(); i++) {
|
||||
cos.showText(header.get(i).getText());
|
||||
cos.newLineAtOffset(colSizes.get(i), 0);
|
||||
|
||||
}
|
||||
|
||||
cos.setFont(standardFont, document.getStandardFont().getFontSize());
|
||||
for (int lineNo = 0; lineNo < data.size(); lineNo++) {
|
||||
List<Text> currentRow = data.get(lineNo);
|
||||
cos.newLineAtOffset(xOffSet, yOffset);
|
||||
for (int colNo = 0; colNo < currentRow.size(); colNo++) {
|
||||
cos.showText(currentRow.get(colNo).getText());
|
||||
cos.newLineAtOffset(colSizes.get(colNo), 0);
|
||||
}
|
||||
}
|
||||
cos.endText();
|
||||
// cos.showText("Telefon:");
|
||||
// cos.newLineAtOffset(tabSize, 0);
|
||||
// cos.showText("05257 / 1234567");
|
||||
// cos.newLineAtOffset(tabSize * -1, -14); // leading
|
||||
// cos.showText("Fax:");
|
||||
// cos.newLineAtOffset(tabSize, 0);
|
||||
// cos.showText("05257 / 1234567");
|
||||
// cos.newLineAtOffset(tabSize * -1, -14); // leading
|
||||
|
||||
}
|
||||
|
||||
/* *** getter / setter *** */
|
||||
|
||||
@ -60,14 +60,18 @@ public class PDFDocumentTest {
|
||||
doc.addContent(informationContent);
|
||||
|
||||
|
||||
TableContent informationContent2 = new TableContent (doc, doc.getStandardFont(), 400, 500);
|
||||
TableContent informationContent2 = new TableContent (doc, doc.getStandardFont(), 400, 580);
|
||||
List<String> headers = new ArrayList<>();
|
||||
headers.add("Kunden-Nr");
|
||||
headers.add("${customer.customerNumber}");
|
||||
headers.add("${customerNumber}");
|
||||
List<Integer> colSize = new ArrayList<>();
|
||||
colSize.add (100);
|
||||
colSize.add (100);
|
||||
informationContent2.setHeader(headers, colSize);
|
||||
informationContent2.addLine ("Rechnungs-Nr.:", "${invoiceNumber}");
|
||||
informationContent2.addLine ("Ausgabe: ", "Dezember");
|
||||
informationContent2.addLine ("Rechnungsdatum:", "${invoiceDate?date}");
|
||||
doc.addContent(informationContent2);
|
||||
|
||||
jsonString = gson.toJson(doc);
|
||||
System.out.println(jsonString);
|
||||
@ -75,7 +79,6 @@ public class PDFDocumentTest {
|
||||
File file = new File("c:/temp/test.ftlh");
|
||||
FileUtils.writeStringToFile(file, jsonString, "UTF-8");
|
||||
|
||||
|
||||
// create pdf
|
||||
Configuration cfg = new Configuration(Configuration.VERSION_2_3_24);
|
||||
cfg.setDirectoryForTemplateLoading(new File("c:/temp"));
|
||||
@ -86,6 +89,8 @@ public class PDFDocumentTest {
|
||||
Template template = cfg.getTemplate("test.ftlh");
|
||||
PDFTemplate pdfDoc = new PDFTemplate(template);
|
||||
pdfDoc.addToDatamodel("invoiceDate", new Date());
|
||||
pdfDoc.addToDatamodel("customerNumber", "8755");
|
||||
pdfDoc.addToDatamodel("invoiceNumber", "1234567");
|
||||
pdfDoc.create("c:/temp/test.pdf");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user