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,8 +4,6 @@ import com.google.gson.annotations.Expose;
import java.io.IOException; import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle; 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; protected PDFDocument document;
@Expose @Expose
protected int x; protected Integer x;
@Expose @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) { public Content(PDFDocument doc, int x, int y) {
this.document = doc; this.document = doc;
@ -27,11 +31,20 @@ public abstract class Content {
this.y = y; this.y = y;
} }
public void setDocument (PDFDocument doc) { public void setDocument(PDFDocument doc) {
this.document = 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 *** */ /* *** getter / setter *** */
public int getX() { public int getX() {

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(); 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;
for (Content content : pdfDoc.getContentList()) { for (Content content : pdfDoc.getContentList()) {
content.setDocument (pdfDoc); // FIXME move to serialization content.setDocument (pdfDoc); // FIXME move to serialization
content.addContentToPdf(rect, cos); content.setCoordinate (coord);
coord = content.addContentToPdf(rect, cos);
} }
cos.close(); cos.close();
doc.save(filenName); doc.save(filenName);

View File

@ -26,6 +26,11 @@ public class TableContent extends Content {
@Expose @Expose
private List<List<Text>> data = null; 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) { public TableContent(PDFDocument doc, Font hf, int x, int y) {
super(doc, x, y); super(doc, x, y);
this.headerFont = hf; this.headerFont = hf;
@ -58,7 +63,7 @@ public class TableContent extends Content {
} }
@Override @Override
protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException { protected Coordinate addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
cos.beginText(); cos.beginText();
PDFont hFont = document.getFont(headerFont.getFontName()); PDFont hFont = document.getFont(headerFont.getFontName());
@ -68,25 +73,29 @@ public class TableContent extends Content {
xOffSet -= colSizes.get(i); xOffSet -= colSizes.get(i);
} }
int yOffset = document.getStandardFont().getFontSize() * -1 - document.getStandardFont().getPadding(); int yOffset = document.getStandardFont().getFontSize() * -1 - document.getStandardFont().getPadding();
int currentX = x;
int currentY = y;
cos.setFont(hFont, headerFont.getFontSize()); cos.setFont(hFont, headerFont.getFontSize());
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.get(i).getText()); cos.showText(header.get(i).getText());
cos.newLineAtOffset(colSizes.get(i), 0); cos.newLineAtOffset(colSizes.get(i), 0);
} }
cos.setFont(standardFont, document.getStandardFont().getFontSize()); cos.setFont(standardFont, document.getStandardFont().getFontSize());
for (int lineNo = 0; lineNo < data.size(); lineNo++) { for (int lineNo = 0; lineNo < data.size(); lineNo++) {
List<Text> currentRow = data.get(lineNo); List<Text> currentRow = data.get(lineNo);
cos.newLineAtOffset(xOffSet, yOffset); cos.newLineAtOffset(xOffSet, yOffset);
currentY += yOffset;
for (int colNo = 0; colNo < currentRow.size(); colNo++) { for (int colNo = 0; colNo < currentRow.size(); colNo++) {
cos.showText(currentRow.get(colNo).getText()); cos.showText(currentRow.get(colNo).getText());
cos.newLineAtOffset(colSizes.get(colNo), 0); cos.newLineAtOffset(colSizes.get(colNo), 0);
} }
} }
cos.endText(); cos.endText();
return new Coordinate(currentX, currentY);
} }
/* *** getter / setter *** */ /* *** getter / setter *** */

View File

@ -18,7 +18,8 @@ public class TextContent extends Content {
private final List<Text> textLines; private final List<Text> textLines;
public TextContent(PDFDocument doc) { public TextContent(PDFDocument doc) {
this(doc, -1, -1); super(doc);
this.textLines = new LinkedList<>();
} }
public TextContent(PDFDocument doc, int x, int y) { public TextContent(PDFDocument doc, int x, int y) {
@ -49,17 +50,22 @@ public class TextContent extends Content {
/* *** getter / setter */ /* *** getter / setter */
@Override @Override
protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException { protected Coordinate addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
cos.beginText(); cos.beginText();
cos.newLineAtOffset(x, y); cos.newLineAtOffset(x, 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());
cos.setLeading(font.getFontSize() + font.getPadding()); int leading = font.getFontSize() + font.getPadding();
currentY -= 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);
} }
} }

View File

@ -44,12 +44,6 @@ public class PDFDocumentTest {
.addLine("12345 Musterhausen"); .addLine("12345 Musterhausen");
doc.addContent(addressContent); 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); TextContent informationContent = new TextContent(doc, 400, 662);
informationContent.addLine("Anzeigenabrechnung", "bold"); informationContent.addLine("Anzeigenabrechnung", "bold");
informationContent.addLine("Veronika Mühlencord", "helv12"); informationContent.addLine("Veronika Mühlencord", "helv12");
@ -59,8 +53,7 @@ public class PDFDocumentTest {
informationContent.addLine("Hövelhof, den ${invoiceDate?date}", "bold"); informationContent.addLine("Hövelhof, den ${invoiceDate?date}", "bold");
doc.addContent(informationContent); doc.addContent(informationContent);
TableContent informationContent2 = new TableContent (doc, doc.getStandardFont());
TableContent informationContent2 = new TableContent (doc, doc.getStandardFont(), 400, 580);
List<String> headers = new ArrayList<>(); List<String> headers = new ArrayList<>();
headers.add("Kunden-Nr"); headers.add("Kunden-Nr");
headers.add("${customerNumber}"); headers.add("${customerNumber}");
@ -73,6 +66,17 @@ public class PDFDocumentTest {
informationContent2.addLine ("Rechnungsdatum:", "${invoiceDate?date}"); informationContent2.addLine ("Rechnungsdatum:", "${invoiceDate?date}");
doc.addContent(informationContent2); 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); jsonString = gson.toJson(doc);
System.out.println(jsonString); System.out.println(jsonString);