added mutli font support
This commit is contained in:
@ -2,8 +2,6 @@ package de.muehlencord.shared.pdf;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
||||
import org.apache.pdfbox.pdmodel.common.PDRectangle;
|
||||
import org.apache.pdfbox.pdmodel.font.PDFont;
|
||||
@ -14,43 +12,27 @@ import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
||||
* @author jomu
|
||||
*/
|
||||
public abstract class Content {
|
||||
|
||||
protected PDFDocument document;
|
||||
|
||||
@Expose
|
||||
protected int x;
|
||||
|
||||
@Expose
|
||||
protected int y;
|
||||
|
||||
@Expose
|
||||
protected Font standardFont;
|
||||
|
||||
@Expose
|
||||
protected Map<String, Font> fontMap;
|
||||
|
||||
public Content(int x, int y) {
|
||||
this.standardFont = new Font("Helvetica", 11);
|
||||
|
||||
public Content(PDFDocument doc, int x, int y) {
|
||||
this.document = doc;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.fontMap = null;
|
||||
}
|
||||
|
||||
public void addFont(String name, Font font) {
|
||||
if (fontMap == null) {
|
||||
fontMap = new ConcurrentHashMap<>();
|
||||
}
|
||||
fontMap.put(name, font);
|
||||
|
||||
public void setDocument (PDFDocument doc) {
|
||||
this.document = doc;
|
||||
}
|
||||
|
||||
protected PDFont getFont(String fontName) throws ConfigurationException {
|
||||
if (fontName.equals(PDType1Font.HELVETICA.getBaseFont())) {
|
||||
return PDType1Font.HELVETICA;
|
||||
} else {
|
||||
throw new ConfigurationException("Font " + fontName + " not supported");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected abstract void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException;
|
||||
|
||||
|
||||
/* *** getter / setter *** */
|
||||
public int getX() {
|
||||
return x;
|
||||
@ -68,12 +50,4 @@ public abstract class Content {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public Font getStandardFont() {
|
||||
return standardFont;
|
||||
}
|
||||
|
||||
public void setStandardFont(Font standardFont) {
|
||||
this.standardFont = standardFont;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,13 +1,18 @@
|
||||
package de.muehlencord.shared.pdf;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jomu
|
||||
*/
|
||||
public class Font {
|
||||
|
||||
@Expose
|
||||
private String fontName;
|
||||
@Expose
|
||||
private int fontSize;
|
||||
@Expose
|
||||
private int padding;
|
||||
|
||||
public Font(String fontName, int fontSize) {
|
||||
|
||||
@ -12,6 +12,7 @@ public class GsonUtil {
|
||||
public final static Gson getGsonInstance() {
|
||||
return new GsonBuilder()
|
||||
.setPrettyPrinting()
|
||||
.excludeFieldsWithoutExposeAnnotation()
|
||||
.registerTypeAdapter(Content.class, new InterfaceAdapter<>())
|
||||
.create();
|
||||
}
|
||||
|
||||
@ -3,30 +3,68 @@ package de.muehlencord.shared.pdf;
|
||||
import com.google.gson.annotations.Expose;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import org.apache.pdfbox.pdmodel.font.PDFont;
|
||||
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jomu
|
||||
*/
|
||||
public class PDFDocument {
|
||||
|
||||
|
||||
@Expose
|
||||
private PaperSize paperSize;
|
||||
|
||||
|
||||
@Expose
|
||||
private final List<Content> contentList;
|
||||
protected Font standardFont;
|
||||
|
||||
@Expose
|
||||
protected Map<String, Font> fontMap;
|
||||
|
||||
@Expose
|
||||
private final List<Content> contentList;
|
||||
|
||||
public PDFDocument() {
|
||||
this.contentList = new ArrayList<>();
|
||||
this.standardFont = new Font("Helvetica", 11);
|
||||
this.fontMap = null;
|
||||
}
|
||||
|
||||
public PDFDocument addContent (Content content) {
|
||||
contentList.add (content);
|
||||
return this;
|
||||
|
||||
public void addFont(String name, Font font) {
|
||||
if (fontMap == null) {
|
||||
fontMap = new ConcurrentHashMap<>();
|
||||
}
|
||||
fontMap.put(name, font);
|
||||
}
|
||||
|
||||
protected PDFont getFont(String fontName) throws ConfigurationException {
|
||||
if (fontName.equals(PDType1Font.HELVETICA.getBaseFont())) {
|
||||
return PDType1Font.HELVETICA;
|
||||
} else if (fontName.equals(PDType1Font.HELVETICA_BOLD.getBaseFont())) {
|
||||
return PDType1Font.HELVETICA_BOLD;
|
||||
}else {
|
||||
throw new ConfigurationException("Font " + fontName + " not supported");
|
||||
}
|
||||
}
|
||||
|
||||
/* *** getter / setter *** */
|
||||
public Font getFontByAlias(String fontAlias) throws ConfigurationException {
|
||||
if ((fontMap != null) && (fontMap.containsKey(fontAlias))) {
|
||||
return fontMap.get(fontAlias);
|
||||
} else {
|
||||
throw new ConfigurationException("Font " + fontAlias + " not found in mapping. ");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public PDFDocument addContent(Content content) {
|
||||
contentList.add(content);
|
||||
return this;
|
||||
}
|
||||
|
||||
/* *** getter / setter *** */
|
||||
public PaperSize getPaperSize() {
|
||||
return paperSize;
|
||||
}
|
||||
@ -35,9 +73,16 @@ public class PDFDocument {
|
||||
this.paperSize = paperSize;
|
||||
}
|
||||
|
||||
public Font getStandardFont() {
|
||||
return standardFont;
|
||||
}
|
||||
|
||||
public void setStandardFont(Font standardFont) {
|
||||
this.standardFont = standardFont;
|
||||
}
|
||||
|
||||
public List<Content> getContentList() {
|
||||
return contentList;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
package de.muehlencord.shared.pdf;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import freemarker.template.Template;
|
||||
import freemarker.template.TemplateException;
|
||||
import java.io.IOException;
|
||||
@ -62,6 +61,7 @@ public class PDFTemplate {
|
||||
PDRectangle rect = page.getMediaBox();
|
||||
PDPageContentStream cos = new PDPageContentStream(doc, page, AppendMode.APPEND, false);
|
||||
for (Content content : pdfDoc.getContentList()) {
|
||||
content.setDocument (pdfDoc); // FIXME move to serialization
|
||||
content.addContentToPdf(rect, cos);
|
||||
}
|
||||
cos.close();
|
||||
|
||||
@ -21,14 +21,13 @@ public class TableContent extends Content {
|
||||
@Expose
|
||||
List<List<Object>> data;
|
||||
|
||||
public TableContent(int x, int y) {
|
||||
super(x, y);
|
||||
public TableContent(PDFDocument doc, int x, int y) {
|
||||
super(doc, x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
|
||||
protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
|
||||
cos.beginText();
|
||||
cos.setFont(getFont(headerFont.getFontName()), headerFont.getFontSize());
|
||||
cos.newLineAtOffset(x,y);
|
||||
}
|
||||
|
||||
|
||||
40
pdf/src/main/java/de/muehlencord/shared/pdf/Text.java
Normal file
40
pdf/src/main/java/de/muehlencord/shared/pdf/Text.java
Normal file
@ -0,0 +1,40 @@
|
||||
package de.muehlencord.shared.pdf;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author joern.muehlencord
|
||||
*/
|
||||
public class Text {
|
||||
|
||||
@Expose
|
||||
private final String text;
|
||||
@Expose
|
||||
private final String fontAlias;
|
||||
|
||||
public Text() {
|
||||
this.text = "";
|
||||
this.fontAlias = null;
|
||||
}
|
||||
|
||||
public Text(String text) {
|
||||
this.text = text;
|
||||
this.fontAlias = null;
|
||||
}
|
||||
|
||||
public Text(String text, String fontAlias) {
|
||||
this.text = text;
|
||||
this.fontAlias = fontAlias;
|
||||
}
|
||||
|
||||
|
||||
/* *** getter / setter *** */
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public String getFontAlias() {
|
||||
return fontAlias;
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
package de.muehlencord.shared.pdf;
|
||||
|
||||
import com.google.gson.annotations.Expose;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@ -13,51 +14,50 @@ import org.apache.pdfbox.pdmodel.font.PDFont;
|
||||
*/
|
||||
public class TextContent extends Content {
|
||||
|
||||
private final List<String> textLines;
|
||||
@Expose
|
||||
private final List<Text> textLines;
|
||||
|
||||
public TextContent() {
|
||||
this(-1, -1);
|
||||
public TextContent(PDFDocument doc) {
|
||||
this(doc, -1, -1);
|
||||
}
|
||||
|
||||
public TextContent(int x, int y) {
|
||||
super(x, y);
|
||||
public TextContent(PDFDocument doc, int x, int y) {
|
||||
super(doc, x, y);
|
||||
this.textLines = new LinkedList<>();
|
||||
}
|
||||
|
||||
public TextContent(int x, int y, String text) {
|
||||
super(x, y);
|
||||
public TextContent(PDFDocument doc, int x, int y, String text) {
|
||||
super(doc, x, y);
|
||||
this.textLines = new LinkedList<>();
|
||||
this.textLines.add(text);
|
||||
this.textLines.add(new Text(text));
|
||||
}
|
||||
|
||||
public TextContent addLine() {
|
||||
this.textLines.add("");
|
||||
this.textLines.add(new Text());
|
||||
return this;
|
||||
}
|
||||
|
||||
public TextContent addLine(String text) {
|
||||
this.textLines.add(text);
|
||||
this.textLines.add(new Text(text));
|
||||
return this;
|
||||
}
|
||||
|
||||
public TextContent addLine(String text, String fontShortcut) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
public TextContent addLine(String text, String fontAlias) {
|
||||
this.textLines.add (new Text(text, fontAlias));
|
||||
return this;
|
||||
}
|
||||
|
||||
/* *** getter / setter */
|
||||
public List<String> getTextLines() {
|
||||
return textLines;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
|
||||
PDFont font = getFont(standardFont.getFontName());
|
||||
cos.beginText();
|
||||
cos.setFont(font, standardFont.getFontSize());
|
||||
cos.setLeading(standardFont.getFontSize() + standardFont.getPadding());
|
||||
cos.newLineAtOffset(x, y);
|
||||
for (String line : textLines) {
|
||||
cos.showText(line);
|
||||
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());
|
||||
cos.showText(textLine.getText());
|
||||
cos.newLine();
|
||||
}
|
||||
cos.endText();
|
||||
|
||||
@ -1,24 +0,0 @@
|
||||
package de.muehlencord.shared.pdf;
|
||||
|
||||
import org.apache.pdfbox.pdmodel.font.PDFont;
|
||||
import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jomu
|
||||
*/
|
||||
public class ContentTest {
|
||||
|
||||
public ContentTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFont() throws ConfigurationException {
|
||||
TextContent textContent = new TextContent(1,1,"Helvetica");
|
||||
PDFont font = textContent.getFont("Helvetica");
|
||||
assertEquals (PDType1Font.HELVETICA, font);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,18 +1,8 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package de.muehlencord.shared.pdf;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
*
|
||||
@ -31,21 +21,27 @@ public class PDFDocumentTest {
|
||||
public void testToJson() {
|
||||
System.out.println("testToJson");
|
||||
PDFDocument doc = new PDFDocument();
|
||||
doc.addFont("bold", new Font("Helvetica-Bold", 12, 2));
|
||||
doc.addFont("helv12", new Font("Helvetica", 12, 2));
|
||||
|
||||
doc.setPaperSize(PaperSize.A4);
|
||||
TextContent addressContent = new TextContent(40, 692, "Max Mustermann")
|
||||
TextContent addressContent = new TextContent(doc, 40, 692, "Max Mustermann")
|
||||
.addLine("Musterstraße 123")
|
||||
.addLine("12345 Musterhausen");
|
||||
doc.addContent(addressContent);
|
||||
|
||||
TextContent invoiceInfoInformation = new TextContent(40, 442, "Sehr geehrter Anzeigenkunde, ")
|
||||
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(400, 662);
|
||||
informationContent.addFont("bold", new Font("Helvetica-Bold", 12, 2));
|
||||
informationContent.addLine ("Anzeigenabrechnung", "bold");
|
||||
TextContent informationContent = new TextContent(doc, 400, 662);
|
||||
informationContent.addLine ("Anzeigenabrechnung", "bold");
|
||||
informationContent.addLine ("Veronika Mühlencord", "helv12");
|
||||
informationContent.addLine ("Telefon: 05257/940154", "helv12");
|
||||
informationContent.addLine ("Telefax: 05257/940156", "helv12");
|
||||
doc.addContent (informationContent);
|
||||
|
||||
System.out.println(gson.toJson(doc));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user