Example usage for com.lowagie.text.pdf PdfWriter VERSION_1_3

List of usage examples for com.lowagie.text.pdf PdfWriter VERSION_1_3

Introduction

In this page you can find the example usage for com.lowagie.text.pdf PdfWriter VERSION_1_3.

Prototype

char VERSION_1_3

To view the source code for com.lowagie.text.pdf PdfWriter VERSION_1_3.

Click Source Link

Document

possible PDF version (header)

Usage

From source file:ispyb.common.util.PDFFormFiller.java

License:Open Source License

/**
 * Initialize the PDF document// w w  w  .j  ava  2s.  c  o  m
 * 
 * @param pdfForm
 *            the path to the PDF form to be filled
 * @param os
 *            the output stream object
 * @param pageOrientation
 *            the page orientation (Portrait or Landscape)
 * @throws Exception
 */
public void init(String pdfForm, OutputStream os, int pageOrientation) throws Exception {
    if (LOG.isDebugEnabled())
        LOG.debug("Initializing with template: " + pdfForm);
    try {
        this.reader = new PdfReader(pdfForm);
        this.stamper = new PdfStamper(this.reader, os);
        this.writer = this.stamper.getWriter();
        this.writer.setPdfVersion(PdfWriter.VERSION_1_3);
        this.setPageOrientation(pageOrientation);
        this.formFields = this.stamper.getAcroFields();
    } catch (Exception e) {
        LOG.error("init", e);
        throw new Exception("Error initializing the PDF objects for file \"" + pdfForm + "\".");
    }
}

From source file:ispyb.common.util.PDFFormFiller.java

License:Open Source License

/**
 * Initialize the PDF document/*  w  w w. j  av  a  2  s  .c  o m*/
 * 
 * @param pdfForm
 *            the path to the PDF form to be filled
 * @param os
 *            the output stream object
 * @param pageOrientation
 *            the page orientation (Portrait or Landscape)
 * @throws Exception
 */
private void init(byte[] bs, OutputStream os, int pageOrientation) throws Exception {
    try {
        this.reader = new PdfReader(bs);
        this.stamper = new PdfStamper(this.reader, os);
        this.writer = this.stamper.getWriter();
        this.writer.setPdfVersion(PdfWriter.VERSION_1_3);
        this.setPageOrientation(pageOrientation);
        this.formFields = this.stamper.getAcroFields();
    } catch (Exception e) {
        LOG.error("init", e);
        throw new Exception("Error initializing the PDF objects for file bytes.");
    }
}

From source file:ispyb.common.util.PDFFormFiller.java

License:Open Source License

public void init(InputStream input, OutputStream os) throws Exception {
    try {// w w  w  .  j a v a2 s  . c o m
        this.reader = new PdfReader(input);
        this.stamper = new PdfStamper(this.reader, os);
        this.writer = this.stamper.getWriter();
        this.writer.setPdfVersion(PdfWriter.VERSION_1_3);
        this.setPageOrientation(PAGE_PORTRAIT);
        this.formFields = this.stamper.getAcroFields();
    } catch (Exception e) {
        LOG.error("init", e);
        throw new Exception("Error initializing the PDF objects for file bytes.");
    }
}

From source file:lucee.runtime.tag.PDF.java

License:Open Source License

/**
 * @param version the version to set/* ww w . j a  v  a 2s  .c o  m*/
 * @throws ApplicationException 
 */
public void setVersion(double version) throws ApplicationException {
    if (1.1 == version)
        this.version = '1';
    else if (1.2 == version)
        this.version = PdfWriter.VERSION_1_2;
    else if (1.3 == version)
        this.version = PdfWriter.VERSION_1_3;
    else if (1.4 == version)
        this.version = PdfWriter.VERSION_1_4;
    else if (1.5 == version)
        this.version = PdfWriter.VERSION_1_5;
    else if (1.6 == version)
        this.version = PdfWriter.VERSION_1_6;

    else
        throw new ApplicationException("invalid version definition [" + Caster.toString(version)
                + "], valid version definitions are " + "[1.1, 1.2, 1.3, 1.4, 1.5, 1.6]");
}

From source file:org.allcolor.yahp.cl.converter.CDocumentReconstructor.java

License:Open Source License

/**
 * construct a pdf document from pdf parts.
 * /* w w w .  ja v  a 2 s.  c o m*/
 * @param files
 *            list containing the pdf to assemble
 * @param properties
 *            converter properties
 * @param fout
 *            outputstream to write the new pdf
 * @param base_url
 *            base url of the document
 * @param producer
 *            producer of the pdf
 * 
 * @throws CConvertException
 *             if an error occured while reconstruct.
 */
public static void reconstruct(final List files, final Map properties, final OutputStream fout,
        final String base_url, final String producer, final PageSize[] size, final List hf)
        throws CConvertException {
    OutputStream out = fout;
    OutputStream out2 = fout;
    boolean signed = false;
    OutputStream oldOut = null;
    File tmp = null;
    File tmp2 = null;
    try {
        tmp = File.createTempFile("yahp", "pdf");
        tmp2 = File.createTempFile("yahp", "pdf");
        oldOut = out;
        if ("true".equals(properties.get(IHtmlToPdfTransformer.USE_PDF_SIGNING))) {
            signed = true;
            out2 = new FileOutputStream(tmp2);
        } // end if
        else {
            out2 = oldOut;
        }
        out = new FileOutputStream(tmp);
        com.lowagie.text.Document document = null;
        PdfCopy writer = null;
        boolean first = true;

        Map mapSizeDoc = new HashMap();

        int totalPage = 0;

        for (int i = 0; i < files.size(); i++) {
            final File fPDF = (File) files.get(i);
            final PdfReader reader = new PdfReader(fPDF.getAbsolutePath());
            reader.consolidateNamedDestinations();

            final int n = reader.getNumberOfPages();

            if (first) {
                first = false;
                // step 1: creation of a document-object
                // set title/creator/author
                document = new com.lowagie.text.Document(reader.getPageSizeWithRotation(1));
                // step 2: we create a writer that listens to the document
                writer = new PdfCopy(document, out);
                // use pdf version 1.5
                writer.setPdfVersion(PdfWriter.VERSION_1_3);
                // compress the pdf
                writer.setFullCompression();

                // check if encryption is needed
                if ("true".equals(properties.get(IHtmlToPdfTransformer.USE_PDF_ENCRYPTION))) {
                    final String password = (String) properties
                            .get(IHtmlToPdfTransformer.PDF_ENCRYPTION_PASSWORD);
                    final int securityType = CDocumentReconstructor.getSecurityFlags(properties);
                    writer.setEncryption(PdfWriter.STANDARD_ENCRYPTION_128, password, null, securityType);
                } // end if

                final String title = (String) properties.get(IHtmlToPdfTransformer.PDF_TITLE);

                if (title != null) {
                    document.addTitle(title);
                } // end if
                else if (base_url != null) {
                    document.addTitle(base_url);
                } // end else if

                final String creator = (String) properties.get(IHtmlToPdfTransformer.PDF_CREATOR);

                if (creator != null) {
                    document.addCreator(creator);
                } // end if
                else {
                    document.addCreator(IHtmlToPdfTransformer.VERSION);
                } // end else

                final String author = (String) properties.get(IHtmlToPdfTransformer.PDF_AUTHOR);

                if (author != null) {
                    document.addAuthor(author);
                } // end if

                final String sproducer = (String) properties.get(IHtmlToPdfTransformer.PDF_PRODUCER);

                if (sproducer != null) {
                    document.add(new Meta("Producer", sproducer));
                } // end if
                else {
                    document.add(new Meta("Producer", (IHtmlToPdfTransformer.VERSION
                            + " - http://www.allcolor.org/YaHPConverter/ - " + producer)));
                } // end else

                // step 3: we open the document
                document.open();
            } // end if

            PdfImportedPage page;

            for (int j = 0; j < n;) {
                ++j;
                totalPage++;
                mapSizeDoc.put("" + totalPage, "" + i);
                page = writer.getImportedPage(reader, j);
                writer.addPage(page);
            } // end for
        } // end for

        document.close();
        out.flush();
        out.close();
        {
            final PdfReader reader = new PdfReader(tmp.getAbsolutePath());
            ;
            final int n = reader.getNumberOfPages();
            final PdfStamper stp = new PdfStamper(reader, out2);
            int i = 0;
            BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED);
            final CHtmlToPdfFlyingSaucerTransformer trans = new CHtmlToPdfFlyingSaucerTransformer();
            while (i < n) {
                i++;
                int indexSize = Integer.parseInt((String) mapSizeDoc.get("" + i));
                final int[] dsize = size[indexSize].getSize();
                final int[] dmargin = size[indexSize].getMargin();
                for (final Iterator it = hf.iterator(); it.hasNext();) {
                    final CHeaderFooter chf = (CHeaderFooter) it.next();
                    if (chf.getSfor().equals(CHeaderFooter.ODD_PAGES) && (i % 2 == 0)) {
                        continue;
                    } else if (chf.getSfor().equals(CHeaderFooter.EVEN_PAGES) && (i % 2 != 0)) {
                        continue;
                    }
                    final String text = chf.getContent().replaceAll("<pagenumber>", "" + i)
                            .replaceAll("<pagecount>", "" + n);
                    // text over the existing page
                    final PdfContentByte over = stp.getOverContent(i);
                    final ByteArrayOutputStream bbout = new ByteArrayOutputStream();
                    if (chf.getType().equals(CHeaderFooter.HEADER)) {
                        trans.transform(new ByteArrayInputStream(text.getBytes("utf-8")), base_url,
                                new PageSize(dsize[0] - (dmargin[0] + dmargin[1]), dmargin[3]), new ArrayList(),
                                properties, bbout);
                    } else if (chf.getType().equals(CHeaderFooter.FOOTER)) {
                        trans.transform(new ByteArrayInputStream(text.getBytes("utf-8")), base_url,
                                new PageSize(dsize[0] - (dmargin[0] + dmargin[1]), dmargin[2]), new ArrayList(),
                                properties, bbout);
                    }
                    final PdfReader readerHF = new PdfReader(bbout.toByteArray());
                    if (chf.getType().equals(CHeaderFooter.HEADER)) {
                        over.addTemplate(stp.getImportedPage(readerHF, 1), dmargin[0], dsize[1] - dmargin[3]);
                    } else if (chf.getType().equals(CHeaderFooter.FOOTER)) {
                        over.addTemplate(stp.getImportedPage(readerHF, 1), dmargin[0], 0);
                    }
                    readerHF.close();
                }
            }
            stp.close();
        }
        try {
            out2.flush();
        } catch (Exception ignore) {
        } finally {
            try {
                out2.close();
            } catch (Exception ignore) {
            }
        }
        if (signed) {

            final String keypassword = (String) properties
                    .get(IHtmlToPdfTransformer.PDF_SIGNING_PRIVATE_KEY_PASSWORD);
            final String password = (String) properties.get(IHtmlToPdfTransformer.PDF_ENCRYPTION_PASSWORD);
            final String keyStorepassword = (String) properties
                    .get(IHtmlToPdfTransformer.PDF_SIGNING_KEYSTORE_PASSWORD);
            final String privateKeyFile = (String) properties
                    .get(IHtmlToPdfTransformer.PDF_SIGNING_PRIVATE_KEY_FILE);
            final String reason = (String) properties.get(IHtmlToPdfTransformer.PDF_SIGNING_REASON);
            final String location = (String) properties.get(IHtmlToPdfTransformer.PDF_SIGNING_LOCATION);
            final boolean selfSigned = !"false"
                    .equals(properties.get(IHtmlToPdfTransformer.USE_PDF_SELF_SIGNING));
            PdfReader reader = null;

            if (password != null) {
                reader = new PdfReader(tmp2.getAbsolutePath(), password.getBytes());
            } // end if
            else {
                reader = new PdfReader(tmp2.getAbsolutePath());
            } // end else

            final KeyStore ks = selfSigned ? KeyStore.getInstance(KeyStore.getDefaultType())
                    : KeyStore.getInstance("pkcs12");
            ks.load(new FileInputStream(privateKeyFile), keyStorepassword.toCharArray());

            final String alias = (String) ks.aliases().nextElement();
            final PrivateKey key = (PrivateKey) ks.getKey(alias, keypassword.toCharArray());
            final Certificate chain[] = ks.getCertificateChain(alias);
            final PdfStamper stp = PdfStamper.createSignature(reader, oldOut, '\0');

            if ("true".equals(properties.get(IHtmlToPdfTransformer.USE_PDF_ENCRYPTION))) {
                stp.setEncryption(PdfWriter.STANDARD_ENCRYPTION_128, password, null,
                        CDocumentReconstructor.getSecurityFlags(properties));
            } // end if

            final PdfSignatureAppearance sap = stp.getSignatureAppearance();

            if (selfSigned) {
                sap.setCrypto(key, chain, null, PdfSignatureAppearance.SELF_SIGNED);
            } // end if
            else {
                sap.setCrypto(key, chain, null, PdfSignatureAppearance.WINCER_SIGNED);
            } // end else

            if (reason != null) {
                sap.setReason(reason);
            } // end if

            if (location != null) {
                sap.setLocation(location);
            } // end if

            stp.close();
            oldOut.flush();
        } // end if
    } // end try
    catch (final Exception e) {
        throw new CConvertException(
                "ERROR: An Exception occured while reconstructing the pdf document: " + e.getMessage(), e);
    } // end catch
    finally {
        try {
            tmp.delete();
        } // end try
        catch (final Exception ignore) {
        }
        try {
            tmp2.delete();
        } // end try
        catch (final Exception ignore) {
        }
    } // end finally
}

From source file:org.lucee.extension.pdf.tag.PDF.java

License:Open Source License

/**
 * @param version/*from   www  .  j a  v  a 2s  . c  om*/
 *            the version to set
 * @throws PageException
 */
public void setVersion(double version) throws PageException {
    if (1.1 == version)
        this.version = '1';
    else if (1.2 == version)
        this.version = PdfWriter.VERSION_1_2;
    else if (1.3 == version)
        this.version = PdfWriter.VERSION_1_3;
    else if (1.4 == version)
        this.version = PdfWriter.VERSION_1_4;
    else if (1.5 == version)
        this.version = PdfWriter.VERSION_1_5;
    else if (1.6 == version)
        this.version = PdfWriter.VERSION_1_6;

    else
        throw engine.getExceptionUtil().createApplicationException(
                "invalid version definition [" + engine.getCastUtil().toString(version)
                        + "], valid version definitions are " + "[1.1, 1.2, 1.3, 1.4, 1.5, 1.6]");
}