completed first version of table based content

This commit is contained in:
jomu
2016-05-19 23:12:52 +00:00
parent 87838d3c0d
commit 36de01c8db
2 changed files with 55 additions and 30 deletions

View File

@ -3,6 +3,7 @@ package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.common.PDRectangle;
@ -25,30 +26,35 @@ public class TableContent extends Content {
@Expose @Expose
private List<List<Text>> data = null; private List<List<Text>> data = null;
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.headerFont = hf; this.headerFont = hf;
} }
public void setHeader (List<String> headers, List<Integer> cols) { public void setHeader(List<String> headers, List<Integer> cols) {
header = new ArrayList<>(); header = new ArrayList<>();
headers.stream().forEach((cellText) -> { headers.stream().forEach((cellText) -> {
header.add (new Text (cellText)); header.add(new Text(cellText));
}); });
colSizes = new ArrayList<>(); colSizes = new ArrayList<>();
cols.stream().forEach((col) -> { 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) { if (data == null) {
data = new ArrayList<>(); data = new ArrayList<>();
} }
List<Text> newLine = new ArrayList<>();
values.stream().forEach((cellText) -> { values.stream().forEach((cellText) -> {
header.add (new Text (cellText)); newLine.add(new Text(cellText));
}); });
data.add(newLine);
} }
@Override @Override
@ -56,17 +62,31 @@ public class TableContent extends Content {
cos.beginText(); cos.beginText();
PDFont hFont = document.getFont(headerFont.getFontName()); 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.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 *** */ /* *** getter / setter *** */

View File

@ -60,14 +60,18 @@ public class PDFDocumentTest {
doc.addContent(informationContent); 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<>(); List<String> headers = new ArrayList<>();
headers.add("Kunden-Nr"); headers.add("Kunden-Nr");
headers.add("${customer.customerNumber}"); headers.add("${customerNumber}");
List<Integer> colSize = new ArrayList<>(); List<Integer> colSize = new ArrayList<>();
colSize.add (100); colSize.add (100);
colSize.add (100); colSize.add (100);
informationContent2.setHeader(headers, colSize); 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); jsonString = gson.toJson(doc);
System.out.println(jsonString); System.out.println(jsonString);
@ -75,7 +79,6 @@ public class PDFDocumentTest {
File file = new File("c:/temp/test.ftlh"); File file = new File("c:/temp/test.ftlh");
FileUtils.writeStringToFile(file, jsonString, "UTF-8"); FileUtils.writeStringToFile(file, jsonString, "UTF-8");
// create pdf // create pdf
Configuration cfg = new Configuration(Configuration.VERSION_2_3_24); Configuration cfg = new Configuration(Configuration.VERSION_2_3_24);
cfg.setDirectoryForTemplateLoading(new File("c:/temp")); cfg.setDirectoryForTemplateLoading(new File("c:/temp"));
@ -86,6 +89,8 @@ public class PDFDocumentTest {
Template template = cfg.getTemplate("test.ftlh"); Template template = cfg.getTemplate("test.ftlh");
PDFTemplate pdfDoc = new PDFTemplate(template); PDFTemplate pdfDoc = new PDFTemplate(template);
pdfDoc.addToDatamodel("invoiceDate", new Date()); pdfDoc.addToDatamodel("invoiceDate", new Date());
pdfDoc.addToDatamodel("customerNumber", "8755");
pdfDoc.addToDatamodel("invoiceNumber", "1234567");
pdfDoc.create("c:/temp/test.pdf"); pdfDoc.create("c:/temp/test.pdf");
} }