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

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

Introduction

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

Prototype

boolean STRENGTH40BITS

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

Click Source Link

Usage

From source file:com.globalsight.everest.webapp.pagehandler.edit.inctxrv.pdf.PreviewPDFHelper.java

License:Apache License

public static File setCopyOnlyPermission(File p_file) throws DocumentException, IOException {
    PdfReader reader = new PdfReader(p_file.getAbsolutePath());
    String outPutFile = FileUtils.getPrefix(p_file.getAbsolutePath()) + "_hidebars.pdf";
    int n = reader.getNumberOfPages();
    Rectangle psize = reader.getPageSize(1);
    float width = psize.height();
    float height = psize.width();
    Document document = new Document(new Rectangle(width, height));
    PdfCopy writer = new PdfCopy(document, new FileOutputStream(outPutFile));
    writer.setEncryption(PdfWriter.STRENGTH40BITS, null, null, PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING);
    try {//from ww w  . ja va2 s  . c o  m
        document.open();
        int i = 0;
        while (i < n) {
            document.newPage();
            i++;
            PdfImportedPage page1 = writer.getImportedPage(reader, i);
            writer.addPage(page1);
        }
        document.close();
    } catch (IllegalArgumentException e) {
        if (!e.getMessage().equals("PdfReader not opened with owner password")) {
            throw new EnvoyServletException(e);
        } else {
            LOGGER.error(e.getMessage(), e);
        }
    }

    return new File(outPutFile);
}

From source file:com.globalsight.everest.webapp.pagehandler.edit.online.PreviewPDFPageHandler.java

License:Apache License

public static File setCopyOnlyPermission(File p_file) throws DocumentException, IOException {
    PdfReader reader = new PdfReader(p_file.getAbsolutePath());
    String outPutFile = FileUtils.getPrefix(p_file.getAbsolutePath()) + "_hidebars.pdf";
    int n = reader.getNumberOfPages();
    Rectangle psize = reader.getPageSize(1);
    float width = psize.height();
    float height = psize.width();
    Document document = new Document(new Rectangle(width, height));
    PdfCopy writer = new PdfCopy(document, new FileOutputStream(outPutFile));
    writer.setEncryption(PdfWriter.STRENGTH40BITS, null, null, PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING);
    try {/*from   w  w  w .j  a va2s.c om*/
        document.open();
        int i = 0;
        while (i < n) {
            document.newPage();
            i++;
            PdfImportedPage page1 = writer.getImportedPage(reader, i);
            writer.addPage(page1);
        }
        document.close();
    } catch (IllegalArgumentException e) {
        if (!e.getMessage().equals("PdfReader not opened with owner password")) {
            throw new EnvoyServletException(e);
        } else {
            CATEGORY.error(e.getMessage(), e);
        }
    }

    return new File(outPutFile);
}

From source file:org.areasy.common.doclet.document.Document.java

License:Open Source License

/**
 * Creates the document instance and initializes it.
 */// www.  j ava 2 s.c  o m
public static void initialize() throws Exception {
    // step 1: creation of a document-object
    pdfDocument = new com.lowagie.text.Document();

    float leftmargin = DefaultConfiguration.getFloat(ARG_DOC_MARGIN_LEFT, LEFT_MARGIN_WIDTH);
    float rightmargin = DefaultConfiguration.getFloat(ARG_DOC_MARGIN_RIGHT, RIGHT_MARGIN_WIDTH);
    float topmargin = DefaultConfiguration.getFloat(ARG_DOC_MARGIN_TOP, TOP_MARGIN_WIDTH);
    float bottommargin = DefaultConfiguration.getFloat(ARG_DOC_MARGIN_BOTTOM, BOTTOM_MARGIN_WIDTH);
    boolean printing = DefaultConfiguration.getBooleanConfigValue(ARG_ALLOW_PRINTING, ARG_VAL_YES);
    boolean encrypting = DefaultConfiguration.getBooleanConfigValue(ARG_ALLOW_ENCRYPTION, ARG_VAL_NO);

    // set left-, right-, top- and bottom-margins
    pdfDocument.setMargins(leftmargin, rightmargin, topmargin, bottommargin);

    // step 2:
    // we create a writer that listens to the document and directs a PDF-stream to a file
    pdfWriter = PdfWriter.getInstance(pdfDocument, new FileOutputStream(Doclet.getPdfFile()));

    if (encrypting) {
        if (printing)
            pdfWriter.setEncryption(PdfWriter.STRENGTH40BITS, null, null, PdfWriter.AllowPrinting);
        else
            pdfWriter.setEncryption(PdfWriter.STRENGTH40BITS, null, null, 0);
    }

    //set document data
    pdfDocument.addAuthor(DefaultConfiguration.getConfiguration().getString(ARG_DOC_AUTHOR, ""));
    pdfDocument.addSubject(DefaultConfiguration.getConfiguration().getString(ARG_DOC_TITLE, ""));
    pdfDocument.addTitle(DefaultConfiguration.getConfiguration().getString(ARG_DOC_TITLE, ""));
}

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

License:Open Source License

/**
 * @param args the command line arguments
 */// ww  w  . j  a va  2  s  .  co  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());
    }
}