added alignment right to tablecontent

removed obsolete variables from add content to PDF method
This commit is contained in:
jomu
2016-06-07 10:17:35 +00:00
parent faed785303
commit e1c7f37037
7 changed files with 97 additions and 47 deletions

View File

@ -12,13 +12,14 @@ import org.apache.pdfbox.pdmodel.common.PDRectangle;
public abstract class Content { public abstract class Content {
protected PDFDocument document; protected PDFDocument document;
protected PDRectangle rect;
@Expose @Expose
protected Integer x; protected Integer x;
@Expose @Expose
protected Integer y; protected Integer y;
public Content(PDFDocument document) { public Content(PDFDocument document) {
this.document = document; this.document = document;
this.x = null; this.x = null;
@ -26,7 +27,7 @@ public abstract class Content {
} }
public Content(PDFDocument doc, int x, int y) { public Content(PDFDocument doc, int x, int y) {
this.document = doc; this(doc);
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
@ -44,7 +45,7 @@ public abstract class Content {
} }
} }
protected abstract Coordinate addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException; protected abstract Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException;
/* *** getter / setter *** */ /* *** getter / setter *** */
public int getX() { public int getX() {
@ -62,5 +63,12 @@ public abstract class Content {
public void setY(int y) { public void setY(int y) {
this.y = y; this.y = y;
} }
protected PDRectangle getRectangle() {
return rect;
}
protected void setRect(PDRectangle rect) {
this.rect = rect;
}
} }

View File

@ -32,8 +32,8 @@ public class DefaultTableRow extends TableRow {
public void add(Text text) { public void add(Text text) {
row.add(text); row.add(text);
} }
/* *** TableRow methods *** */ /* *** TableRow methods *** */
@Override @Override
public int getColumnCount() { public int getColumnCount() {

View File

@ -6,7 +6,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.ConcurrentLinkedDeque;
import org.apache.commons.lang3.text.StrBuilder;
import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.apache.pdfbox.pdmodel.font.PDType1Font;

View File

@ -63,14 +63,14 @@ public class PDFTemplate {
throw new ConfigurationException("Papersize " + pdfDoc.getPaperSize().getLabel() + " not supported"); throw new ConfigurationException("Papersize " + pdfDoc.getPaperSize().getLabel() + " not supported");
} }
doc.addPage(page); doc.addPage(page);
PDRectangle rect = page.getMediaBox();
PDPageContentStream cos = new PDPageContentStream(doc, page, AppendMode.APPEND, false); PDPageContentStream cos = new PDPageContentStream(doc, page, AppendMode.APPEND, false);
Coordinate coord = null; Coordinate coord = null;
for (Content content : pdfDoc.getContentList()) { for (Content content : pdfDoc.getContentList()) {
content.setDocument(pdfDoc); // FIXME move to serialization content.setDocument(pdfDoc); // FIXME move to serialization
content.setRect (page.getMediaBox());
content.setCoordinate(coord); content.setCoordinate(coord);
coord = content.addContentToPdf(rect, cos); coord = content.addContentToPdf(cos);
} }
cos.close(); cos.close();
doc.save(filenName); doc.save(filenName);

View File

@ -6,14 +6,17 @@ import java.util.ArrayList;
import java.util.Arrays; 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.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDFont;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* *
* @author jomu * @author jomu
*/ */
public class TableContent extends Content { public class TableContent extends Content {
private static final Logger LOGGER = LoggerFactory.getLogger(TableContent.class);
@Expose @Expose
private final Font headerFont; private final Font headerFont;
@ -52,6 +55,19 @@ public class TableContent extends Content {
return newLine; return newLine;
} }
public DefaultTableRow addTextLine(Text... values) {
return addTextLine(Arrays.asList(values));
}
public DefaultTableRow addTextLine(List<Text> values) {
DefaultTableRow newLine = new DefaultTableRow();
values.stream().forEach((text) -> {
newLine.add(text);
});
data.add(newLine);
return newLine;
}
protected TableRow getRow(int no) { protected TableRow getRow(int no) {
return data.get(no); return data.get(no);
} }
@ -61,7 +77,8 @@ public class TableContent extends Content {
} }
@Override @Override
protected Coordinate addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException { protected Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException {
float margin = 0F;
cos.beginText(); cos.beginText();
PDFont hFont = document.getFont(headerFont.getFontName()); PDFont hFont = document.getFont(headerFont.getFontName());
@ -78,8 +95,10 @@ public class TableContent extends Content {
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.getHeader(i).getText()); cos.showText(header.getHeader(i).getText());
cos.newLineAtOffset(header.getColumnSize(i), 0); float textWdith = (standardFont.getStringWidth(header.getHeader(i).getText()) / 1000F) * document.getStandardFont().getFontSize();
} LOGGER.info ("Text width for {} = {}", header.getHeader(i).getText(), textWdith);
cos.newLineAtOffset(header.getColumnSize(i), 0);
}
if (data.isEmpty()) { if (data.isEmpty()) {
currentY -= headerFont.getFontSize() - headerFont.getPadding(); currentY -= headerFont.getFontSize() - headerFont.getPadding();
} }
@ -90,8 +109,21 @@ public class TableContent extends Content {
cos.newLineAtOffset(xOffSet, yOffset); cos.newLineAtOffset(xOffSet, yOffset);
currentY += yOffset; currentY += yOffset;
for (int colNo = 0; colNo < currentRow.getColumnCount(); colNo++) { for (int colNo = 0; colNo < currentRow.getColumnCount(); colNo++) {
cos.showText(currentRow.getColumnValue(colNo).getText()); Text currentColText = currentRow.getColumnValue(colNo);
cos.newLineAtOffset(header.getColumnSize(colNo), 0); float startX;
if (currentColText.getAlign() == TextAlignment.RIGHT) {
float textWdith = (standardFont.getStringWidth(currentColText.getText()) / 1000F) * document.getStandardFont().getFontSize();
float width = header.getColumnSize(colNo) - 2 * margin;
startX = width - textWdith;
LOGGER.info ("Text width for {} = {}", currentColText.getText(), textWdith);
cos.newLineAtOffset(startX, 0);
} else {
startX = 0;
}
cos.showText(currentColText.getText());
cos.newLineAtOffset(header.getColumnSize(colNo) - startX, 0);
} }
} }
currentY += yOffset; currentY += yOffset;

View File

@ -5,7 +5,6 @@ import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
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.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDFont;
/** /**
@ -55,7 +54,7 @@ public class TextContent extends Content {
/* *** getter / setter */ /* *** getter / setter */
@Override @Override
protected Coordinate addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException { protected Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException {
float margin = 40F; float margin = 40F;
cos.beginText(); cos.beginText();
cos.newLineAtOffset(x, y); cos.newLineAtOffset(x, y);

View File

@ -17,7 +17,7 @@ import org.junit.Test;
*/ */
@FixMethodOrder @FixMethodOrder
public class PDFDocumentTest { public class PDFDocumentTest {
private String jsonString = null; private String jsonString = null;
@Test @Test
@ -41,43 +41,55 @@ public class PDFDocumentTest {
informationContent.addLine(); informationContent.addLine();
informationContent.addLine("Hövelhof, den ${invoiceDate?date}", "bold"); informationContent.addLine("Hövelhof, den ${invoiceDate?date}", "bold");
doc.addContent(informationContent); doc.addContent(informationContent);
TableContent informationContent2 = new TableContent (doc, doc.getStandardFont()); TableContent informationContent2 = new TableContent(doc, doc.getStandardFont());
informationContent2.getHeaders() informationContent2.getHeaders()
.add("Kunden-Nr", 100) .add("Kunden-Nr", 100)
.add("${customerNumber}", 100); .add("${customerNumber}", 100);
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}");
doc.addContent(informationContent2); doc.addContent(informationContent2);
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", TextAlignment.RIGHT) .addLine("Wir danken für den Auftrag und bitten um Erledigung der folgenden Anzeigenabrechnung", TextAlignment.RIGHT)
.addLine(); .addLine();
doc.addContent(invoiceInfoInformation); doc.addContent(invoiceInfoInformation);
TableContent invoiceLines = new TableContent(doc, doc.getFontByAlias("bold")); TableContent invoiceLines = new TableContent(doc, doc.getFontByAlias("bold"));
invoiceLines.getHeaders() invoiceLines.getHeaders()
.add ("Menge", 100) .add("Menge", 60)
.add ("Beschreibung", 300) .add("Beschreibung", 300)
.add ("Einzelpreis", 100) .add(new Text("Einzelpreis", TextAlignment.RIGHT), 80)
.add ("Summe", 100, TextAlignment.RIGHT); .add(new Text("Summe", TextAlignment.RIGHT), 80);
invoiceLines.addLine("1","Anzeige Hövelhofer Rundschau", "10", "10"); invoiceLines.addTextLine(
invoiceLines.addLine ("${invoiceline.amount}", "${invoiceline.description}", "${invoiceline.price}", "${invoiceline.total}").createList("invoiceLines", "invoiceline"); new Text("1000", TextAlignment.RIGHT),
invoiceLines.addLine("2","Anzeige Hövelhofer Rundschau", "10", "20"); new Text("Anzeige Hövelhofer Rundschau"),
doc.addContent(invoiceLines); new Text("10,00 €", TextAlignment.RIGHT),
new Text("10,00 €", TextAlignment.RIGHT));
TextContent test = new TextContent (doc) invoiceLines.addTextLine(
.addLine("Das ist ein Test"); new Text("${invoiceline.amount}", TextAlignment.RIGHT),
doc.addContent (test); new Text("${invoiceline.description}"),
new Text("${invoiceline.price}", TextAlignment.RIGHT),
new Text("${invoiceline.total}", TextAlignment.RIGHT)).createList("invoiceLines", "invoiceline");
invoiceLines.addTextLine(
new Text("2", TextAlignment.RIGHT),
new Text("Anzeige Hövelhofer Rundschau"),
new Text("10,00 €", TextAlignment.RIGHT),
new Text("20,00 €", TextAlignment.RIGHT));
doc.addContent(invoiceLines);
TextContent test = new TextContent(doc)
.addLine("Das ist ein Test");
doc.addContent(test);
jsonString = doc.getTemplateString(); jsonString = doc.getTemplateString();
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"));
@ -85,14 +97,14 @@ public class PDFDocumentTest {
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
cfg.setLogTemplateExceptions(false); cfg.setLogTemplateExceptions(false);
Template template = cfg.getTemplate("test.ftlh"); Template template = cfg.getTemplate("test.ftlh");
PDFTemplate pdfDoc = new PDFTemplate(template); PDFTemplate pdfDoc = new PDFTemplate(template);
Invoice invoice = new Invoice(); Invoice invoice = new Invoice();
invoice.addInvoiceLine(new InvoiceLine ("Product 1", "10", "1", "10")); invoice.addInvoiceLine(new InvoiceLine("Product 1", "10,00 €", "1", "10,00 €"));
invoice.addInvoiceLine(new InvoiceLine ("Product 2", "5", "10", "50")); invoice.addInvoiceLine(new InvoiceLine("Product 2", "5,00 €", "10", "50,00 €"));
invoice.addInvoiceLine(new InvoiceLine ("Product 3", "100", "20", "2000")); invoice.addInvoiceLine(new InvoiceLine("Product 3", "100,00 €", "20", "2000,00 €"));
pdfDoc.addToDatamodel("invoiceDate", new Date()); pdfDoc.addToDatamodel("invoiceDate", new Date());
pdfDoc.addToDatamodel("customerNumber", "8755"); pdfDoc.addToDatamodel("customerNumber", "8755");
pdfDoc.addToDatamodel("invoiceNumber", "1234567"); pdfDoc.addToDatamodel("invoiceNumber", "1234567");