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

View File

@ -12,7 +12,6 @@ public class TableHeader {
@Expose
private final List<Text> headers;
@Expose
private final List<Integer> colSizes;
@ -33,6 +32,14 @@ public class TableHeader {
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() {
return headers.size();
}
@ -45,4 +52,6 @@ public class TableHeader {
return headers.get(pos);
}
}

View File

@ -12,22 +12,38 @@ public class Text extends PDFElement {
private final String text;
@Expose
private final String fontAlias;
@Expose
private TextAlignment align;
public Text() {
this.text = "";
this.fontAlias = null;
this.align = null;
}
public Text(String text) {
this.text = text;
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) {
this.text = text;
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 *** */
public String getText() {
@ -37,4 +53,13 @@ public class Text extends PDFElement {
public String getFontAlias() {
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;
}
public TextContent addLine(String text, TextAlignment align) {
this.textLines.add(new Text(text, align));
return this;
}
public TextContent addLine(String text, String fontAlias) {
this.textLines.add (new Text(text, fontAlias));
this.textLines.add(new Text(text, fontAlias));
return this;
}
/* *** getter / setter */
@Override
protected Coordinate addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
float margin = 40F;
cos.beginText();
cos.newLineAtOffset(x, y);
int currentY = y;
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());
cos.setFont(pdFont, font.getFontSize());
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;
cos.setLeading(leading);
cos.showText(textLine.getText());
@ -66,6 +79,6 @@ public class TextContent extends Content {
}
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, ")
.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();
doc.addContent(invoiceInfoInformation);
@ -63,7 +63,7 @@ public class PDFDocumentTest {
.add ("Menge", 100)
.add ("Beschreibung", 300)
.add ("Einzelpreis", 100)
.add ("Summe", 100);
.add ("Summe", 100, TextAlignment.RIGHT);
invoiceLines.addLine("1","Anzeige Hövelhofer Rundschau", "10", "10");
invoiceLines.addLine ("${invoiceline.amount}", "${invoiceline.description}", "${invoiceline.price}", "${invoiceline.total}").createList("invoiceLines", "invoiceline");
invoiceLines.addLine("2","Anzeige Hövelhofer Rundschau", "10", "20");