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

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

Introduction

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

Prototype

public HashMap getInfo() 

Source Link

Document

Returns the content of the document information dictionary as a HashMap of String.

Usage

From source file:org.mnsoft.pdfocr.PDFTrans.java

License:Open Source License

/**
 * @param args the command line arguments
 *//*  w  w w . j a v a 2s  .  c  o  m*/
@SuppressWarnings({ "deprecation", "rawtypes" })
public static void main(String[] args) {
    if (args.length < 2) {
        usage();
    }

    String input_file = null, output_file = null, doc_title = null, doc_subject = null, doc_keywords = null,
            doc_creator = null, doc_author = null, user_passwd = null, owner_passwd = null;

    boolean encrypt = false;
    boolean encryption_bits = PdfWriter.STRENGTH128BITS;
    int permissions = 0;
    boolean print_info = false;
    boolean print_keywords = false;

    /*
     *  parse options
     */
    for (int i = 0; i < args.length; i++) {
        if (args[i].equals("--title")) {
            doc_title = args[++i];
        } else if (args[i].equals("--subject")) {
            doc_subject = args[++i];
        } else if (args[i].equals("--keywords")) {
            doc_keywords = args[++i];
        } else if (args[i].equals("--creator")) {
            doc_creator = args[++i];
        } else if (args[i].equals("--author")) {
            doc_author = args[++i];
        } else if (args[i].equals("--print-info")) {
            print_info = true;
        } else if (args[i].equals("--print-keywords")) {
            print_keywords = true;
        } else if (args[i].equals("--user-password")) {
            encrypt = true;
            user_passwd = args[++i];
        } else if (args[i].equals("--master-password")) {
            encrypt = true;
            owner_passwd = args[++i];
        } else if (args[i].equals("--encryption-bits")) {
            i++;
            encrypt = true;

            if (args[i].equals("128")) {
                encryption_bits = PdfWriter.STRENGTH128BITS;
            } else if (args[i].equals("40")) {
                encryption_bits = PdfWriter.STRENGTH40BITS;
            } else {
                usage();
            }

            continue;
        } else if (args[i].equals("--permissions")) {
            i++;

            StringTokenizer st = new StringTokenizer(args[i], ",");
            while (st.hasMoreTokens()) {
                String s = st.nextToken();
                if (s.equals("print")) {
                    permissions |= PdfWriter.AllowPrinting;
                } else if (s.equals("degraded-print")) {
                    permissions |= PdfWriter.AllowDegradedPrinting;
                } else if (s.equals("copy")) {
                    permissions |= PdfWriter.AllowCopy;
                } else if (s.equals("modify-contents")) {
                    permissions |= PdfWriter.AllowModifyContents;
                } else if (s.equals("modify-annotations")) {
                    permissions |= PdfWriter.AllowModifyAnnotations;
                } else if (s.equals("assembly")) {
                    permissions |= PdfWriter.AllowAssembly;
                } else if (s.equals("fill-in")) {
                    permissions |= PdfWriter.AllowFillIn;
                } else if (s.equals("screen-readers")) {
                    permissions |= PdfWriter.AllowScreenReaders;
                } else {
                    warning("Unknown permission '" + s + "' ignored");
                }
            }

            continue;
        } else if (args[i].startsWith("--")) {
            error("Unknown option '" + args[i] + "'");
        } else if (input_file == null) {
            input_file = args[i];
        } else if (output_file == null) {
            output_file = args[i];
        } else {
            usage();
        }
    }

    if (!print_keywords) {
        if ((input_file == null) || (output_file == null)) {
            usage();
        }

        if (input_file.equals(output_file)) {
            error("Input and output files must be different");
        }
    }

    try {
        /*
         *  we create a reader for the input file
         */
        if (!print_keywords) {
            System.out.println("Reading " + input_file + "...");
        }

        PdfReader reader = new PdfReader(input_file);

        /*
         *  we retrieve the total number of pages
         */
        final int n = reader.getNumberOfPages();
        if (!print_keywords) {
            System.out.println("There are " + n + " pages in the original file.");
        }

        /*
         *  get the document information
         */
        final Map info = reader.getInfo();

        /*
         *  print the document information if asked to do so
         */
        if (print_info) {
            System.out.println("Document information:");

            final Iterator it = info.entrySet().iterator();
            while (it.hasNext()) {
                final Map.Entry entry = (Map.Entry) it.next();
                System.out.println(entry.getKey() + " = \"" + entry.getValue() + "\"");
            }
        }

        if (print_keywords) {
            String keywords = "" + info.get("Keywords");
            if ((null == keywords) || "null".equals(keywords)) {
                keywords = "";
            }

            System.out.println(keywords);
            System.exit(0);
        }

        /*
         *  if any meta data field is unspecified,
         *  copy the value from the input document
         */
        if (doc_title == null) {
            doc_title = (String) info.get("Title");
        }

        if (doc_subject == null) {
            doc_subject = (String) info.get("Subject");
        }

        if (doc_keywords == null) {
            doc_keywords = (String) info.get("Keywords");
        }

        if (doc_creator == null) {
            doc_creator = (String) info.get("Creator");
        }

        if (doc_author == null) {
            doc_author = (String) info.get("Author");
        }

        // null metadata field are simply set to the empty string
        if (doc_title == null) {
            doc_title = "";
        }

        if (doc_subject == null) {
            doc_subject = "";
        }

        if (doc_keywords == null) {
            doc_keywords = "";
        }

        if (doc_creator == null) {
            doc_creator = "";
        }

        if (doc_author == null) {
            doc_author = "";
        }

        /*
         *  step 1: creation of a document-object
         */
        final Document document = new Document(reader.getPageSizeWithRotation(1));

        /*
         *  step 2: we create a writer that listens to the document
         */
        final PdfCopy writer = new PdfCopy(document, new FileOutputStream(output_file));

        /*
         *  step 3.1: we add the meta data
         */
        document.addTitle(doc_title);
        document.addSubject(doc_subject);
        document.addKeywords(doc_keywords);
        document.addCreator(doc_creator);
        document.addAuthor(doc_author);

        /*
         *  step 3.2: we set up the protection and encryption parameters
         */
        if (encrypt) {
            writer.setEncryption(encryption_bits, user_passwd, owner_passwd, permissions);
        }

        /*
         *  step 4: we open the document
         */
        System.out.print("Writing " + output_file + "... ");
        document.open();

        PdfImportedPage page;

        int i = 0;

        // step 5: we add content
        while (i < n) {
            i++;
            page = writer.getImportedPage(reader, i);
            writer.addPage(page);

            System.out.print("[" + i + "] ");
        }

        final PRAcroForm form = reader.getAcroForm();
        if (form != null) {
            writer.copyAcroForm(reader);
        }

        System.out.println();

        // step 6: we close the document
        document.close();
    } catch (Exception e) {
        error(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:org.mnsoft.pdfocr.Wrapper.java

License:Open Source License

/**
 * Run the Wrapper.//from  www .  java2  s  .  com
 *
 * @throws IOException
 * @throws InterruptedException
 * @throws DocumentException
 */
@SuppressWarnings("rawtypes")
public void run() throws IOException, InterruptedException, DocumentException {
    RecursiveFileListIterator it = new RecursiveFileListIterator(new File(wd), new FileFilter(".pdf"));

    while (it.hasNext()) {
        final File originalFile = it.next();
        final String originalFilePath = originalFile.getAbsolutePath();

        /*
         * Open the reader on the original File
         */
        PdfReader readerOnOriginalFile;

        try {
            readerOnOriginalFile = new PdfReader(originalFilePath);
        } catch (Exception e) {
            log.error("! ERROR: " + e.getMessage() + " File: " + originalFilePath);

            continue;
        }

        /*
         * Get the document information
         */
        Map info = readerOnOriginalFile.getInfo();

        /*
         * Get the document creator. If the document
         * has already been worked on, continue with
         * the next document.
         */
        String doc_creator = (String) info.get("Creator");

        if (this.OCR_CREATOR.equals(doc_creator)) {
            log.debug(
                    "+ INFO: File " + originalFilePath + " had already been run trough OCR engine. Skipping.");

            continue;
        }

        /*
         * Get the document time stamp so that we can set it later.
         */
        final Date doc_timestamp = new Date(originalFile.lastModified());

        /*
         * Get the number of pages in the original file
         */
        int nOri = readerOnOriginalFile.getNumberOfPages();

        log.debug("+ Working on: " + originalFilePath + " (" + nOri + " pages).");

        final StringBuffer sb = new StringBuffer();

        sb.append(originalFilePath + " ... ");

        /*
         * Get the remaining meta data
         */
        String doc_title = ((String) info.get("Title") == null) ? "" : (String) info.get("Title");
        String doc_subject = ((String) info.get("Subject") == null) ? "" : (String) info.get("Subject");
        String doc_keywords = ((String) info.get("Keywords") == null) ? "" : (String) info.get("Keywords");
        String doc_author = ((String) info.get("Author") == null) ? "" : (String) info.get("Author");

        readerOnOriginalFile.close();

        /*
         * Set the creator to our marker
         */
        doc_creator = this.OCR_CREATOR;

        /*
         * Run the OCR Engine
         */
        File outputFileFromOCR = null;
        try {
            outputFileFromOCR = ocr(originalFile);
        } catch (Exception e) {
            log.error("! ERROR: " + e.getMessage());

            continue;
        }

        /*
         * Check for the result of the OCR Engine
         */
        if ((outputFileFromOCR == null) || !outputFileFromOCR.exists()) {
            continue;
        }

        log.debug("+ " + outputFileFromOCR.getAbsolutePath() + " has come out of the OCR engine.");

        /*
         * Create final output
         */

        /*
         * Create a temporary file and copy the source
         * file to it, to avoid UTF-8 encoding problems
         * on the filename confusing the OCR engine
         */
        final File temp = File.createTempFile("ocr", ".pdf", new File(this.TMP_DIR));
        temp.deleteOnExit();

        mergePDFs(originalFile, outputFileFromOCR, temp, doc_title, doc_subject, doc_keywords, doc_author,
                doc_creator);

        FileUtils.deleteQuietly(originalFile);

        FileUtils.moveFile(temp, new File(originalFilePath));

        /*
         * Set the file access time
         */
        if ("true".equals(getAttribute("KEEPTS"))) {
            if (originalFile.exists()) {
                originalFile.setLastModified(doc_timestamp.getTime() + 1000);
            }
        }

        /*
         * Finally, remove the temporary document
         */
        FileUtils.deleteQuietly(temp);
        FileUtils.deleteQuietly(outputFileFromOCR);
    }
}

From source file:org.pdfsam.guiclient.commons.business.loaders.callable.AddPdfDocument.java

License:Open Source License

/**
 * initialization of the document data/*from   w  w  w  . j  a v  a2s  .c o  m*/
 * @param reader
 * @param tableItem
 */
@SuppressWarnings("unchecked")
private void initTableItemDocumentData(PdfReader reader, PdfSelectionTableItem tableItem) {
    if (reader != null && tableItem != null) {
        HashMap<String, String> info = reader.getInfo();
        if (info != null && info.size() > 0) {
            for (Map.Entry<String, String> entry : info.entrySet()) {
                if (entry != null) {
                    String key = entry.getKey();
                    String value = StringUtils.trimToEmpty(entry.getValue());
                    if (key.equals(TITLE)) {
                        tableItem.getDocumentMetaData().setTitle(value);
                    } else if (key.equals(PRODUCER)) {
                        tableItem.getDocumentMetaData().setProducer(value);
                    } else if (key.equals(AUTHOR)) {
                        tableItem.getDocumentMetaData().setAuthor(value);
                    } else if (key.equals(SUBJECT)) {
                        tableItem.getDocumentMetaData().setSubject(value);
                    } else if (key.equals(CREATOR)) {
                        tableItem.getDocumentMetaData().setCreator(value);
                    } else if (key.equals(MODDATE)) {
                        tableItem.getDocumentMetaData().setModificationDate(value);
                    } else if (key.equals(CREATIONDATE)) {
                        tableItem.getDocumentMetaData().setCreationDate(value);
                    } else if (key.equals(KEYWORDS)) {
                        tableItem.getDocumentMetaData().setKeywords(value);
                    }
                }
            }
        }
    }
}

From source file:org.sejda.impl.itext.component.PdfStamperHandler.java

License:Apache License

/**
 * Adds the creator to the metadata taken from the reader and it sets it to the {@link PdfStamper}
 * /*from w w  w . j  a v a2  s .  co  m*/
 * @param reader
 */
public void setCreatorOnStamper(PdfReader reader) {
    @SuppressWarnings("unchecked")
    HashMap<String, String> meta = reader.getInfo();
    setMetadataOnStamper(meta);
}