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

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

Introduction

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

Prototype

public PdfReader(final PdfReader reader) 

Source Link

Document

Creates an independent duplicate.

Usage

From source file:fyp.JavaWritePDF.java

public void manipulatePdf2(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.insertPage(reader.getNumberOfPages() + 1, reader.getPageSizeWithRotation(1));
    stamper.insertPage(reader.getNumberOfPages() + 1, reader.getPageSizeWithRotation(1));// create a second new page
    Image img = Image.getInstance(IMG);
    Image img2 = Image.getInstance(IMG2);
    img.setAbsolutePosition(0, 350);/*from  w ww . ja  v  a  2 s.c  o m*/
    img2.setAbsolutePosition(0, 350);
    stamper.getOverContent(2).addImage(img);
    stamper.getOverContent(3).addImage(img2); // add the second sessions image to the new page
    stamper.close();
    File delfile = new File(SRC);
    delfile.delete();
    respiratorytest.success();
}

From source file:gov.nih.nci.firebird.service.pdf.PdfProcessor.java

License:Open Source License

final void process() throws IOException {
    PdfReader reader = new PdfReader(srcPdfInputStream);
    PdfStamper stamper = null;// w ww  . j  ava2s. c  om
    try {
        stamper = new PdfStamper(reader, destPdfOutputStream);
        handleProcessing(reader, stamper);
    } catch (DocumentException e) {
        throw new IllegalStateException("Unexpected problem processing PDF", e);
    } finally {
        reader.close();
        close(stamper);
    }

}

From source file:gov.nih.nci.firebird.service.signing.DigitalSigningServiceBean.java

License:Open Source License

@Override
// This is minimal parameter list for signing.
public void signPdf(InputStream srcPdf, OutputStream destPdf, DigitalSigningAttributes signingAttributes)
        throws DigitalSigningException, IOException {
    try {/*w ww  . j a va  2s. c  o  m*/
        KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE, BOUNCY_CASTLE_PROVIDER);
        ks.load(signingAttributes.getSigningKeyStore(), signingAttributes.getSigningPassword().toCharArray());
        String alias = ks.aliases().nextElement();
        PrivateKey key = (PrivateKey) ks.getKey(alias, null);
        Certificate[] chain = ks.getCertificateChain(alias);
        // reader and stamper
        PdfReader reader = new PdfReader(srcPdf);
        PdfStamper stamper = PdfStamper.createSignature(reader, destPdf, '\0');
        PdfSignatureAppearance appearance = createPdfSigAppearance(stamper, signingAttributes, chain);
        // create signature dictionary
        PdfSignature dic = createSignatureDictionary(appearance,
                PdfPKCS7.getSubjectFields((X509Certificate) chain[0]).getField("CN"),
                signingAttributes.getSigningReason(), signingAttributes.getSigningLocation());
        appearance.setCryptoDictionary(dic);
        // Reserve space for signature content.
        HashMap<PdfName, Integer> exc = new HashMap<PdfName, Integer>();
        exc.put(PdfName.CONTENTS, Integer.valueOf(CONTENT_SIZE * 2 + 2));
        appearance.preClose(exc);
        // Create hash of content.
        byte[] digest = createMessageDigest(appearance);
        // Creates signed hash.
        byte[] signedHash = generateSignedHash(key, chain, digest);
        // add signature contents.
        PdfDictionary dic2 = new PdfDictionary();
        dic2.put(PdfName.CONTENTS, new PdfString(signedHash).setHexWriting(true));
        appearance.close(dic2);
    } catch (GeneralSecurityException e) {
        throw new DigitalSigningException("Failed signing pdf.", e);
    } catch (DocumentException de) {
        throw new DigitalSigningException("Failed signing pdf.", de);
    }
}

From source file:gov.utah.dts.det.ccl.actions.facility.information.license.reports.LicenseCertificate.java

private static PdfReader getTemplateReader(String templatefile) {
    PdfReader reader = null;/*from w  w w . j  a v a 2s.  com*/
    try {
        reader = new PdfReader(templatefile);
    } catch (Exception e) {

    }
    return reader;
}

From source file:gravabncertificado.GerandoArquivoCarimbado_1.java

public static void GerandoArquivoCarimbadoPDF(String caminhoarquivo, String BN) throws InvalidPdfException {
    //Copiando arquivo informado.
    try {//from   www . j a  v a2 s.c om

        // Adicionado parametro para nao retornar erro quando o documento for protegido.
        PdfReader.unethicalreading = true;
        //Cria o reader para o primeiro PDF

        PdfReader reader = new PdfReader(caminhoarquivo);

        // n recebe o numero total de paginas
        int n = reader.getNumberOfPages();

        //Tamanho da primeira Pagina
        ;

        //Cria Segundo PDF

        Document document = new Document(PageSize.A4, 36, 36, 36, 36);

        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-C.pdf"));

        document.open();

        // Adiciona conteudo ao PDF Carimbado.
        PdfContentByte cb = writer.getDirectContent();
        int i = 0;
        int p = 0;
        String caminhodestino = (caminhoarquivo.substring(0, caminhoarquivo.length() - BN.length()));

        // DESABILITADO PORQUE O MOVER NAO ESTAVA FUNCIONANDO.
        File destinooriginal = new File(caminhodestino + "ORIGINAL\\");
        if (!destinooriginal.exists()) {
            destinooriginal.mkdir();
        }

        caminhodestino = (caminhodestino + "ORIGINAL\\" + BN);

        //TESTANDO NOVA FORMA DE COPIAR

        File origem = new File(caminhoarquivo);
        File destino = new File(caminhodestino);

        FileInputStream fis = new FileInputStream(origem);
        FileOutputStream fos = new FileOutputStream(destino);

        FileChannel inChannel = fis.getChannel();
        FileChannel outChannel = fos.getChannel();

        long transferFrom = outChannel.transferFrom(inChannel, 0, inChannel.size());

        fis.close();
        fos.close();
        inChannel.close();
        outChannel.close();

        // Thread.sleep(10);

        BN = BN.substring(0, BN.length() - 4);

        while (i < n) {
            document.newPage();
            p++;
            i++;

            PdfContentByte under = writer.getDirectContentUnder();
            PdfImportedPage page1 = writer.getImportedPage(reader, i);
            cb.addTemplate(page1, 0, i * 0.2f);

            // Page 1: a rectangle
            /* RETANGULO DESATIVADO
            drawRectangle(under, 100, 50);
            under.setRGBColorFill(255, 220, 220);
            under.rectangle(width /50, 5, 118, 40);
            under.fill();
            */

            //CARIMBO DA BN

            BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
            cb.beginText();
            cb.setFontAndSize(bf, 14);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 44, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |               |", width / 6, 32, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |               |", width / 6, 22, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " NR", width / 6, 28, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " " + BN, width / 6, 16, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |               |", width / 6, 12, 0);
            cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 14, 0);
            cb.endText();

        }

        document.close();
        writer.close();
        reader.close();
        origem.delete();

        /* PdfContentByte under = writer.getDirectContentUnder();
                 
         // Page 1: a rectangle
             drawRectangle(under, 20, 20);
             under.setRGBColorFill(0xFF, 0xD7, 0x00);
             under.rectangle(5, 5, 15, 15);
             under.fill(); */
        //document.newPage();

        //COPIANDO ARQUIVO CARIMBADO

        /* DESABILITADO PORQUE NAO ESTAVA FUNCIONANDO.
        boolean bool;
                
         //caminhoarquivo = caminhoarquivo.replace("\\", "\\\\");
         //caminhodestino = caminhodestino.replace("\\", "\\\\");
                    
        File origem = new File(caminhoarquivo);
        File destino = new File(caminhodestino);
                
                    
        bool = origem.renameTo(destino);
        System.out.println(origem);
        System.out.println(caminhodestino);
        */

    } catch (IOException | DocumentException ex) {
    }

}

From source file:gravabncertificado007.CarimboCertificado.java

public void aplicaCarimboBin(String BN, String caminhoarquivo)
        throws DocumentException, IOException, RuntimeException {

    PdfReader.unethicalreading = true;/*from  w w w .  j av  a 2  s .  co m*/
    //Cria o reader para o primeiro PDF

    PdfReader reader = new PdfReader(caminhoarquivo);
    Rectangle psize = reader.getPageSize(1);
    float width = psize.getWidth();
    float height = psize.getHeight();

    Document document = new Document(new Rectangle(width, height));

    PdfWriter writer = PdfWriter.getInstance(document,
            new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-C.pdf"));

    document.open();

    int i = 0;
    BN = BN.substring(BN.length() - 13, BN.length() - 4);
    PdfContentByte cb = writer.getDirectContent();
    while (i < reader.getNumberOfPages()) {
        i++;

        document.newPage();

        PdfContentByte under = writer.getDirectContentUnder();
        PdfImportedPage page1 = writer.getImportedPage(reader, i);
        cb.addTemplate(page1, 0, i * 0.2f);

        //CARIMBO DA BN
        BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
        cb.beginText();
        cb.setFontAndSize(bf, 14);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 44, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |               |", width / 6, 32, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |               |", width / 6, 22, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " NR", width / 6, 28, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " " + BN, width / 6, 16, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |               |", width / 6, 12, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 14, 0);
        cb.endText();

    }

    document.close();
    writer.close();
    reader.close();

}

From source file:gravabncertificado007.CarimboCertificado.java

public void aplicaCariboGedi(String BN, String caminhoarquivo)
        throws DocumentException, IOException, RuntimeException {

    PdfReader.unethicalreading = true;//from   w  w  w. j  av  a 2s  .c  o  m
    //Cria o reader para o primeiro PDF

    PdfReader reader = new PdfReader(caminhoarquivo);
    Rectangle psize = reader.getPageSize(1);
    float width = psize.getWidth();
    float height = psize.getHeight();

    Document document = new Document(new Rectangle(width, height));

    PdfWriter writer = PdfWriter.getInstance(document,
            new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-G.pdf"));

    document.open();

    int i = 0;
    BN = BN.substring(BN.length() - 13, BN.length() - 4);
    PdfContentByte cb = writer.getDirectContent();
    while (i < reader.getNumberOfPages()) {
        i++;

        document.newPage();

        PdfContentByte under = writer.getDirectContentUnder();
        PdfImportedPage page1 = writer.getImportedPage(reader, i);
        cb.addTemplate(page1, 0, i * 0.2f);

        BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
        cb.beginText();
        cb.setFontAndSize(bf, 14);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "  _________________  ", width / 6, 44, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |                 |", width / 6, 32, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |                 |", width / 6, 22, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |Copia Controlada |", width / 6, 28, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |                 |", width / 6, 16, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |                 |", width / 6, 12, 0);
        cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " |_________________|", width / 6, 14, 0);
        cb.endText();
    }

    document.close();
    writer.close();
    reader.close();

}

From source file:helper.PdfText.java

License:Apache License

/**
 * @param pdfFile this file will be extracted.
 * @return the plain text of the pdf/*from  w ww .  ja v a2  s .co  m*/
 */
public String itext(File pdfFile) {

    PdfReader reader;
    try {
        reader = new PdfReader(pdfFile.getAbsolutePath());
        PdfReaderContentParser parser = new PdfReaderContentParser(reader);
        StringBuffer buf = new StringBuffer();
        TextExtractionStrategy strategy;
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
            buf.append(strategy.getResultantText());
        }

        return buf.toString();
    } catch (IOException e) {
        throw new HttpArchiveException(500, e);
    }

}

From source file:hr.tstrelar.pdfsigcheck.helpers.SignatureInfo.java

License:Open Source License

private void init(String fileName) throws IOException {
    PdfReader reader = new PdfReader(fileName);
    af = reader.getAcroFields();//w  w  w .  j  a va  2s .c om
    log.info("Found " + getTotalRevisions() + " revision(s)");
    for (String s : af.getSignatureNames()) {
        signatureRevisions.put(s, new DocumentRevision(af, kall, s));
        log.fine("Signature name: " + s);
    }
}

From source file:hsa.awp.admingui.report.printer.MergePDFUtil.java

License:Open Source License

private List<PdfReader> createPdfReaders(List<InputStream> streamOfPDFFiles) throws IOException {
    List<PdfReader> readers = new ArrayList<PdfReader>();
    for (InputStream pdf : streamOfPDFFiles) {
        PdfReader pdfReader = new PdfReader(pdf);
        readers.add(pdfReader);/*from  ww  w .  jav a  2  s .c  om*/
    }
    return readers;
}