Example usage for com.lowagie.text.pdf PdfReader PdfReader

List of usage examples for com.lowagie.text.pdf PdfReader PdfReader

Introduction

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

Prototype

public PdfReader(PdfReader reader) 

Source Link

Document

Creates an independent duplicate.

Usage

From source file:org.squale.welcom.outils.pdf.advanced.WPdfFieldReader.java

License:Open Source License

/**
 * fill//  w  w  w  .j a v a2s. c  om
 * 
 * @return un pdfReader
 * @throws IOException exception pouvant etre levee
 * @throws DocumentException exception pouvant etre levee
 */
public PdfReader fill() throws IOException, DocumentException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    fill(out);
    return new PdfReader(out.toByteArray());
}

From source file:org.squale.welcom.outils.pdf.advanced.WPdfMerge.java

License:Open Source License

/**
 * @param reader le reader//from  w w w  .  j a va2s  .  c  om
 * @throws IOException exception pouvant etre levee
 */
public void add(final byte[] reader) throws IOException {
    if (reader != null) {
        readers.add(new PdfReader(reader));
    }
}

From source file:org.squale.welcom.outils.pdf.advanced.WPdfMerge.java

License:Open Source License

/**
 * @throws DocumentException exception pouvant etre levee
 * @throws IOException exception pouvant etre levee
 *//*from   w  ww  .j  a  v a  2s  .  c  o m*/
public void close() throws DocumentException, IOException {
    byte[] theReport = merge();
    if (theReport != null) {
        final byte[] filledReport = fillDecoration(new PdfReader(theReport));
        if (filledReport != null) {
            theReport = filledReport;
        }
        // theReport=fillFooter(new PdfReader(theReport));
        out.write(theReport);
    }

}

From source file:org.squale.welcom.outils.pdf.advanced.WPdfMerge.java

License:Open Source License

/**
 * merge/*from  w ww. j  a  v  a2  s  .  c o  m*/
 * 
 * @return byte array rempli
 */
private byte[] merge() {
    final ByteArrayOutputStream tmpout = new ByteArrayOutputStream();
    int pageOffset = 0;
    int f = 0;
    Document document = null;
    final ArrayList master = new ArrayList();
    PdfCopy writer = null;

    final Iterator it = readers.iterator();

    while (it.hasNext()) {
        PdfReader reader = (PdfReader) it.next();

        try {
            // Renome tout les champs;
            reader = new PdfReader(renameFieldUnique(reader));

            reader.consolidateNamedDestinations();
            // we retrieve the total number of pages
            final int n = reader.getNumberOfPages();
            final List bookmarks = SimpleBookmark.getBookmark(reader);
            if (bookmarks != null) {
                if (pageOffset != 0) {
                    SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null);
                }
                master.addAll(bookmarks);
            }
            pageOffset += n;

            if (f == 0) {
                // step 1: creation of a document-object
                document = new Document(reader.getPageSizeWithRotation(1));
                // step 2: we create a writer that listens to the document
                writer = new PdfCopy(document, tmpout);
                // step 3: we open the document
                document.open();
            }
            // step 4: we add content
            PdfImportedPage page;
            for (int i = 0; i < n;) {
                ++i;
                page = writer.getImportedPage(reader, i);
                writer.addPage(page);
            }
            final PRAcroForm form = reader.getAcroForm();
            if (form != null) {
                writer.copyAcroForm(reader);
            }
            f++;
            if (master.size() > 0) {
                writer.setOutlines(master);
            }
            // step 5: we close the document
            // document.close();

        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
    if (document != null) {
        document.close();
    }
    return tmpout.toByteArray();
}

From source file:org.squale.welcom.outils.pdf.WPdfUtil.java

License:Open Source License

/**
 * Ajoute dans les parametres du report les paramtres pour l'ouverture en plein ecran Utilise itext.jar
 * /* w ww.  jav a  2 s  . co m*/
 * @param report : report
 * @param out : flux pour ecrire
 * @throws DocumentException : Probleme a la lecture du document
 * @throws IOException : Probleme a l'ecriture dans la stream
 */
public static void fullScreen(final byte report[], final OutputStream out)
        throws DocumentException, IOException {
    final PdfStamper stamper = new PdfStamper(new PdfReader(report), out);

    stamper.setViewerPreferences(PdfWriter.PageLayoutTwoColumnRight | PdfWriter.PageModeFullScreen
            | PdfWriter.NonFullScreenPageModeUseThumbs);

    stamper.close();
}

From source file:org.squale.welcom.outils.pdf.WPdfUtil.java

License:Open Source License

/**
 * Ajoute dans les parametres du report l'impression au chargement du document Utilise itext.jar
 * //from w w w. j ava 2  s .  co m
 * @param report : report
 * @param out : flux pour ecrire
 * @throws DocumentException : Probleme a la lecture du document
 * @throws IOException : Probleme a l'ecriture dans la stream
 */
public static void autoPrint(final byte report[], final OutputStream out)
        throws DocumentException, IOException {
    final PdfStamper stamper = new PdfStamper(new PdfReader(report), out);

    stamper.addJavaScript("this.print(false);");

    stamper.close();
}

From source file:org.squale.welcom.outils.pdf.WPdfUtil.java

License:Open Source License

/**
 * Ajoute dans les parametres du report l'impression au chargement du document avec choix pour l'imprimante Utilise
 * itext.jar//  w  w  w. j  a v a 2s  .c o  m
 * 
 * @param report : report
 * @param out : flux pour ecrire
 * @throws DocumentException : Probleme a la lecture du document
 * @throws IOException : Probleme a l'ecriture dans la stream
 */
public static void autoPrintPopup(final byte report[], final OutputStream out)
        throws DocumentException, IOException {
    final PdfStamper stamper = new PdfStamper(new PdfReader(report), out);

    stamper.addJavaScript("this.print(true);");

    stamper.close();
}

From source file:org.viafirma.util.QRCodeUtil.java

License:Apache License

/**
 * Genera un justificante de firma de un fichero pdf de entrada.
 * /*from w  w  w.ja  v a  2 s .co  m*/
 * @param input
 *            Fichero pdf de entrada
 * @param texto
 * @param textoQR
 * @param codFirma
 * @param out
 * @throws ExcepcionErrorInterno
 */
public void firmarPDF(InputStream input, String texto, String texto2Line, String textoQR, String codFirma,
        OutputStream out) throws ExcepcionErrorInterno {
    // leemos el pdf utilizando iText.
    try {
        // Recuperamos el documento original
        PdfReader reader = new PdfReader(input);

        // Obtenemos el tamao de la pgina
        Rectangle pageSize = reader.getPageSize(1);

        // Creamos un nuevo documento del mismo tamao que el original
        Document document = new Document(pageSize);

        // creo una instancia para escritura en el documento
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();

        // insertamos la portada del documento que estamos firmando.
        float escala = (pageSize.getHeight() - 350) / pageSize.getHeight();

        // Aadimos al documento la imagen de cabecera y de pie
        addContent(texto, texto2Line, textoQR, codFirma, document, pageSize, true, false);

        PdfContentByte cb = writer.getDirectContent();

        PdfImportedPage portada = writer.getImportedPage(reader, 1);
        cb.setRGBColorStroke(0xCC, 0xCC, 0xCC);
        cb.transform(AffineTransform.getTranslateInstance(document.leftMargin(), 210));
        cb.transform(AffineTransform.getScaleInstance(escala, escala));
        cb.addTemplate(portada, 0, 0);// , escala, 0, 0, escala,dx,dy);
        document.close();
        out.close();
    } catch (Exception e) {
        throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e);
    }
}

From source file:org.viafirma.util.QRCodeUtil.java

License:Apache License

/**
 * Genera un documento sellado en fichero pdf.
 * // w ww. j a v  a2s  .  com
 * @param input
 *            Fichero pdf de entrada
 * @param texto
 * @param textoQR
 * @param codFirma
 * @param out
 * @throws ExcepcionErrorInterno
 */
public void firmarPDFSellado(InputStream input, String texto, String texto2Line, String textoQR,
        String codFirma, OutputStream out) throws ExcepcionErrorInterno {
    // leemos el pdf utilizando iText.
    try {
        // Recuperamos el documento original
        PdfReader reader = new PdfReader(input);

        // Obtenemos el tamao de la pgina
        Rectangle pageSize = reader.getPageSize(1);

        // Creamos un nuevo documento del mismo tamao que el original
        Document document = new Document(pageSize);

        // creo una instancia para escritura en el documento
        PdfWriter writer = PdfWriter.getInstance(document, out);
        document.open();

        // Aadimos al documento la imagen de cabecera y de pie
        float altoCabeceraYPie = addContent(texto, texto2Line, textoQR, codFirma, document, pageSize, true,
                true);
        altoCabeceraYPie = altoCabeceraYPie + document.topMargin();
        // insertamos la portada del documento que estamos firmando.
        float escala = (pageSize.getHeight() - altoCabeceraYPie) / pageSize.getHeight();

        PdfContentByte cb = writer.getDirectContent();

        PdfImportedPage portada = writer.getImportedPage(reader, 1);
        cb.setRGBColorStroke(0xCC, 0xCC, 0xCC);
        cb.transform(AffineTransform.getScaleInstance(escala, escala));
        cb.transform(AffineTransform.getTranslateInstance(document.leftMargin() * 2.5,
                ALTO_ETIQUETA + document.topMargin()));

        cb.addTemplate(portada, 0, 0);// , escala, 0, 0, escala,dx,dy);
        document.close();
        out.close();
    } catch (Exception e) {
        throw new ExcepcionErrorInterno(CodigoError.ERROR_INTERNO, e);
    }
}

From source file:org.webpki.pdf.PDFSigner.java

License:Apache License

public byte[] addDocumentSignature(byte[] indoc, boolean certified) throws IOException {
    try {//w  ww . j  a  va2  s.  c  o m
        PdfReader reader = new PdfReader(indoc);
        ByteArrayOutputStream bout = new ByteArrayOutputStream(8192);
        PdfStamper stp = PdfStamper.createSignature(reader, bout, '\0', null, true);

        for (Attachment file : attachments) {
            stp.addFileAttachment(file.description, file.data, "dummy", file.filename);
        }

        PdfSignatureAppearance sap = stp.getSignatureAppearance();
        sap.setCrypto(null, signer.getCertificatePath(), null, PdfSignatureAppearance.WINCER_SIGNED);

        if (reason != null) {
            sap.setReason(reason);
        }
        if (location != null) {
            sap.setLocation(location);
        }

        if (enable_signature_graphics) {
            sap.setVisibleSignature(new Rectangle(100, 100, 400, 130), reader.getNumberOfPages(), null);
        }

        sap.setCertified(certified);

        //           sap.setExternalDigest (new byte[128], new byte[20], "RSA");
        sap.setExternalDigest(new byte[512], new byte[20], "RSA");
        sap.preClose();
        MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
        byte buf[] = new byte[8192];
        int n;
        InputStream inp = sap.getRangeStream();
        while ((n = inp.read(buf)) > 0) {
            messageDigest.update(buf, 0, n);
        }
        byte hash[] = messageDigest.digest();
        PdfSigGenericPKCS sg = sap.getSigStandard();
        PdfLiteral slit = (PdfLiteral) sg.get(PdfName.CONTENTS);
        byte[] outc = new byte[(slit.getPosLength() - 2) / 2];
        PdfPKCS7 sig = sg.getSigner();
        sig.setExternalDigest(signer.signData(hash, AsymSignatureAlgorithms.RSA_SHA1), hash, "RSA");
        PdfDictionary dic = new PdfDictionary();
        byte[] ssig = sig.getEncodedPKCS7();
        System.arraycopy(ssig, 0, outc, 0, ssig.length);
        dic.put(PdfName.CONTENTS, new PdfString(outc).setHexWriting(true));
        sap.close(dic);

        return bout.toByteArray();
    } catch (NoSuchAlgorithmException nsae) {
        throw new IOException(nsae.getMessage());
    } catch (DocumentException de) {
        throw new IOException(de.getMessage());
    }
}