added basic imageContent support

This commit is contained in:
jomu
2016-06-07 11:54:37 +00:00
parent 90ef289d50
commit d33ff848cf
8 changed files with 88 additions and 26 deletions

View File

@ -15,10 +15,10 @@ public abstract class Content {
protected PDRectangle rect; protected PDRectangle rect;
@Expose @Expose
protected Integer x; protected Float x;
@Expose @Expose
protected Integer y; protected Float y;
public Content(PDFDocument document) { public Content(PDFDocument document) {
this.document = document; this.document = document;
@ -26,7 +26,7 @@ public abstract class Content {
this.y = null; this.y = null;
} }
public Content(PDFDocument doc, int x, int y) { public Content(PDFDocument doc, float x, float y) {
this(doc); this(doc);
this.x = x; this.x = x;
this.y = y; this.y = y;
@ -48,19 +48,19 @@ public abstract class Content {
protected abstract Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException; protected abstract Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException;
/* *** getter / setter *** */ /* *** getter / setter *** */
public int getX() { public float getX() {
return x; return x;
} }
public void setX(int x) { public void setX(float x) {
this.x = x; this.x = x;
} }
public int getY() { public float getY() {
return y; return y;
} }
public void setY(int y) { public void setY(float y) {
this.y = y; this.y = y;
} }

View File

@ -6,19 +6,19 @@ package de.muehlencord.shared.pdf;
*/ */
public class Coordinate { public class Coordinate {
final int x; final float x;
final int y; final float y;
public Coordinate(int x, int y) { public Coordinate(float x, float y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
public int getX() { public float getX() {
return x; return x;
} }
public int getY() { public float getY() {
return y; return y;
} }
} }

View File

@ -0,0 +1,48 @@
package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
import java.io.IOException;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
/**
*
* @author joern.muehlencord
*/
public class ImageContent extends Content {
@Expose
private Float scale = null;
public ImageContent(PDFDocument document) {
super(document);
}
public ImageContent(PDFDocument document, Float x, Float y) {
super(document, x, y);
}
public ImageContent(PDFDocument document, Float x, Float y, Float scale) {
super(document, x, y);
this.scale = scale;
}
@Override
protected Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException {
PDImageXObject pdImage = PDImageXObject.createFromFile("c:/temp/logo-verkehrsverein-hoevelh.jpg", document.getPdDocument());
float height = pdImage.getHeight() * getScale();
float width = pdImage.getWidth() * getScale();
cos.drawImage(pdImage, x, y, width, height);
return new Coordinate(x, y - height);
}
/* *** getter / setter */
public Float getScale() {
if (scale == null) {
return 1F;
} else {
return scale;
}
}
}

View File

@ -6,6 +6,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.ConcurrentLinkedDeque;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDFont; import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.apache.pdfbox.pdmodel.font.PDType1Font;
@ -27,6 +28,8 @@ public class PDFDocument {
@Expose @Expose
private final List<Content> contentList; private final List<Content> contentList;
private PDDocument pdDocument;
public PDFDocument() { public PDFDocument() {
this.contentList = new ArrayList<>(); this.contentList = new ArrayList<>();
this.standardFont = new Font("Helvetica", 11); this.standardFont = new Font("Helvetica", 11);
@ -163,4 +166,13 @@ public class PDFDocument {
return contentList; return contentList;
} }
public PDDocument getPdDocument() {
return pdDocument;
}
public void setPdDocument(PDDocument pdDocument) {
this.pdDocument = pdDocument;
}
} }

View File

@ -51,8 +51,7 @@ public class PDFTemplate {
Gson gson = GsonUtil.getGsonInstance(); Gson gson = GsonUtil.getGsonInstance();
PDFDocument pdfDoc = gson.fromJson(json, PDFDocument.class); PDFDocument pdfDoc = gson.fromJson(json, PDFDocument.class);
pdfDoc.setPdDocument(new PDDocument());
PDDocument doc = new PDDocument();
PDPage page; PDPage page;
switch (pdfDoc.getPaperSize()) { switch (pdfDoc.getPaperSize()) {
@ -62,9 +61,9 @@ public class PDFTemplate {
default: default:
throw new ConfigurationException("Papersize " + pdfDoc.getPaperSize().getLabel() + " not supported"); throw new ConfigurationException("Papersize " + pdfDoc.getPaperSize().getLabel() + " not supported");
} }
doc.addPage(page); pdfDoc.getPdDocument().addPage(page);
PDPageContentStream cos = new PDPageContentStream(doc, page, AppendMode.APPEND, false); PDPageContentStream cos = new PDPageContentStream(pdfDoc.getPdDocument(), page, AppendMode.APPEND, false);
Coordinate coord = null; 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
@ -73,7 +72,7 @@ public class PDFTemplate {
coord = content.addContentToPdf(cos); coord = content.addContentToPdf(cos);
} }
cos.close(); cos.close();
doc.save(filenName); pdfDoc.getPdDocument().save(filenName);
} }
void applyTemplate(Template template) { void applyTemplate(Template template) {

View File

@ -87,8 +87,8 @@ public class TableContent extends Content {
xOffSet -= header.getColumnSize(i); xOffSet -= header.getColumnSize(i);
} }
int yOffset = document.getStandardFont().getFontSize() * -1 - document.getStandardFont().getPadding(); int yOffset = document.getStandardFont().getFontSize() * -1 - document.getStandardFont().getPadding();
int currentX = x; float currentX = x;
int currentY = y; float currentY = y;
cos.setFont(hFont, headerFont.getFontSize()); cos.setFont(hFont, headerFont.getFontSize());
cos.newLineAtOffset(x, y); cos.newLineAtOffset(x, y);

View File

@ -21,12 +21,12 @@ public class TextContent extends Content {
this.textLines = new LinkedList<>(); this.textLines = new LinkedList<>();
} }
public TextContent(PDFDocument doc, int x, int y) { public TextContent(PDFDocument doc, Float x, Float y) {
super(doc, x, y); super(doc, x, y);
this.textLines = new LinkedList<>(); this.textLines = new LinkedList<>();
} }
public TextContent(PDFDocument doc, int x, int y, String text) { public TextContent(PDFDocument doc, Float x, Float y, String text) {
super(doc, x, y); super(doc, x, y);
this.textLines = new LinkedList<>(); this.textLines = new LinkedList<>();
this.textLines.add(new Text(text)); this.textLines.add(new Text(text));
@ -58,7 +58,7 @@ public class TextContent extends Content {
float margin = 40F; float margin = 40F;
cos.beginText(); cos.beginText();
cos.newLineAtOffset(x, y); cos.newLineAtOffset(x, y);
int currentY = y; float 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());

View File

@ -28,12 +28,15 @@ public class PDFDocumentTest {
doc.addFont("helv12", new Font("Helvetica", 12, 2)); doc.addFont("helv12", new Font("Helvetica", 12, 2));
doc.setPaperSize(PaperSize.A4); doc.setPaperSize(PaperSize.A4);
TextContent addressContent = new TextContent(doc, 40, 692, "Max Mustermann") TextContent addressContent = new TextContent(doc, 40F, 692F, "Max Mustermann")
.addLine("Musterstraße 123") .addLine("Musterstraße 123")
.addLine("12345 Musterhausen"); .addLine("12345 Musterhausen");
doc.addContent(addressContent); doc.addContent(addressContent);
TextContent informationContent = new TextContent(doc, 400, 662); ImageContent logoContent = new ImageContent (doc, 400F,700F, 0.6F);
doc.addContent (logoContent);
TextContent informationContent = new TextContent(doc, 400F, 662F);
informationContent.addLine("Anzeigenabrechnung", "bold"); informationContent.addLine("Anzeigenabrechnung", "bold");
informationContent.addLine("Veronika Mühlencord", "helv12"); informationContent.addLine("Veronika Mühlencord", "helv12");
informationContent.addLine("Telefon: 05257/940154", "helv12"); informationContent.addLine("Telefon: 05257/940154", "helv12");
@ -51,7 +54,7 @@ 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, ") TextContent invoiceInfoInformation = new TextContent(doc, 40F, 442F, "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")