added support for floating content positions

This commit is contained in:
jomu
2016-05-19 23:45:53 +00:00
parent 36de01c8db
commit 8aa33a0262
6 changed files with 88 additions and 30 deletions

View File

@ -4,35 +4,48 @@ 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;
/**
*
* @author jomu
*/
public abstract class Content {
protected PDFDocument document;
@Expose
protected int x;
protected Integer x;
@Expose
protected int y;
public Content(PDFDocument doc, int x, 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;
this.x = x;
this.y = y;
}
public void setDocument (PDFDocument doc) {
public void setDocument(PDFDocument doc) {
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() {
return x;

View 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;
}
}

View File

@ -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);

View File

@ -25,6 +25,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);
@ -58,9 +63,9 @@ 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());
PDFont standardFont = document.getFont(document.getStandardFont().getFontName());
int xOffSet = 0;
@ -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();
cos.endText();
return new Coordinate(currentX, currentY);
}
/* *** getter / setter *** */

View File

@ -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);
}
}

View File

@ -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");
@ -58,9 +52,8 @@ public class PDFDocumentTest {
informationContent.addLine();
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}");
@ -72,6 +65,17 @@ public class PDFDocumentTest {
informationContent2.addLine ("Ausgabe: ", "Dezember");
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);