removed obsolete tableheader class

This commit is contained in:
jomu
2016-06-15 14:46:34 +00:00
parent e76d72b31c
commit b58b3f6906
6 changed files with 168 additions and 186 deletions

View File

@ -0,0 +1,25 @@
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
/**
*
* @author joern.muehlencord
*/
class ColumnDefinition {
@Expose
private final Float width;
protected ColumnDefinition(Float width) {
this.width = width;
}
/* *** getter *** */
protected float getWidth() {
return width;
}
}

View File

@ -71,6 +71,10 @@ class DefaultTableRow extends TableRow {
}
}
protected String getFontAlias(int columnPos) {
return row.get(columnPos).getFontAlias();
}
@Override
protected boolean isList() {
return isList;

View File

@ -4,7 +4,6 @@ import com.google.gson.annotations.Expose;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
@ -20,41 +19,35 @@ public class PDFTableContent extends Content {
private static final Logger LOGGER = LoggerFactory.getLogger(PDFTableContent.class);
@Expose
private final PDFFont headerFont;
@Expose
private final TableHeader header;
private final List<ColumnDefinition> columSizes;
@Expose
private List<TableRow> data = null;
public PDFTableContent(PDFDocument doc, PDFFont hf) {
super(doc);
this.header = new TableHeader();
this.headerFont = hf;
this.data = new LinkedList<>();
this.columSizes = new ArrayList<>();
this.data = new ArrayList<>();
}
public PDFTableContent(PDFDocument doc, PDFFont hf, int x, int y) {
super(doc, x, y);
this.header = new TableHeader();
this.headerFont = hf;
this.columSizes = new ArrayList<>();
this.data = new ArrayList<>();
}
public PDFTableContent addHeader(String headerText, int colSize) {
this.header.add(headerText, colSize);
public PDFTableContent addColumn(float size) {
// TODO check if enough columns are defined
this.columSizes.add(new ColumnDefinition(size));
return this;
}
public PDFTableContent addHeader(String headerText, int colSize, float padding) {
this.header.add(headerText, colSize, padding);
return this;
public PDFTableContent addColumn(float... sizes) {
// TODO check if enough columns are defined
for (float size : sizes) {
addColumn(size);
}
public PDFTableContent addHeader(String headerText, int colSize, PDFTextAlignment alignment) {
this.header.add(headerText, colSize, alignment);
return this;
}
@ -72,104 +65,112 @@ public class PDFTableContent extends Content {
}
public PDFTableContent addRow(String... values) throws ConfigurationException {
return addRow(Arrays.asList(values));
return PDFTableContent.this.addRow(Arrays.asList(values));
}
public PDFTableContent addRow(List<String> values) throws ConfigurationException {
DefaultTableRow newLine = new DefaultTableRow();
data.add(newLine);
DefaultTableRow newRow = new DefaultTableRow();
data.add(newRow);
if (values.size() != columSizes.size()) {
throw new ConfigurationException("Table defines " + columSizes.size() + " columns, but " + values.size() + " columns given");
}
validateConfiguration(newRow, false);
for (String cellText : values) {
addColumn (cellText);
PDFTableContent.this.setCellValue(cellText);
}
return this;
}
public PDFTableContent addColumn(String text) throws ConfigurationException {
public PDFTableContent setCellValue(String text) throws ConfigurationException {
TableRow lastRow = data.get(data.size() - 1);
validateConfiguration(lastRow, true);
lastRow.addColumn(new TextElement(text));
return this;
}
public PDFTableContent setCellValue(String text, String fontAlias) throws ConfigurationException {
TableRow lastRow = data.get(data.size() - 1);
validateConfiguration(lastRow, true);
data.get(data.size() - 1).addColumn(new TextElement(text, fontAlias));
return this;
}
public PDFTableContent setCellValue(String text, float padding) throws ConfigurationException {
TableRow lastRow = data.get(data.size() - 1);
validateConfiguration(lastRow, true);
lastRow.addColumn(new TextElement(text), padding);
return this;
}
public PDFTableContent setCellValue(String text, String fontAlias, float padding) throws ConfigurationException {
TableRow lastRow = data.get(data.size() - 1);
validateConfiguration(lastRow, true);
lastRow.addColumn(new TextElement(text, fontAlias), padding);
return this;
}
public PDFTableContent setCellValue(String text, PDFTextAlignment alignment) throws ConfigurationException {
TableRow lastRow = data.get(data.size() - 1);
validateConfiguration(lastRow, true);
lastRow.addColumn(new TextElement(text, alignment));
return this;
}
public PDFTableContent setCellValue(String text, String fontAlias, PDFTextAlignment alignment) throws ConfigurationException {
if (data.isEmpty()) {
throw new ConfigurationException ("Need to call newRow first");
throw new ConfigurationException("Need to call newRow first");
}
data.get(data.size()-1).addColumn(new TextElement (text));
data.get(data.size() - 1).addColumn(new TextElement(text, fontAlias, alignment));
return this;
}
public PDFTableContent addColumn(String text, PDFTextAlignment alignment) throws ConfigurationException {
public PDFTableContent setCellValue(String text, PDFTextAlignment alignment, float padding) throws ConfigurationException {
if (data.isEmpty()) {
throw new ConfigurationException ("Need to call newRow first");
throw new ConfigurationException("Need to call newRow first");
}
data.get(data.size()-1).addColumn(new TextElement (text, alignment));
data.get(data.size() - 1).addColumn(new TextElement(text, alignment), padding);
return this;
}
public PDFTableContent addColumn(String text, PDFTextAlignment alignment, float padding) throws ConfigurationException {
if (data.isEmpty()) {
throw new ConfigurationException ("Need to call newRow first");
}
data.get(data.size()-1).addColumn(new TextElement (text, alignment), padding);
return this;
}
protected TableRow getRow(int no) {
return data.get(no);
}
protected int getRowCount() {
return data.size();
}
@Override
protected Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException {
cos.beginText();
PDFont hFont = document.getFont(headerFont.getFontName());
PDFont standardFont = document.getFont(document.getStandardFont().getFontName());
int xOffSet = 0;
for (int i = 0; i < header.size(); i++) {
xOffSet -= header.getColumnSize(i);
for (int i = 0; i < columSizes.size(); i++) {
xOffSet -= columSizes.get(i).getWidth();
}
int yOffset = document.getStandardFont().getFontSize() * -1 - document.getStandardFont().getPadding();
float currentX = x;
float currentY = y;
cos.setFont(hFont, headerFont.getFontSize());
cos.newLineAtOffset(x, y);
for (int i = 0; i < header.size(); i++) {
String headerText = header.getHeader(i);
PDFTextAlignment currentAlignment = header.getAlignment(i);
float cellPadding = header.getCellPadding(i);
float startX;
if (currentAlignment == PDFTextAlignment.RIGHT) {
float textWdith = (standardFont.getStringWidth(headerText) / 1000F) * document.getStandardFont().getFontSize();
float width = header.getColumnSize(i) - (2F * cellPadding);
startX = width - textWdith;
cos.newLineAtOffset(startX, 0);
} else {
startX = 0;
}
cos.showText(headerText);
cos.newLineAtOffset(header.getColumnSize(i) - startX, 0);
}
if (data.isEmpty()) {
currentY -= headerFont.getFontSize() - headerFont.getPadding();
}
cos.setFont(standardFont, document.getStandardFont().getFontSize());
for (int lineNo = 0; lineNo < data.size(); lineNo++) {
TableRow currentRow = data.get(lineNo);
cos.newLineAtOffset(xOffSet, yOffset);
currentY += yOffset;
for (int colNo = 0; colNo < currentRow.getColumnCount(); colNo++) {
String currentColText = currentRow.getColumnValue(colNo);
PDFTextAlignment currentColAlignment = currentRow.getAlignment(colNo);
float cellPadding = currentRow.getCellPadding(colNo);
float startX;
float colWidth = columSizes.get(colNo).getWidth();
String fontAlias = currentRow.getFontAlias(colNo);
PDFFont font;
int fontSize;
if (fontAlias != null) {
font = document.getFontByAlias(fontAlias);
fontSize = document.getFontByAlias(fontAlias).getFontSize();
} else {
font = document.getStandardFont();
fontSize = document.getStandardFont().getFontSize();
}
PDFont pdFont = document.getFont(font.getFontName());
cos.setFont(pdFont, fontSize);
float startX;
// FIXME duplication with header and textContent object
if (currentColAlignment == PDFTextAlignment.RIGHT) {
float textWdith = (standardFont.getStringWidth(currentColText) / 1000F) * document.getStandardFont().getFontSize();
float width = header.getColumnSize(colNo) - (2F * cellPadding);
float textWdith = (pdFont.getStringWidth(currentColText) / 1000F) * fontSize;
float width = colWidth - (2F * cellPadding);
startX = width - textWdith;
LOGGER.info("Text width for {} = {}", currentColText, textWdith);
cos.newLineAtOffset(startX, 0);
@ -178,8 +179,10 @@ public class PDFTableContent extends Content {
}
cos.showText(currentColText);
cos.newLineAtOffset(header.getColumnSize(colNo) - startX, 0);
cos.newLineAtOffset(columSizes.get(colNo).getWidth() - startX, 0);
}
cos.newLineAtOffset(xOffSet, yOffset);
currentY += yOffset;
}
currentY += yOffset;
@ -188,8 +191,15 @@ public class PDFTableContent extends Content {
return new Coordinate(currentX, currentY);
}
/* *** getter / setter *** */
public PDFFont getHeaderFont() {
return headerFont;
private void validateConfiguration(TableRow lastRow, boolean calledFromSetCellValue) throws ConfigurationException {
if (data.isEmpty()) {
throw new ConfigurationException("Need to call newRow first");
}
if (calledFromSetCellValue) {
if (lastRow.getColumnCount() >= columSizes.size()) {
throw new ConfigurationException("Cannot add more columns than defined");
}
}
}
}

View File

@ -1,74 +0,0 @@
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author joern.muehlencord
*/
class TableHeader {
@Expose
private final List<CellValue> headerCells;
public TableHeader() {
headerCells = new ArrayList<>();
}
public TableHeader add(String headerText, int colSize) {
headerCells.add (new TextCellValue (headerText, colSize));
return this;
}
public TableHeader add(String headerText, int colSize, float padding) {
headerCells.add (new TextCellValue (headerText, colSize));
return this;
}
public TableHeader add(String headerText, int colSize, PDFTextAlignment align) {
headerCells.add (new TextCellValue (headerText, colSize, align));
return this;
}
public TableHeader add(String headerText, int colSize, PDFTextAlignment align, float padding) {
headerCells.add (new TextCellValue (headerText, colSize, align, padding));
return this;
}
/* *** getter *** */
public int size() {
return headerCells.size();
}
public int getColumnSize(int pos) {
return headerCells.get(pos).getColSize();
}
public String getHeader(int pos) throws ConfigurationException {
CellValue header = headerCells.get(pos);
if (header instanceof TextCellValue) {
TextCellValue textHeader = (TextCellValue) header;
return textHeader.getCellText();
} else {
return "Unsupported";
}
}
public PDFTextAlignment getAlignment (int pos) {
CellValue header = headerCells.get(pos);
if (header instanceof TextCellValue) {
TextCellValue textHeader = (TextCellValue) header;
return textHeader.getAlignment();
} else {
return null;
}
}
protected float getCellPadding(int pos) {
return headerCells.get(pos).getCellPadding();
}
}

View File

@ -18,6 +18,8 @@ abstract class TableRow {
protected abstract float getCellPadding (int columnPos);
protected abstract String getFontAlias(int columnPos);
protected abstract String getListName();
protected abstract String getVarName();
@ -27,4 +29,6 @@ abstract class TableRow {
protected abstract void addColumn(TextElement element, float padding);
}

View File

@ -1,5 +1,17 @@
package de.muehlencord.shared.pdf;
package de.muehlencord.shared.pdf.test;
import de.muehlencord.shared.pdf.ConfigurationException;
import de.muehlencord.shared.pdf.Invoice;
import de.muehlencord.shared.pdf.InvoiceLine;
import de.muehlencord.shared.pdf.PDFDocument;
import de.muehlencord.shared.pdf.PDFFont;
import de.muehlencord.shared.pdf.PDFImageContent;
import de.muehlencord.shared.pdf.PDFPaperSize;
import de.muehlencord.shared.pdf.PDFTableContent;
import de.muehlencord.shared.pdf.PDFTemplate;
import de.muehlencord.shared.pdf.PDFTextAlignment;
import de.muehlencord.shared.pdf.PDFTextContent;
import de.muehlencord.shared.pdf.TemplateException;
import de.muehlencord.shared.pdf.util.ImageUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
@ -49,10 +61,9 @@ public class PDFDocumentTest {
doc.addContent(informationContent);
PDFTableContent informationContent2 = new PDFTableContent(doc, doc.getStandardFont());
informationContent2
.addHeader("Kunden-Nr", 100)
.addHeader("${invoice.customerNumber}", 100);
informationContent2.addRow("Rechnungs-Nr.:", "${invoice.invoiceNumber}")
informationContent2.addColumn(100, 100)
.addRow("Kunden-Nr", "${invoice.customerNumber}")
.addRow("Rechnungs-Nr.:", "${invoice.invoiceNumber}")
.addRow("Ausgabe: ", "Dezember")
.addRow("Rechnungsdatum:", "${invoice.invoiceDate?date}");
doc.addContent(informationContent2);
@ -65,25 +76,27 @@ public class PDFDocumentTest {
doc.addContent(invoiceInfoInformation);
PDFTableContent invoiceLines = new PDFTableContent(doc, doc.getFontByAlias("bold"));
invoiceLines.addHeader("Menge", 50, 10F)
.addHeader("Beschreibung", 300, 10F)
.addHeader("Einzelpreis", 80, PDFTextAlignment.RIGHT)
.addHeader("Summe", 80, PDFTextAlignment.RIGHT);
invoiceLines.addColumn(50, 300, 80, 80);
invoiceLines.addRow()
.addColumn("1000", PDFTextAlignment.RIGHT, 5F)
.addColumn("Anzeige Hövelhofer Rundschau")
.addColumn("10,00 €", PDFTextAlignment.RIGHT)
.addColumn("10,00 €", PDFTextAlignment.RIGHT);
.setCellValue("Menge", "bold", 10F)
.setCellValue("Beschreibung", "bold", 10F)
.setCellValue("Einzelpreis", "bold", PDFTextAlignment.RIGHT)
.setCellValue("Summe", "bold", PDFTextAlignment.RIGHT);
invoiceLines.addRow()
.setCellValue("1000", PDFTextAlignment.RIGHT, 5F)
.setCellValue("Anzeige Hövelhofer Rundschau")
.setCellValue("10,00 €", PDFTextAlignment.RIGHT)
.setCellValue("10,00 €", PDFTextAlignment.RIGHT);
invoiceLines.addListRow("invoice.invoiceLines", "invoiceline")
.addColumn("${invoiceline.amount}", PDFTextAlignment.RIGHT, 5F)
.addColumn("${invoiceline.description}")
.addColumn("${invoiceline.price}", PDFTextAlignment.RIGHT)
.addColumn("${invoiceline.total}", PDFTextAlignment.RIGHT);
.setCellValue("${invoiceline.amount}", PDFTextAlignment.RIGHT, 5F)
.setCellValue("${invoiceline.description}")
.setCellValue("${invoiceline.price}", PDFTextAlignment.RIGHT)
.setCellValue("${invoiceline.total}", PDFTextAlignment.RIGHT);
invoiceLines.addRow()
.addColumn("2", PDFTextAlignment.RIGHT, 5F)
.addColumn("Anzeige Hövelhofer Rundschau")
.addColumn("10,00 €", PDFTextAlignment.RIGHT)
.addColumn("20,00 €", PDFTextAlignment.RIGHT);
.setCellValue("2", PDFTextAlignment.RIGHT, 5F)
.setCellValue("Anzeige Hövelhofer Rundschau")
.setCellValue("10,00 €", PDFTextAlignment.RIGHT)
.setCellValue("20,00 €", PDFTextAlignment.RIGHT);
doc.addContent(invoiceLines);
PDFTextContent test = new PDFTextContent(doc)