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 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;
|
||||||
@ -24,49 +25,68 @@ 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) {
|
|
||||||
if (data == null) {
|
|
||||||
data = new ArrayList<>();
|
|
||||||
}
|
|
||||||
values.stream().forEach((cellText) -> {
|
|
||||||
header.add (new Text (cellText));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) -> {
|
||||||
|
newLine.add(new Text(cellText));
|
||||||
|
});
|
||||||
|
data.add(newLine);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
|
protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
|
||||||
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 *** */
|
||||||
|
|||||||
@ -60,22 +60,25 @@ 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);
|
||||||
|
|
||||||
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user