diff options
author | pdanner <pdanner@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c> | 2010-12-06 16:34:52 +0000 |
---|---|---|
committer | pdanner <pdanner@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c> | 2010-12-06 16:34:52 +0000 |
commit | 29ad090c29567ff1a4d3a2ec9b8ad0b5d80ee24d (patch) | |
tree | 5b75b34c822a79f70b83c266465dda70b9baeaf2 /pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/itext | |
parent | 04375406fc1634adbf9b37143a2125327da6a11e (diff) | |
download | pdf-as-3-29ad090c29567ff1a4d3a2ec9b8ad0b5d80ee24d.tar.gz pdf-as-3-29ad090c29567ff1a4d3a2ec9b8ad0b5d80ee24d.tar.bz2 pdf-as-3-29ad090c29567ff1a4d3a2ec9b8ad0b5d80ee24d.zip |
git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@671 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c
Diffstat (limited to 'pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/itext')
-rw-r--r-- | pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/itext/IText.java | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/itext/IText.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/itext/IText.java new file mode 100644 index 0000000..7175991 --- /dev/null +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/itext/IText.java @@ -0,0 +1,111 @@ +package at.gv.egiz.pdfas.web.itext;
+
+import java.awt.color.ICC_Profile;
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.output.ByteArrayOutputStream;
+import org.apache.log4j.Logger;
+
+import com.lowagie.text.Document;
+import com.lowagie.text.DocumentException;
+import com.lowagie.text.Font;
+import com.lowagie.text.PageSize;
+import com.lowagie.text.Paragraph;
+import com.lowagie.text.Rectangle;
+import com.lowagie.text.pdf.BaseFont;
+import com.lowagie.text.pdf.PdfArray;
+import com.lowagie.text.pdf.PdfDictionary;
+import com.lowagie.text.pdf.PdfICCBased;
+import com.lowagie.text.pdf.PdfName;
+import com.lowagie.text.pdf.PdfString;
+import com.lowagie.text.pdf.PdfWriter;
+
+public final class IText {
+
+ private IText() {
+ }
+
+ private static final Logger LOG = Logger.getLogger(IText.class);
+
+ private static final Font DEFAULT_FONT = new Font(Font.HELVETICA, 12, Font.NORMAL);
+ private static final String DEFAULT_TTF_FONT_RESOURCE = "DejaVuSansCondensed.ttf";
+ private static final String DEFAULT_ICC_PROFILE_RESOURCE = "srgb.profile";
+ private static final Rectangle DEFAULT_PAGE_SIZE = PageSize.A4;
+ private static final int[] DEFAULT_MARGINS_MM = { 25, 25, 25, 25 }; // top, right, bottom, left
+ public static final String DEFAULT_FILENAME = "text";
+
+ private static Font DEFAULT_TTF_FONT;
+ private static PdfICCBased DEFAULT_ICC_COLOR_SPACE;
+
+ static {
+ try {
+ byte[] ttfFontData = IOUtils.toByteArray(IText.class.getResourceAsStream(DEFAULT_TTF_FONT_RESOURCE));
+ DEFAULT_TTF_FONT = new Font(BaseFont.createFont(DEFAULT_TTF_FONT_RESOURCE, BaseFont.WINANSI, true, true, ttfFontData, null));
+ } catch (Exception e) {
+ LOG.error(e);
+ DEFAULT_TTF_FONT = DEFAULT_FONT;
+ }
+ try {
+ byte[] iccData = IOUtils.toByteArray(IText.class.getResourceAsStream(DEFAULT_ICC_PROFILE_RESOURCE));
+ DEFAULT_ICC_COLOR_SPACE = new PdfICCBased(ICC_Profile.getInstance(iccData));
+ DEFAULT_ICC_COLOR_SPACE.remove(PdfName.ALTERNATE);
+ } catch (Exception e) {
+ LOG.error(e);
+ DEFAULT_ICC_COLOR_SPACE = null;
+ }
+ }
+
+ public static byte[] createPDF(String content, boolean pdfa) throws DocumentException, IOException {
+ ByteArrayOutputStream baOut = new ByteArrayOutputStream();
+ createPDF(content, pdfa, baOut);
+ return baOut.toByteArray();
+ }
+
+ public static byte[] createPDF(String content) throws DocumentException, IOException {
+ return createPDF(content, false);
+ }
+
+
+ private static void addContent(Document document, boolean pdfa, String content) throws DocumentException {
+ Paragraph p = new Paragraph(content, pdfa ? DEFAULT_TTF_FONT : DEFAULT_FONT);
+ document.add(p);
+ }
+ public static void createPDF(String content, OutputStream outputStream) throws DocumentException, IOException {
+ createPDF(content, false, outputStream);
+ }
+
+ public static void createPDF(String content, boolean pdfa, OutputStream outputStream) throws DocumentException, IOException {
+ final float factor = (float) 72 / (float) 25.4;
+ Document document = new Document(
+ DEFAULT_PAGE_SIZE,
+ (float) DEFAULT_MARGINS_MM[3] * factor, // left
+ (float) DEFAULT_MARGINS_MM[1] * factor, // right
+ (float) DEFAULT_MARGINS_MM[0] * factor, // top
+ (float) DEFAULT_MARGINS_MM[2] * factor // bottom
+ );
+
+ PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
+ if (pdfa) {
+ pdfWriter.setPDFXConformance(PdfWriter.PDFA1B);
+ }
+ document.open();
+ if (pdfa) {
+ PdfDictionary pdfOutputIntent = new PdfDictionary(PdfName.OUTPUTINTENT);
+ pdfOutputIntent.put(PdfName.OUTPUTCONDITIONIDENTIFIER, new PdfString("sRGB IEC61966-2.1"));
+ pdfOutputIntent.put(PdfName.INFO, new PdfString("sRGB IEC61966-2.1"));
+ pdfOutputIntent.put(PdfName.S, PdfName.GTS_PDFA1);
+ if (DEFAULT_ICC_COLOR_SPACE != null) {
+ pdfOutputIntent.put(PdfName.DESTOUTPUTPROFILE, pdfWriter.addToBody(DEFAULT_ICC_COLOR_SPACE).getIndirectReference());
+ }
+ pdfWriter.getExtraCatalog().put(PdfName.OUTPUTINTENTS, new PdfArray(pdfOutputIntent));
+ }
+ addContent(document, pdfa, content);
+ if (pdfa) {
+ pdfWriter.createXmpMetadata();
+ }
+ document.close();
+ }
+
+}
|