added support for floating content positions
This commit is contained in:
@ -4,8 +4,6 @@ import com.google.gson.annotations.Expose;
|
||||
import java.io.IOException;
|
||||
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.PDType1Font;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -16,10 +14,16 @@ public abstract class Content {
|
||||
protected PDFDocument document;
|
||||
|
||||
@Expose
|
||||
protected int x;
|
||||
protected Integer x;
|
||||
|
||||
@Expose
|
||||
protected int y;
|
||||
protected Integer y;
|
||||
|
||||
public Content(PDFDocument document) {
|
||||
this.document = document;
|
||||
this.x = null;
|
||||
this.y = null;
|
||||
}
|
||||
|
||||
public Content(PDFDocument doc, int x, int y) {
|
||||
this.document = doc;
|
||||
@ -31,7 +35,16 @@ public abstract class Content {
|
||||
this.document = doc;
|
||||
}
|
||||
|
||||
protected abstract void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException;
|
||||
public void setCoordinate(Coordinate coord) {
|
||||
if (x == null) {
|
||||
x = coord.getX();
|
||||
}
|
||||
if (y == null) {
|
||||
y = coord.getY();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Coordinate addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException;
|
||||
|
||||
/* *** getter / setter *** */
|
||||
public int getX() {
|
||||
|
||||
24
pdf/src/main/java/de/muehlencord/shared/pdf/Coordinate.java
Normal file
24
pdf/src/main/java/de/muehlencord/shared/pdf/Coordinate.java
Normal file
@ -0,0 +1,24 @@
|
||||
package de.muehlencord.shared.pdf;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class Coordinate {
|
||||
|
||||
final int x;
|
||||
final int y;
|
||||
|
||||
public Coordinate(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
}
|
||||
@ -64,9 +64,11 @@ public class PDFTemplate {
|
||||
|
||||
PDRectangle rect = page.getMediaBox();
|
||||
PDPageContentStream cos = new PDPageContentStream(doc, page, AppendMode.APPEND, false);
|
||||
Coordinate coord = null;
|
||||
for (Content content : pdfDoc.getContentList()) {
|
||||
content.setDocument (pdfDoc); // FIXME move to serialization
|
||||
content.addContentToPdf(rect, cos);
|
||||
content.setCoordinate (coord);
|
||||
coord = content.addContentToPdf(rect, cos);
|
||||
}
|
||||
cos.close();
|
||||
doc.save(filenName);
|
||||
|
||||
@ -26,6 +26,11 @@ public class TableContent extends Content {
|
||||
@Expose
|
||||
private List<List<Text>> data = null;
|
||||
|
||||
public TableContent (PDFDocument doc, Font hf) {
|
||||
super (doc);
|
||||
this.headerFont = hf;
|
||||
}
|
||||
|
||||
public TableContent(PDFDocument doc, Font hf, int x, int y) {
|
||||
super(doc, x, y);
|
||||
this.headerFont = hf;
|
||||
@ -58,7 +63,7 @@ public class TableContent extends Content {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
|
||||
protected Coordinate addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
|
||||
cos.beginText();
|
||||
|
||||
PDFont hFont = document.getFont(headerFont.getFontName());
|
||||
@ -68,25 +73,29 @@ public class TableContent extends Content {
|
||||
xOffSet -= colSizes.get(i);
|
||||
}
|
||||
int yOffset = document.getStandardFont().getFontSize() * -1 - document.getStandardFont().getPadding();
|
||||
int currentX = x;
|
||||
int currentY = y;
|
||||
|
||||
cos.setFont(hFont, headerFont.getFontSize());
|
||||
cos.newLineAtOffset(x, y);
|
||||
for (int i = 0; i < header.size(); i++) {
|
||||
cos.showText(header.get(i).getText());
|
||||
cos.newLineAtOffset(colSizes.get(i), 0);
|
||||
|
||||
}
|
||||
|
||||
cos.setFont(standardFont, document.getStandardFont().getFontSize());
|
||||
for (int lineNo = 0; lineNo < data.size(); lineNo++) {
|
||||
List<Text> currentRow = data.get(lineNo);
|
||||
cos.newLineAtOffset(xOffSet, yOffset);
|
||||
currentY += yOffset;
|
||||
for (int colNo = 0; colNo < currentRow.size(); colNo++) {
|
||||
cos.showText(currentRow.get(colNo).getText());
|
||||
cos.newLineAtOffset(colSizes.get(colNo), 0);
|
||||
}
|
||||
}
|
||||
cos.endText();
|
||||
|
||||
return new Coordinate(currentX, currentY);
|
||||
}
|
||||
|
||||
/* *** getter / setter *** */
|
||||
|
||||
@ -18,7 +18,8 @@ public class TextContent extends Content {
|
||||
private final List<Text> textLines;
|
||||
|
||||
public TextContent(PDFDocument doc) {
|
||||
this(doc, -1, -1);
|
||||
super(doc);
|
||||
this.textLines = new LinkedList<>();
|
||||
}
|
||||
|
||||
public TextContent(PDFDocument doc, int x, int y) {
|
||||
@ -49,17 +50,22 @@ public class TextContent extends Content {
|
||||
|
||||
/* *** getter / setter */
|
||||
@Override
|
||||
protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
|
||||
protected Coordinate addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
|
||||
cos.beginText();
|
||||
cos.newLineAtOffset(x, y);
|
||||
int currentY = y;
|
||||
for (Text textLine : textLines) {
|
||||
Font font = (textLine.getFontAlias()== null ? document.getStandardFont() : document.getFontByAlias(textLine.getFontAlias()));
|
||||
PDFont pdFont = document.getFont(font.getFontName());
|
||||
cos.setFont(pdFont, font.getFontSize());
|
||||
cos.setLeading(font.getFontSize() + font.getPadding());
|
||||
int leading = font.getFontSize() + font.getPadding();
|
||||
currentY -= leading;
|
||||
cos.setLeading(leading);
|
||||
cos.showText(textLine.getText());
|
||||
cos.newLine();
|
||||
}
|
||||
cos.endText();
|
||||
|
||||
return new Coordinate (x, currentY);
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,12 +44,6 @@ public class PDFDocumentTest {
|
||||
.addLine("12345 Musterhausen");
|
||||
doc.addContent(addressContent);
|
||||
|
||||
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");
|
||||
doc.addContent(invoiceInfoInformation);
|
||||
|
||||
TextContent informationContent = new TextContent(doc, 400, 662);
|
||||
informationContent.addLine("Anzeigenabrechnung", "bold");
|
||||
informationContent.addLine("Veronika Mühlencord", "helv12");
|
||||
@ -59,8 +53,7 @@ public class PDFDocumentTest {
|
||||
informationContent.addLine("Hövelhof, den ${invoiceDate?date}", "bold");
|
||||
doc.addContent(informationContent);
|
||||
|
||||
|
||||
TableContent informationContent2 = new TableContent (doc, doc.getStandardFont(), 400, 580);
|
||||
TableContent informationContent2 = new TableContent (doc, doc.getStandardFont());
|
||||
List<String> headers = new ArrayList<>();
|
||||
headers.add("Kunden-Nr");
|
||||
headers.add("${customerNumber}");
|
||||
@ -73,6 +66,17 @@ public class PDFDocumentTest {
|
||||
informationContent2.addLine ("Rechnungsdatum:", "${invoiceDate?date}");
|
||||
doc.addContent(informationContent2);
|
||||
|
||||
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");
|
||||
doc.addContent(invoiceInfoInformation);
|
||||
|
||||
TextContent test = new TextContent (doc)
|
||||
.addLine("Das ist ein Test");
|
||||
doc.addContent (test);
|
||||
|
||||
|
||||
jsonString = gson.toJson(doc);
|
||||
System.out.println(jsonString);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user