added mutli font support

This commit is contained in:
jomu
2016-05-03 23:10:08 +00:00
parent 4837c4b810
commit 238d46c118
10 changed files with 147 additions and 111 deletions

View File

@ -2,8 +2,6 @@ package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import java.io.IOException; import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
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.PDFont;
@ -14,43 +12,27 @@ import org.apache.pdfbox.pdmodel.font.PDType1Font;
* @author jomu * @author jomu
*/ */
public abstract class Content { public abstract class Content {
protected PDFDocument document;
@Expose @Expose
protected int x; protected int x;
@Expose @Expose
protected int y; protected int y;
@Expose public Content(PDFDocument doc, int x, int y) {
protected Font standardFont; this.document = doc;
@Expose
protected Map<String, Font> fontMap;
public Content(int x, int y) {
this.standardFont = new Font("Helvetica", 11);
this.x = x; this.x = x;
this.y = y; this.y = y;
this.fontMap = null;
} }
public void addFont(String name, Font font) { public void setDocument (PDFDocument doc) {
if (fontMap == null) { this.document = doc;
fontMap = new ConcurrentHashMap<>();
}
fontMap.put(name, font);
} }
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; protected abstract void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException;
/* *** getter / setter *** */ /* *** getter / setter *** */
public int getX() { public int getX() {
return x; return x;
@ -68,12 +50,4 @@ public abstract class Content {
this.y = y; this.y = y;
} }
public Font getStandardFont() {
return standardFont;
}
public void setStandardFont(Font standardFont) {
this.standardFont = standardFont;
}
} }

View File

@ -1,13 +1,18 @@
package de.muehlencord.shared.pdf; package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
/** /**
* *
* @author jomu * @author jomu
*/ */
public class Font { public class Font {
@Expose
private String fontName; private String fontName;
@Expose
private int fontSize; private int fontSize;
@Expose
private int padding; private int padding;
public Font(String fontName, int fontSize) { public Font(String fontName, int fontSize) {

View File

@ -12,6 +12,7 @@ public class GsonUtil {
public final static Gson getGsonInstance() { public final static Gson getGsonInstance() {
return new GsonBuilder() return new GsonBuilder()
.setPrettyPrinting() .setPrettyPrinting()
.excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(Content.class, new InterfaceAdapter<>()) .registerTypeAdapter(Content.class, new InterfaceAdapter<>())
.create(); .create();
} }

View File

@ -3,30 +3,68 @@ package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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 * @author jomu
*/ */
public class PDFDocument { public class PDFDocument {
@Expose @Expose
private PaperSize paperSize; private PaperSize paperSize;
@Expose @Expose
private final List<Content> contentList; protected Font standardFont;
@Expose
protected Map<String, Font> fontMap;
@Expose
private final List<Content> contentList;
public PDFDocument() { public PDFDocument() {
this.contentList = new ArrayList<>(); this.contentList = new ArrayList<>();
this.standardFont = new Font("Helvetica", 11);
this.fontMap = null;
} }
public PDFDocument addContent (Content content) { public void addFont(String name, Font font) {
contentList.add (content); if (fontMap == null) {
return this; 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() { public PaperSize getPaperSize() {
return paperSize; return paperSize;
} }
@ -35,9 +73,16 @@ public class PDFDocument {
this.paperSize = paperSize; this.paperSize = paperSize;
} }
public Font getStandardFont() {
return standardFont;
}
public void setStandardFont(Font standardFont) {
this.standardFont = standardFont;
}
public List<Content> getContentList() { public List<Content> getContentList() {
return contentList; return contentList;
} }
} }

View File

@ -1,7 +1,6 @@
package de.muehlencord.shared.pdf; package de.muehlencord.shared.pdf;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import freemarker.template.Template; import freemarker.template.Template;
import freemarker.template.TemplateException; import freemarker.template.TemplateException;
import java.io.IOException; import java.io.IOException;
@ -62,6 +61,7 @@ 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);
for (Content content : pdfDoc.getContentList()) { for (Content content : pdfDoc.getContentList()) {
content.setDocument (pdfDoc); // FIXME move to serialization
content.addContentToPdf(rect, cos); content.addContentToPdf(rect, cos);
} }
cos.close(); cos.close();

View File

@ -21,14 +21,13 @@ public class TableContent extends Content {
@Expose @Expose
List<List<Object>> data; List<List<Object>> data;
public TableContent(int x, int y) { public TableContent(PDFDocument doc, int x, int y) {
super(x, y); super(doc, x, y);
} }
@Override @Override
protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException { protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
cos.beginText(); cos.beginText();
cos.setFont(getFont(headerFont.getFontName()), headerFont.getFontSize());
cos.newLineAtOffset(x,y); cos.newLineAtOffset(x,y);
} }

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

View File

@ -1,5 +1,6 @@
package de.muehlencord.shared.pdf; package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -13,51 +14,50 @@ import org.apache.pdfbox.pdmodel.font.PDFont;
*/ */
public class TextContent extends Content { public class TextContent extends Content {
private final List<String> textLines; @Expose
private final List<Text> textLines;
public TextContent() { public TextContent(PDFDocument doc) {
this(-1, -1); this(doc, -1, -1);
} }
public TextContent(int x, int y) { public TextContent(PDFDocument doc, int x, int y) {
super(x, y); super(doc, x, y);
this.textLines = new LinkedList<>(); this.textLines = new LinkedList<>();
} }
public TextContent(int x, int y, String text) { public TextContent(PDFDocument doc, int x, int y, String text) {
super(x, y); super(doc, x, y);
this.textLines = new LinkedList<>(); this.textLines = new LinkedList<>();
this.textLines.add(text); this.textLines.add(new Text(text));
} }
public TextContent addLine() { public TextContent addLine() {
this.textLines.add(""); this.textLines.add(new Text());
return this; return this;
} }
public TextContent addLine(String text) { public TextContent addLine(String text) {
this.textLines.add(text); this.textLines.add(new Text(text));
return this; return this;
} }
public TextContent addLine(String text, String fontShortcut) { public TextContent addLine(String text, String fontAlias) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. this.textLines.add (new Text(text, fontAlias));
return this;
} }
/* *** getter / setter */ /* *** getter / setter */
public List<String> getTextLines() {
return textLines;
}
@Override @Override
protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException { protected void addContentToPdf(PDRectangle rect, PDPageContentStream cos) throws IOException, ConfigurationException {
PDFont font = getFont(standardFont.getFontName());
cos.beginText(); cos.beginText();
cos.setFont(font, standardFont.getFontSize());
cos.setLeading(standardFont.getFontSize() + standardFont.getPadding());
cos.newLineAtOffset(x, y); cos.newLineAtOffset(x, y);
for (String line : textLines) { for (Text textLine : textLines) {
cos.showText(line); 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.newLine();
} }
cos.endText(); cos.endText();

View File

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

View File

@ -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; package de.muehlencord.shared.pdf;
import com.google.gson.Gson; 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.BeforeClass;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*;
/** /**
* *
@ -31,21 +21,27 @@ public class PDFDocumentTest {
public void testToJson() { public void testToJson() {
System.out.println("testToJson"); System.out.println("testToJson");
PDFDocument doc = new PDFDocument(); 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); 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("Musterstraße 123")
.addLine("12345 Musterhausen"); .addLine("12345 Musterhausen");
doc.addContent(addressContent); 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() .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");
doc.addContent(invoiceInfoInformation); doc.addContent(invoiceInfoInformation);
TextContent informationContent = new TextContent(400, 662); TextContent informationContent = new TextContent(doc, 400, 662);
informationContent.addFont("bold", new Font("Helvetica-Bold", 12, 2)); informationContent.addLine ("Anzeigenabrechnung", "bold");
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)); System.out.println(gson.toJson(doc));
} }