added support for text alignment

This commit is contained in:
jomu
2016-06-03 23:44:59 +00:00
parent 969b583001
commit faed785303
7 changed files with 77 additions and 18 deletions

View File

@ -52,7 +52,7 @@ public class TableContent extends Content {
return newLine; return newLine;
} }
protected TableRow getRow (int no) { protected TableRow getRow(int no) {
return data.get(no); return data.get(no);
} }
@ -78,8 +78,8 @@ 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); cos.newLineAtOffset(header.getColumnSize(i), 0);
} }
if (data.isEmpty()) { if (data.isEmpty()) {
currentY -= headerFont.getFontSize() - headerFont.getPadding(); currentY -= headerFont.getFontSize() - headerFont.getPadding();
} }
@ -95,6 +95,7 @@ public class TableContent extends Content {
} }
} }
currentY += yOffset; currentY += yOffset;
cos.endText(); cos.endText();
return new Coordinate(currentX, currentY); return new Coordinate(currentX, currentY);

View File

@ -12,7 +12,6 @@ public class TableHeader {
@Expose @Expose
private final List<Text> headers; private final List<Text> headers;
@Expose @Expose
private final List<Integer> colSizes; private final List<Integer> colSizes;
@ -33,6 +32,14 @@ public class TableHeader {
return this; return this;
} }
public TableHeader add (String headerText, int colSize, TextAlignment align) {
headers.add (new Text (headerText, align));
colSizes.add (colSize);
return this;
}
/* *** getter *** */
public int size() { public int size() {
return headers.size(); return headers.size();
} }
@ -45,4 +52,6 @@ public class TableHeader {
return headers.get(pos); return headers.get(pos);
} }
} }

View File

@ -12,22 +12,38 @@ public class Text extends PDFElement {
private final String text; private final String text;
@Expose @Expose
private final String fontAlias; private final String fontAlias;
@Expose
private TextAlignment align;
public Text() { public Text() {
this.text = ""; this.text = "";
this.fontAlias = null; this.fontAlias = null;
this.align = null;
} }
public Text(String text) { public Text(String text) {
this.text = text; this.text = text;
this.fontAlias = null; this.fontAlias = null;
this.align = null;
}
public Text(String text, TextAlignment align) {
this.text = text;
this.fontAlias = null;
this.align = align;
} }
public Text(String text, String fontAlias) { public Text(String text, String fontAlias) {
this.text = text; this.text = text;
this.fontAlias = fontAlias; this.fontAlias = fontAlias;
this.align = null;
} }
public Text(String text, String fontAlias, TextAlignment align) {
this.text = text;
this.fontAlias = fontAlias;
this.align = align;
}
/* *** getter / setter *** */ /* *** getter / setter *** */
public String getText() { public String getText() {
@ -37,4 +53,13 @@ public class Text extends PDFElement {
public String getFontAlias() { public String getFontAlias() {
return fontAlias; return fontAlias;
} }
public TextAlignment getAlign() {
if (align == null) {
return TextAlignment.LEFT;
} else {
return align;
}
}
} }

View File

@ -0,0 +1,11 @@
package de.muehlencord.shared.pdf;
/**
*
* @author joern.muehlencord
*/
public enum TextAlignment {
LEFT,
RIGHT;
}

View File

@ -43,22 +43,35 @@ public class TextContent extends Content {
return this; return this;
} }
public TextContent addLine(String text, TextAlignment align) {
this.textLines.add(new Text(text, align));
return this;
}
public TextContent addLine(String text, String fontAlias) { public TextContent addLine(String text, String fontAlias) {
this.textLines.add (new Text(text, fontAlias)); this.textLines.add(new Text(text, fontAlias));
return this; return this;
} }
/* *** getter / setter */ /* *** getter / setter */
@Override @Override
protected Coordinate addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException { protected Coordinate addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
float margin = 40F;
cos.beginText(); cos.beginText();
cos.newLineAtOffset(x, y); cos.newLineAtOffset(x, y);
int currentY = y; int currentY = y;
for (Text textLine : textLines) { for (Text textLine : textLines) {
Font font = (textLine.getFontAlias()== null ? document.getStandardFont() : document.getFontByAlias(textLine.getFontAlias())); Font font = (textLine.getFontAlias() == null ? document.getStandardFont() : document.getFontByAlias(textLine.getFontAlias()));
PDFont pdFont = document.getFont(font.getFontName()); PDFont pdFont = document.getFont(font.getFontName());
cos.setFont(pdFont, font.getFontSize()); cos.setFont(pdFont, font.getFontSize());
int leading = font.getFontSize() + font.getPadding(); int leading = font.getFontSize() + font.getPadding();
if (textLine.getAlign() == TextAlignment.RIGHT) {
float textWdith = (pdFont.getStringWidth(textLine.getText()) / 1000F) * font.getFontSize();
float width = rect.getUpperRightX() - rect.getLowerLeftX() - 2 * margin;
float startX = width - textWdith;
cos.newLineAtOffset(startX, 0);
}
currentY -= leading; currentY -= leading;
cos.setLeading(leading); cos.setLeading(leading);
cos.showText(textLine.getText()); cos.showText(textLine.getText());
@ -66,6 +79,6 @@ public class TextContent extends Content {
} }
cos.endText(); cos.endText();
return new Coordinate (x, currentY); return new Coordinate(x, currentY);
} }
} }

View File

@ -54,7 +54,7 @@ public class PDFDocumentTest {
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") .addLine("Wir danken für den Auftrag und bitten um Erledigung der folgenden Anzeigenabrechnung", TextAlignment.RIGHT)
.addLine(); .addLine();
doc.addContent(invoiceInfoInformation); doc.addContent(invoiceInfoInformation);
@ -63,7 +63,7 @@ public class PDFDocumentTest {
.add ("Menge", 100) .add ("Menge", 100)
.add ("Beschreibung", 300) .add ("Beschreibung", 300)
.add ("Einzelpreis", 100) .add ("Einzelpreis", 100)
.add ("Summe", 100); .add ("Summe", 100, TextAlignment.RIGHT);
invoiceLines.addLine("1","Anzeige Hövelhofer Rundschau", "10", "10"); invoiceLines.addLine("1","Anzeige Hövelhofer Rundschau", "10", "10");
invoiceLines.addLine ("${invoiceline.amount}", "${invoiceline.description}", "${invoiceline.price}", "${invoiceline.total}").createList("invoiceLines", "invoiceline"); invoiceLines.addLine ("${invoiceline.amount}", "${invoiceline.description}", "${invoiceline.price}", "${invoiceline.total}").createList("invoiceLines", "invoiceline");
invoiceLines.addLine("2","Anzeige Hövelhofer Rundschau", "10", "20"); invoiceLines.addLine("2","Anzeige Hövelhofer Rundschau", "10", "20");