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:com.sustainalytics.crawlerfilter.PDFtoText.java

License:Apache License

public static String extractITextText(String pdf) {
    PdfReader reader = null;/*from w  w w  .ja  va 2s.  com*/
    try {
        reader = new PdfReader(pdf);
    } catch (IOException e) {
        logger.info("Error in reading file with iText parser\n");
    }
    PdfReaderContentParser parser = new PdfReaderContentParser(reader);
    TextExtractionStrategy strategy;
    String text = "";
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
        try {
            strategy = parser.processContent(i, new SimpleTextExtractionStrategy());
            text += strategy.getResultantText();
        } catch (IOException e) {
            logger.info("Error in parsing with iText parser\n");
        }
        logger.info("PDF text extracted from " + pdf + "\n");
    }
    reader.close();

    return text;
}

From source file:com.swisscom.ais.itext.PDF.java

License:Open Source License

/** 
 * Add external revocation information to DSS Dictionary, to enable Long Term Validation (LTV) in Adobe Reader
 * //from w w w . j  a v  a  2  s  .  c  o  m
 * @param ocspArr List of OCSP Responses as base64 encoded String
 * @param crlArr  List of CRLs as base64 encoded String
 * @throws Exception 
 */
public void addValidationInformation(ArrayList<String> ocspArr, ArrayList<String> crlArr) throws Exception {
    if (ocspArr == null && crlArr == null)
        return;

    PdfReader reader = new PdfReader(outputFilePath);

    // Check if source pdf is not protected by a certification
    if (reader.getCertificationLevel() == PdfSignatureAppearance.CERTIFIED_NO_CHANGES_ALLOWED)
        throw new Exception(
                "Could not apply revocation information (LTV) to the DSS Dictionary. Document contains a certification that does not allow any changes.");

    Collection<byte[]> ocspColl = new ArrayList<byte[]>();
    Collection<byte[]> crlColl = new ArrayList<byte[]>();

    // Decode each OCSP Response (String of base64 encoded form) and add it to the Collection (byte[])
    if (ocspArr != null) {
        for (String ocspBase64 : ocspArr) {
            OCSPResp ocspResp = new OCSPResp(new ByteArrayInputStream(Base64.decode(ocspBase64)));
            BasicOCSPResp basicResp = (BasicOCSPResp) ocspResp.getResponseObject();

            if (Soap._debugMode) {
                System.out.println("\nEmbedding OCSP Response...");
                System.out.println("Status                : " + ((ocspResp.getStatus() == 0) ? "GOOD" : "BAD"));
                System.out.println("Produced at           : " + basicResp.getProducedAt());
                System.out.println("This Update           : " + basicResp.getResponses()[0].getThisUpdate());
                System.out.println("Next Update           : " + basicResp.getResponses()[0].getNextUpdate());
                System.out.println("X509 Cert Issuer      : " + basicResp.getCerts()[0].getIssuer());
                System.out.println("X509 Cert Subject     : " + basicResp.getCerts()[0].getSubject());
                System.out.println(
                        "Responder ID X500Name : " + basicResp.getResponderId().toASN1Object().getName());
                System.out.println("Certificate ID        : "
                        + basicResp.getResponses()[0].getCertID().getSerialNumber().toString() + " ("
                        + basicResp.getResponses()[0].getCertID().getSerialNumber().toString(16).toUpperCase()
                        + ")");
            }

            ocspColl.add(basicResp.getEncoded()); // Add Basic OCSP Response to Collection (ASN.1 encoded representation of this object)
        }
    }

    // Decode each CRL (String of base64 encoded form) and add it to the Collection (byte[])
    if (crlArr != null) {
        for (String crlBase64 : crlArr) {
            X509CRL x509crl = (X509CRL) CertificateFactory.getInstance("X.509")
                    .generateCRL(new ByteArrayInputStream(Base64.decode(crlBase64)));

            if (Soap._debugMode) {
                System.out.println("\nEmbedding CRL...");
                System.out.println("IssuerDN                    : " + x509crl.getIssuerDN());
                System.out.println("This Update                 : " + x509crl.getThisUpdate());
                System.out.println("Next Update                 : " + x509crl.getNextUpdate());
                System.out.println(
                        "No. of Revoked Certificates : " + ((x509crl.getRevokedCertificates() == null) ? "0"
                                : x509crl.getRevokedCertificates().size()));
            }

            crlColl.add(x509crl.getEncoded()); // Add CRL to Collection (ASN.1 DER-encoded form of this CRL)
        }
    }

    byteArrayOutputStream = new ByteArrayOutputStream();
    PdfStamper stamper = new PdfStamper(reader, byteArrayOutputStream, '\0', true);
    LtvVerification validation = stamper.getLtvVerification();

    // Add the CRL/OCSP validation information to the DSS Dictionary
    boolean addVerification = false;
    for (String sigName : stamper.getAcroFields().getSignatureNames()) {
        addVerification = validation.addVerification(sigName, // Signature Name
                ocspColl, // OCSP
                crlColl, // CRL
                null // certs
        );
    }

    validation.merge(); // Merges the validation with any validation already in the document or creates a new one.

    stamper.close();
    reader.close();

    // Save to (same) file
    OutputStream outputStream = new FileOutputStream(outputFilePath);
    byteArrayOutputStream.writeTo(outputStream);

    if (Soap._debugMode) {
        if (addVerification)
            System.out.println("\nOK merging LTV validation information to " + outputFilePath);
        else
            System.out.println("\nFAILED merging LTV validation information to " + outputFilePath);
    }

    byteArrayOutputStream.close();
    outputStream.close();
}

From source file:com.test.itext.Renderer.java

private byte[] populateXFA(byte[] templateBytes, byte[] xfaDataBytes) throws RuntimeException {

    // Create an output stream for the rendered doc
    ByteArrayOutputStream rendered = new ByteArrayOutputStream();

    try {/*  ww  w .  j a v  a  2s . c  o m*/
        PdfReader reader = new PdfReader(templateBytes);
        PdfStamper stamper = new PdfStamper(reader, rendered);
        AcroFields form = stamper.getAcroFields();
        XfaForm xfa = form.getXfa();
        xfa.fillXfaForm(new ByteArrayInputStream(xfaDataBytes));
        stamper.close();
        reader.close();
    } catch (IOException e) {
        String msg = "An IOException was thrown while trying to populate the XFA form. Msg=" + e.getMessage();
        e.printStackTrace();
        throw new RuntimeException(msg);
    } catch (DocumentException e) {
        String msg = "A DocumentException was thrown while trying to populate the XFA form. Msg="
                + e.getMessage();
        e.printStackTrace();
        throw new RuntimeException(msg);
    }

    return rendered.toByteArray();
}

From source file:com.test.itext.Renderer.java

private byte[] flattenXfa(byte[] sourceXfaBytes) throws RuntimeException {

    // Create an output stream for the flattened doc
    ByteArrayOutputStream flattened = new ByteArrayOutputStream();

    try {/*from  ww w . jav  a 2 s .  co m*/
        Document document = new Document();
        PdfReader reader = new PdfReader(sourceXfaBytes);
        PdfWriter writer = PdfWriter.getInstance(document, flattened);
        XFAFlattener flattener = new XFAFlattener(document, writer);
        System.out.println("flattener instantiated");
        flattener.flatten(reader);
        document.close();
    } catch (IOException e) {
        String msg = "An IOException was thrown while trying to flatten the XFA form. Msg=" + e.getMessage();
        e.printStackTrace();
        throw new RuntimeException(msg);
    } catch (DocumentException e) {
        String msg = "A DocumentException was thrown while trying to flatten the XFA form. Msg="
                + e.getMessage();
        e.printStackTrace();
        throw new RuntimeException(msg);
    } catch (InterruptedException e) {
        String msg = "An InterruptedException was thrown while trying to flatten the XFA form. Msg="
                + e.getMessage();
        e.printStackTrace();
        throw new RuntimeException(msg);
    } catch (NoClassDefFoundError e) {
        String msg = "A NoClassDefFoundError was thrown while trying to flatten the XFA form. Msg="
                + e.getMessage();
        e.printStackTrace();
        throw new RuntimeException(msg);
    } catch (Exception e) {
        String msg = "A general Exception was thrown while trying to flatten the XFA form. Msg="
                + e.getMessage();
        e.printStackTrace();
        throw new RuntimeException(msg);
    }

    return flattened.toByteArray();
}

From source file:com.tommontom.pdfsplitter.PdfCombine.java

public List<File> combine(File folder) throws IOException, DocumentException {
    if (!folder.isDirectory()) {
        throw new IllegalArgumentException("Provided folder is not a directory: " + folder);
    }/*from www .jav a2  s  .  c  o  m*/
    FileNameFilter FileFilter = new FileNameFilter();
    File[] listOfFiles = folder.listFiles(FileFilter);

    /*
     * Stores the listing of the files
     */
    List<File> newFileList = new ArrayList<>();
    for (int i = 0; i < listOfFiles.length; i++) {
        File file = listOfFiles[i];
        if (!file.isFile()) {
            continue;
        }

        // Split the source filename into its 2 parts
        String fileName = file.getName();
        String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf('.'));

        // Split on a space '\s'
        String[] fileNames = fileNameWithoutExt.split("\\s");
        if (fileNames.length != 2) {
            throw new RuntimeException("File name format is not in right format");
        }

        String fileNameFirst = fileNames[0];
        String fileNameSecond = fileNames[1];
        System.out.println("First lot number: " + fileNameFirst + " Second lot number: " + fileNameSecond);
        String[] fileNameFirstParts = fileNameFirst.split("-");

        // Project num is always the 1st part
        String projectNum = fileNameFirstParts[0];
        if (!projectNum.equals(fileNameFirstParts[0])) {
            throw new RuntimeException("Filename needs to be renamed to the correct format");
        }
        // Strip off the first and second lot number, parse into integers
        int firstLotNum;
        int secondLotNum;
        firstLotNum = Integer.parseInt(fileNameFirstParts[1]);
        secondLotNum = Integer.parseInt(fileNameFirstParts[2]);

        // Determine number of pages by difference of lot numbers

        // Read in the source document
        // Example file name: 16034-212234 16034-212236.pdf > 16034-212234.pdf,
        // 16034-212235.pdf, 16034-212236.pdf
        PdfReader pdfFileReader = new PdfReader(file.getPath());
        int mod = pdfFileReader.getNumberOfPages() % 2;
        if (pdfFileReader.equals(mod)) {
            throw new RuntimeException("File is not an even number of pages");
        }
        Document document = new Document();

        /*
         * instantiates a new document to be made
         */
        int numPages = pdfFileReader.getNumberOfPages();
        int p = 0;
        int j = 0;
        // Create a copy of the orignal source file. We will pick specific pages
        // out below
        document.open();
        while (j < numPages) {
            if (p == numPages) {
                break;
            }
            if (j % 2 == 2) {
                j += 1;
            }
            j += 1;
            firstLotNum++;

            // Dynamic filename
            String dynamicFileName = projectNum + "-" + (firstLotNum - 1) + ".pdf";
            document = new Document();
            PdfCopy copy = new PdfCopy(document,
                    new FileOutputStream(folder.getPath() + "\\" + dynamicFileName));
            document.open();
            p += 2;

            // Import pages from original doc
            copy.addPage(copy.getImportedPage(pdfFileReader, j));
            copy.addPage(copy.getImportedPage(pdfFileReader, p));
            document.close();

            // Add to the list of compled file names
            newFileList.add(new File(dynamicFileName));
        }
        System.out.println("Number of Documents Created:" + numPages);
        System.out.println("Number of Documents Created:" + listOfFiles[i]);
    }

    return newFileList;
}

From source file:com.tommontom.pdfsplitter.PdfMerge.java

public static void doMerge(java.util.List<InputStream> list, String[] imageList, String[] listWordExcels,
        OutputStream outputStream) throws DocumentException, IOException {
    Document document = new Document(PageSize.LETTER, 0, 0, 0, 0);
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
    writer.setFullCompression();/*from  w ww . ja va2  s .  com*/
    document.open();
    PdfContentByte cb = writer.getDirectContent();
    Image img;
    for (InputStream in : list) {
        PdfReader reader = new PdfReader(in);
        for (int i = 1; i <= reader.getNumberOfPages(); i++) {

            document.newPage();
            //import the page from source pdf
            PdfImportedPage page = writer.getImportedPage(reader, i);
            //add the page to the destination pdf
            cb.addTemplate(page, 0, 0);
        }

    }
    for (int i = 0; i < imageList.length; i++) {
        document.newPage();
        if (imageList[i] != null) {
            img = Image.getInstance(String.format("%s", imageList[i]));
            Rectangle one = new Rectangle(img.getPlainWidth(), img.getPlainHeight());
            document.setPageSize(one);
            if (img.getScaledWidth() > img.getScaledHeight()) {
                img.rotate();
            }
            if (img.getScaledWidth() > 792 || img.getScaledHeight() > 792) {
                img.scaleToFit(792, 792);

            }
            img.setDpi(150, 150);
            document.add(img);
        }
    }
    for (int i = 0; i < listWordExcels.length; i++) {
        if (imageList[i] != null) {
            File input = new File(listWordExcels[i]);
            File output = new File(listWordExcels[i] + ".pdf");
            String outputS = listWordExcels[i] + ".pdf";
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
            connection.connect();
            DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
            converter.convert(input, output);
            PdfReader readerWord = new PdfReader(outputS);
            PdfImportedPage page = writer.getImportedPage(readerWord, readerWord.getNumberOfPages());
            cb.addTemplate(page, 0, 0);
        }
    }

    outputStream.flush();
    document.close();
    outputStream.close();
}

From source file:com.tommontom.pdfsplitter.PdfSplit.java

public void pdfSplit(String path) throws IOException, DocumentException, InterruptedException {
    // TODO Instead of hard code path, pass in as argument
    File folder = new File(path);
    FileNameFilter FileFilter = new FileNameFilter();
    File[] listOfFiles = folder.listFiles(FileFilter); /* Stores the listing of the files */

    for (int i = 0; i < listOfFiles.length; i++) {
        File file = listOfFiles[i];
        if (!file.isFile()) {
            continue;
        }//  w w  w .jav a 2  s.c  o m
        // Split the source filename into its 2 parts
        String fileName = file.getName();
        String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf("."));
        PdfReader pdfFileReader = new PdfReader(file.getPath());
        Document document = new Document(PageSize.LETTER, 0, 0, 0,
                0); /* instantiates a new document to be made */

        int k = 0;
        int numPages = pdfFileReader.getNumberOfPages();
        // Split on a space '\s'
        // Determine number of pages by difference of lot numbers
        // Read in the source document
        // Example file name: 16034-212234 16034-212236.pdf > 16034-212234.pdf, 16034-212235.pdf, 16034-212236.pdf
        // Create a copy of the orignal source file. We will pick specific pages out below
        for (int j = 1; j < numPages + 1; j++) {
            String FileName = (fileNameWithoutExt); /* Dynamic file name */

            PdfCopy copy = new PdfCopy(document,
                    new FileOutputStream(path + "\\" + FileName + "(" + j + ")" + ".pdf"));
            document.open();
            copy.addPage(copy.getImportedPage(pdfFileReader, j)); /* Import pages from original document */

            deleteFile[k] = path + "\\" + FileName + "(" + j + 1 + ")" + ".pdf";
            k++;
            if (j == 1) {
                newFileListing = ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n");
            } else if (j > 1) {
                newFileListing += ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n");
            }
            document.close();

        }

        System.out.println("Number of Documents Created:" + numPages);
        pdfFileReader.close();
    }
}

From source file:com.tommontom.pdfsplitter.PdfSplit.java

public void pdfSplitCopy(String path) throws IOException, DocumentException {
    // TODO Instead of hard code path, pass in as argument
    File folder = new File(path);
    FileNameFilter FileFilter = new FileNameFilter();
    File[] listOfFiles = folder.listFiles(FileFilter); /* Stores the listing of the files */

    for (int i = 0; i < listOfFiles.length; i++) {
        File file = listOfFiles[i];
        if (!file.isFile()) {
            continue;
        }/*from   www .  j av  a  2 s.  c  om*/
        // Split the source filename into its 2 parts
        String fileName = file.getName();
        String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf("."));
        PdfReader pdfFileReader = new PdfReader(file.getPath());
        Document document = new Document(PageSize.LETTER, 0, 0, 0,
                0); /* instantiates a new document to be made */

        String[] fileNameNum = fileNameWithoutExt.split("-");
        int fileNameNumOne = Integer.getInteger(fileNameNum[0]);
        int fileNameNumTwo = Integer.getInteger(fileNameNum[1]);
        int constant = 1;
        int k = 0;
        int numPages = fileNameNumTwo - fileNameNumOne;
        // Split on a space '\s'
        // Determine number of pages by difference of lot numbers
        // Read in the source document
        // Example file name: 16034-212234 16034-212236.pdf > 16034-212234.pdf, 16034-212235.pdf, 16034-212236.pdf
        // Create a copy of the orignal source file. We will pick specific pages out below
        document.open();
        for (int j = 0; j < numPages + 1; j++) {
            String FileName = (fileNameWithoutExt); /* Dynamic file name */
            PdfCopy copy = new PdfCopy(document,
                    new FileOutputStream(path + "\\" + FileName + "(" + j + 1 + ")" + ".pdf"));
            deleteFile[k] = (path + "\\" + FileName + "(" + j + 1 + ")" + ".pdf");
            k++;
            document.open();
            copy.addPage(
                    copy.getImportedPage(pdfFileReader, constant)); /* Import pages from original document */

            if (j == 1) {
                newFileListing = ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n");
            } else if (j > 1) {
                newFileListing += ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n");
            }
            document.close();
            copy.close();
        }

        System.out.println("Number of Documents Created:" + numPages);
        pdfFileReader.close();
    }

}

From source file:com.tommontom.pdfsplitter.PdfSplit.java

public void pdfSplitSupplierDoc(String path) throws IOException, DocumentException {
    // TODO Instead of hard code path, pass in as argument
    File folder = new File(path);
    FileNameFilter FileFilter = new FileNameFilter();
    File[] listOfFiles = folder.listFiles(FileFilter); /* Stores the listing of the files */

    for (int i = 0; i < listOfFiles.length; i++) {
        File file = listOfFiles[i];
        if (!file.isFile()) {
            continue;
        }//from ww  w.  ja  v a 2 s .com
        // Split the source filename into its 2 parts
        String fileName = file.getName();
        String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf("."));
        PdfReader pdfFileReader = new PdfReader(file.getPath());
        Document document = new Document(PageSize.LETTER, 0, 0, 0,
                0); /* instantiates a new document to be made */

        String[] fileNames = fileNameWithoutExt.split("-");
        /*
         * if (fileNames.length != 2) { throw new RuntimeException("File name format is not in right format"); }
         */

        String fileNameFirst = fileNames[1];
        String fileNameSecond = fileNames[2];
        System.out.println("First lot number: " + fileNameFirst + " Second lot number: " + fileNameSecond);
        // Project num is always the 1st part
        String projectNum = fileNames[0];
        if (!projectNum.equals(fileNames[0])) {
            throw new RuntimeException("Filename needs to have a project number");
        }

        // Strip off the first and second lot number, parse into integers
        int firstLotNum;
        int secondLotNum;
        firstLotNum = Integer.parseInt(fileNames[1]);
        secondLotNum = Integer.parseInt(fileNames[2].substring(0, fileNames[2].lastIndexOf(".")));
        // Create a copy of the orignal source file. We will pick specific pages out below
        document.open();
        int numPages = secondLotNum - firstLotNum;
        for (int j = 1; j < numPages + 1; j++) {
            String FileName = projectNum + "-" + (firstLotNum) + ".pdf"; /* Dynamic file name */

            firstLotNum++;
            document = new Document(PageSize.LETTER, 0, 0, 0, 0);
            PdfCopy copy = new PdfCopy(document, new FileOutputStream(path + "\\" + FileName));
            document.open();
            copy.addPage(copy.getImportedPage(pdfFileReader, j)); /* Import pages from original document */

            document.close();
            copy.close();
        }
        pdfFileReader.close();
    }
}

From source file:com.tommontom.pdfsplitter.PdfSplit.java

public void pdfSplitDrop(File[] files) throws IOException, DocumentException, InterruptedException {
    // TODO Instead of hard code path, pass in as argument
    String path;/*from  w  w w  . j av a 2  s.  com*/
    if (directoryField.getText().isEmpty() || directoryField.getText().equals(example)) {
        path = files[0].getParent();
    } else {
        path = directoryField.getText();
    }
    File[] listOfFiles = files; /* Stores the listing of the files */

    for (int i = 0; i < listOfFiles.length; i++) {
        File file = listOfFiles[i];
        if (!file.isFile()) {
            continue;
        }
        // Split the source filename into its 2 parts
        String fileName = file.getName();
        String fileNameWithoutExt = fileName.substring(0, fileName.lastIndexOf("."));
        PdfReader pdfFileReader = new PdfReader(file.getPath());
        Document document = new Document(PageSize.LETTER, 0, 0, 0,
                0); /* instantiates a new document to be made */

        int numPages = pdfFileReader.getNumberOfPages();
        // Determine number of pages by difference of lot numbers
        // Read in the source document
        // Example file name: 16034-212234 16034-212236.pdf > 16034-212234.pdf, 16034-212235.pdf, 16034-212236.pdf
        // Create a copy of the orignal source file. We will pick specific pages out below
        int k = 0;
        for (int j = 1; j < numPages + 1; j++) {
            String FileName = fileNameWithoutExt; /* Dynamic file name */

            PdfCopy copy = new PdfCopy(document,
                    new FileOutputStream(path + "\\" + FileName + "(" + j + ")" + ".pdf"));
            document.open();
            copy.addPage(copy.getImportedPage(pdfFileReader, j)); /* Import pages from original document */
            deleteFile[k] += path + "\\" + FileName + "(" + j + ")" + ".pdf";
            k++;
            if (j == 1) {
                newFileListing = ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n");
            } else if (j > 1) {
                newFileListing += ("Created File:" + path + FileName + "(" + j + ")" + ".pdf" + "\n");
            }
            document.close();
            copy.close();
        }

        pdfFileReader.close();
    }
}