aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/gv/egiz/pdfas/itext/IText.java
diff options
context:
space:
mode:
authortknall <tknall@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c>2010-03-16 12:07:29 +0000
committertknall <tknall@7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c>2010-03-16 12:07:29 +0000
commit11b5950be66bcc9d6f0bb28d3fc9d211bc70f4d9 (patch)
tree5a48a33069a318e269245998ecf89b387f331f67 /src/main/java/at/gv/egiz/pdfas/itext/IText.java
parentda4926845267ca8bedf34917bd3bfb94aeafa153 (diff)
downloadpdf-as-3-11b5950be66bcc9d6f0bb28d3fc9d211bc70f4d9.tar.gz
pdf-as-3-11b5950be66bcc9d6f0bb28d3fc9d211bc70f4d9.tar.bz2
pdf-as-3-11b5950be66bcc9d6f0bb28d3fc9d211bc70f4d9.zip
Catching OutOfMemory exceptions, returning appropriate error message/code
Binary signature: bug concerning indirect pdf objects fixed SignaturePositioning improved (Signature position can be declared by String which is parsed) Some more error codes (Out of memory, Invalid signature position) iText utility for creation of pdf files added ConfigUtils updated (destination of configuration to be extracted can now be chosen) PDFASUtils updated (more tools) WebApplication: Freetext pdf creation implemented WebApplication: XSS security updates git-svn-id: https://joinup.ec.europa.eu/svn/pdf-as/trunk@580 7b5415b0-85f9-ee4d-85bd-d5d0c3b42d1c
Diffstat (limited to 'src/main/java/at/gv/egiz/pdfas/itext/IText.java')
-rw-r--r--src/main/java/at/gv/egiz/pdfas/itext/IText.java111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/main/java/at/gv/egiz/pdfas/itext/IText.java b/src/main/java/at/gv/egiz/pdfas/itext/IText.java
new file mode 100644
index 0000000..767dbbc
--- /dev/null
+++ b/src/main/java/at/gv/egiz/pdfas/itext/IText.java
@@ -0,0 +1,111 @@
+package at.gv.egiz.pdfas.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();
+ }
+
+}