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

View File

@ -6,19 +6,19 @@ package de.muehlencord.shared.pdf;
*/
public class Coordinate {
final int x;
final int y;
final float x;
final float y;
public Coordinate(int x, int y) {
public Coordinate(float x, float y) {
this.x = x;
this.y = y;
}
public int getX() {
public float getX() {
return x;
}
public int getY() {
public float getY() {
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.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
@ -26,6 +27,8 @@ public class PDFDocument {
@Expose
private final List<Content> contentList;
private PDDocument pdDocument;
public PDFDocument() {
this.contentList = new ArrayList<>();
@ -163,4 +166,13 @@ public class PDFDocument {
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();
PDFDocument pdfDoc = gson.fromJson(json, PDFDocument.class);
PDDocument doc = new PDDocument();
pdfDoc.setPdDocument(new PDDocument());
PDPage page;
switch (pdfDoc.getPaperSize()) {
@ -62,18 +61,18 @@ public class PDFTemplate {
default:
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;
for (Content content : pdfDoc.getContentList()) {
content.setDocument(pdfDoc); // FIXME move to serialization
content.setDocument(pdfDoc); // FIXME move to serialization
content.setRect (page.getMediaBox());
content.setCoordinate(coord);
coord = content.addContentToPdf(cos);
}
cos.close();
doc.save(filenName);
pdfDoc.getPdDocument().save(filenName);
}
void applyTemplate(Template template) {

View File

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

View File

@ -21,12 +21,12 @@ public class TextContent extends Content {
this.textLines = new LinkedList<>();
}
public TextContent(PDFDocument doc, int x, int y) {
public TextContent(PDFDocument doc, Float x, Float y) {
super(doc, x, y);
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);
this.textLines = new LinkedList<>();
this.textLines.add(new Text(text));
@ -58,7 +58,7 @@ public class TextContent extends Content {
float margin = 40F;
cos.beginText();
cos.newLineAtOffset(x, y);
int currentY = y;
float currentY = y;
for (Text textLine : textLines) {
Font font = (textLine.getFontAlias() == null ? document.getStandardFont() : document.getFontByAlias(textLine.getFontAlias()));
PDFont pdFont = document.getFont(font.getFontName());

View File

@ -28,12 +28,15 @@ public class PDFDocumentTest {
doc.addFont("helv12", new Font("Helvetica", 12, 2));
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("12345 Musterhausen");
doc.addContent(addressContent);
ImageContent logoContent = new ImageContent (doc, 400F,700F, 0.6F);
doc.addContent (logoContent);
TextContent informationContent = new TextContent(doc, 400, 662);
TextContent informationContent = new TextContent(doc, 400F, 662F);
informationContent.addLine("Anzeigenabrechnung", "bold");
informationContent.addLine("Veronika Mühlencord", "helv12");
informationContent.addLine("Telefon: 05257/940154", "helv12");
@ -51,7 +54,7 @@ public class PDFDocumentTest {
informationContent2.addLine("Rechnungsdatum:", "${invoiceDate?date}");
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("Wir danken für den Auftrag und bitten um Erledigung der folgenden Anzeigenabrechnung")