improved image handling

This commit is contained in:
jomu
2016-06-14 22:45:06 +00:00
parent d33ff848cf
commit dddb41c130
2 changed files with 71 additions and 9 deletions

View File

@ -1,8 +1,16 @@
package de.muehlencord.shared.pdf; package de.muehlencord.shared.pdf;
import com.google.gson.annotations.Expose; import com.google.gson.annotations.Expose;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.LosslessFactory;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
/** /**
@ -13,29 +21,79 @@ public class ImageContent extends Content {
@Expose @Expose
private Float scale = null; private Float scale = null;
@Expose
public ImageContent(PDFDocument document) { private String base64CodedImage = null;
public ImageContent(PDFDocument document, String contentString) throws IOException {
super(document); super(document);
this.base64CodedImage = contentString;
}
public ImageContent(PDFDocument document, File file) throws IOException {
super(document);
this.base64CodedImage = getEncodedString(ImageIO.read(file));
} }
public ImageContent(PDFDocument document, Float x, Float y) { public ImageContent(PDFDocument document, BufferedImage img) throws IOException {
super(document);
this.base64CodedImage = getEncodedString(img);
}
public ImageContent(PDFDocument document, Float x, Float y, String contentString) throws IOException {
super(document, x, y); super(document, x, y);
this.base64CodedImage = contentString;
}
public ImageContent(PDFDocument document, Float x, Float y, File file) throws IOException {
super(document, x, y);
this.base64CodedImage = getEncodedString(ImageIO.read(file));
} }
public ImageContent(PDFDocument document, Float x, Float y, Float scale) { public ImageContent(PDFDocument document, Float x, Float y, BufferedImage img) throws IOException {
super(document, x, y);
this.base64CodedImage = getEncodedString(img);
}
public ImageContent(PDFDocument document, Float x, Float y, Float scale, String contentString) throws IOException {
super(document, x, y); super(document, x, y);
this.scale = scale; this.scale = scale;
this.base64CodedImage = contentString;
}
public ImageContent(PDFDocument document, Float x, Float y, Float scale, File file) throws IOException {
super(document, x, y);
this.scale = scale;
this.base64CodedImage = getEncodedString(ImageIO.read(file));
}
public ImageContent(PDFDocument document, Float x, Float y, Float scale, BufferedImage img) throws IOException {
super(document, x, y);
this.scale = scale;
this.base64CodedImage = getEncodedString(img);
} }
@Override @Override
protected Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException { protected Coordinate addContentToPdf(PDPageContentStream cos) throws IOException, ConfigurationException {
PDImageXObject pdImage = PDImageXObject.createFromFile("c:/temp/logo-verkehrsverein-hoevelh.jpg", document.getPdDocument()); BufferedImage image = getImage();
PDImageXObject pdImage = LosslessFactory.createFromImage(document.getPdDocument(), image);
float height = pdImage.getHeight() * getScale(); float height = pdImage.getHeight() * getScale();
float width = pdImage.getWidth() * getScale(); float width = pdImage.getWidth() * getScale();
cos.drawImage(pdImage, x, y, width, height); cos.drawImage(pdImage, x, y, width, height);
return new Coordinate(x, y - height); return new Coordinate(x, y - height);
} }
private String getEncodedString(BufferedImage img) throws IOException {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(img, "png", Base64.getEncoder().wrap(os));
return os.toString(StandardCharsets.UTF_8.name());
}
private BufferedImage getImage() throws IOException {
return ImageIO.read(new ByteArrayInputStream(Base64.getDecoder().decode(this.base64CodedImage)));
}
/* *** getter / setter */ /* *** getter / setter */
public Float getScale() { public Float getScale() {
if (scale == null) { if (scale == null) {
@ -44,5 +102,4 @@ public class ImageContent extends Content {
return scale; return scale;
} }
} }
} }

View File

@ -3,10 +3,12 @@ package de.muehlencord.shared.pdf;
import freemarker.template.Configuration; import freemarker.template.Configuration;
import freemarker.template.Template; import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler; import freemarker.template.TemplateExceptionHandler;
import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.Date; import java.util.Date;
import javax.imageio.ImageIO;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.junit.FixMethodOrder; import org.junit.FixMethodOrder;
import org.junit.Test; import org.junit.Test;
@ -32,9 +34,12 @@ public class PDFDocumentTest {
.addLine("Musterstraße 123") .addLine("Musterstraße 123")
.addLine("12345 Musterhausen"); .addLine("12345 Musterhausen");
doc.addContent(addressContent); doc.addContent(addressContent);
ImageContent logoContent = new ImageContent (doc, 400F,700F, 0.6F); String fileName = "c:/temp/logo-verkehrsverein-hoevelh.jpg";
doc.addContent (logoContent); BufferedImage image = ImageIO.read(new File(fileName));
ImageContent logoContent = new ImageContent(doc, 400F, 700F, 0.6F, image);
doc.addContent(logoContent);
TextContent informationContent = new TextContent(doc, 400F, 662F); TextContent informationContent = new TextContent(doc, 400F, 662F);
informationContent.addLine("Anzeigenabrechnung", "bold"); informationContent.addLine("Anzeigenabrechnung", "bold");