added support for text alignment
This commit is contained in:
@ -63,7 +63,7 @@ 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();
|
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;
|
||||||
|
|||||||
@ -51,11 +51,11 @@ public class TableContent extends Content {
|
|||||||
data.add(newLine);
|
data.add(newLine);
|
||||||
return newLine;
|
return newLine;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected TableRow getRow (int no) {
|
protected TableRow getRow(int no) {
|
||||||
return data.get(no);
|
return data.get(no);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int getRowCount() {
|
protected int getRowCount() {
|
||||||
return data.size();
|
return data.size();
|
||||||
}
|
}
|
||||||
@ -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);
|
||||||
|
|||||||
@ -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;
|
||||||
|
|
||||||
@ -32,6 +31,14 @@ public class TableHeader {
|
|||||||
colSizes.add(colSize);
|
colSizes.add(colSize);
|
||||||
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();
|
||||||
@ -44,5 +51,7 @@ public class TableHeader {
|
|||||||
public Text getHeader(int pos) {
|
public Text getHeader(int pos) {
|
||||||
return headers.get(pos);
|
return headers.get(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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() {
|
||||||
@ -36,5 +52,14 @@ 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,11 @@
|
|||||||
|
package de.muehlencord.shared.pdf;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author joern.muehlencord
|
||||||
|
*/
|
||||||
|
public enum TextAlignment {
|
||||||
|
|
||||||
|
LEFT,
|
||||||
|
RIGHT;
|
||||||
|
}
|
||||||
@ -43,29 +43,42 @@ 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());
|
||||||
cos.newLine();
|
cos.newLine();
|
||||||
}
|
}
|
||||||
cos.endText();
|
cos.endText();
|
||||||
|
|
||||||
return new Coordinate (x, currentY);
|
return new Coordinate(x, currentY);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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");
|
||||||
|
|||||||
Reference in New Issue
Block a user