List of usage examples for com.lowagie.text.pdf PdfReader isEncrypted
public boolean isEncrypted()
true
if the PDF is encrypted. From source file:org.jrimum.bopepo.pdf.PDFUtil.java
License:Apache License
/** * <p>/*w w w. j av a 2 s . c o m*/ * Junta varios arquivos pdf em um soh. * </p> * * @param pdfFiles * Lista de array de bytes * * @return Arquivo PDF em forma de byte * @since 0.2 */ @SuppressWarnings("unchecked") public static byte[] mergeFiles(List<byte[]> pdfFiles) { // retorno byte[] bytes = null; if (isNotNull(pdfFiles) && !pdfFiles.isEmpty()) { int pageOffset = 0; boolean first = true; ArrayList master = null; Document document = null; PdfCopy writer = null; ByteArrayOutputStream byteOS = null; try { byteOS = new ByteArrayOutputStream(); master = new ArrayList(); for (byte[] doc : pdfFiles) { if (isNotNull(doc)) { // cria-se um reader para cada documento PdfReader reader = new PdfReader(doc); if (reader.isEncrypted()) { reader = new PdfReader(doc, "".getBytes()); } reader.consolidateNamedDestinations(); // pega-se o numero total de paginas int n = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (isNotNull(bookmarks)) { if (pageOffset != 0) { SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); } master.addAll(bookmarks); } pageOffset += n; if (first) { // passo 1: criar um document-object document = new Document(reader.getPageSizeWithRotation(1)); // passo 2: criar um writer que observa o documento writer = new PdfCopy(document, byteOS); document.addAuthor("JRimum Group"); document.addSubject("JRimum Merged Document"); document.addCreator("JRimum Utilix"); // passo 3: abre-se o documento document.open(); first = false; } // passo 4: adciona-se o conteudo PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } } } if (master.size() > 0) { writer.setOutlines(master); } // passo 5: fecha-se o documento if (isNotNull(document)) { document.close(); } bytes = byteOS.toByteArray(); } catch (Exception e) { LOG.error("", e); } } return bytes; }
From source file:org.lucee.extension.pdf.PDFStruct.java
License:Open Source License
public Struct getInfo() { PdfReader pr = null; try {//from www .j a va 2s . c o m pr = getPdfReader(); // PdfDictionary catalog = pr.getCatalog(); int permissions = pr.getPermissions(); boolean encrypted = pr.isEncrypted(); Struct info = CFMLEngineFactory.getInstance().getCreationUtil().createStruct(); info.setEL("FilePath", getFilePath()); // access info.setEL("ChangingDocument", allowed(encrypted, permissions, PdfWriter.ALLOW_MODIFY_CONTENTS)); info.setEL("Commenting", allowed(encrypted, permissions, PdfWriter.ALLOW_MODIFY_ANNOTATIONS)); info.setEL("ContentExtraction", allowed(encrypted, permissions, PdfWriter.ALLOW_SCREENREADERS)); info.setEL("CopyContent", allowed(encrypted, permissions, PdfWriter.ALLOW_COPY)); info.setEL("DocumentAssembly", allowed(encrypted, permissions, PdfWriter.ALLOW_ASSEMBLY + PdfWriter.ALLOW_MODIFY_CONTENTS)); info.setEL("FillingForm", allowed(encrypted, permissions, PdfWriter.ALLOW_FILL_IN + PdfWriter.ALLOW_MODIFY_ANNOTATIONS)); info.setEL("Printing", allowed(encrypted, permissions, PdfWriter.ALLOW_PRINTING)); info.setEL("Secure", ""); info.setEL("Signing", allowed(encrypted, permissions, PdfWriter.ALLOW_MODIFY_ANNOTATIONS + PdfWriter.ALLOW_MODIFY_CONTENTS + PdfWriter.ALLOW_FILL_IN)); info.setEL("Encryption", encrypted ? "Password Security" : "No Security");// MUST info.setEL("TotalPages", CFMLEngineFactory.getInstance().getCastUtil().toDouble(pr.getNumberOfPages())); info.setEL("Version", "1." + pr.getPdfVersion()); info.setEL("permissions", "" + permissions); info.setEL("permiss", "" + PdfWriter.ALLOW_FILL_IN); info.setEL("Application", ""); info.setEL("Author", ""); info.setEL("CenterWindowOnScreen", ""); info.setEL("Created", ""); info.setEL("FitToWindow", ""); info.setEL("HideMenubar", ""); info.setEL("HideToolbar", ""); info.setEL("HideWindowUI", ""); info.setEL("Keywords", ""); info.setEL("Language", ""); info.setEL("Modified", ""); info.setEL("PageLayout", ""); info.setEL("Producer", ""); info.setEL("Properties", ""); info.setEL("ShowDocumentsOption", ""); info.setEL("ShowWindowsOption", ""); info.setEL("Subject", ""); info.setEL("Title", ""); info.setEL("Trapped", ""); // info HashMap imap = pr.getInfo(); Iterator it = imap.entrySet().iterator(); Map.Entry entry; while (it.hasNext()) { entry = (Entry) it.next(); info.setEL(CFMLEngineFactory.getInstance().getCastUtil().toString(entry.getKey(), null), entry.getValue()); } return info; } catch (PageException pe) { throw CFMLEngineFactory.getInstance().getExceptionUtil().createPageRuntimeException(pe); } finally { if (pr != null) pr.close(); } }
From source file:org.lucee.extension.pdf.tag.PDF.java
License:Open Source License
private void doActionThumbnail() throws PageException, IOException, DocumentException { required("pdf", "thumbnail", "source", source); PDFStruct doc = toPDFDocument(source, password, null); PdfReader pr = doc.getPdfReader(); boolean isEnc = pr.isEncrypted(); pr.close();// ww w . j a v a 2s . c o m if (isEnc) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // PDFUtil.concat(new PDFDocument[]{doc}, baos, true, true, true, (char)0); PDFUtil.encrypt(doc, baos, null, null, 0, PDFUtil.ENCRYPT_NONE); baos.close(); doc = new PDFStruct(baos.toByteArray(), doc.getResource(), null); } doc.setPages(pages); // scale if (scale < 1) throw engine.getExceptionUtil() .createApplicationException("value of attribute scale [" + scale + "] should be at least 1"); // destination if (destination == null) destination = engine.getResourceUtil().toResourceNotExisting(pageContext, "thumbnails"); // imagePrefix if (imagePrefix == null) { Resource res = doc.getResource(); if (res != null) { String n = res.getName(); int index = n.lastIndexOf('.'); if (index != -1) imagePrefix = n.substring(0, index); else imagePrefix = n; } else imagePrefix = "memory"; } // MUST password PDFUtil.writeImages(doc.getRaw(), doc.getPages(), destination, imagePrefix, format, scale, overwrite, resolution == RESOLUTION_HIGH, transparent); }
From source file:org.pdfsam.guiclient.commons.business.loaders.callable.AddPdfDocument.java
License:Open Source License
/** * * @param fileToAdd file to add/*from w w w . j a v a 2 s . co m*/ * @param password password to open the file * @return the item to add to the table */ PdfSelectionTableItem getPdfSelectionTableItem(File fileToAdd, String password, String pageSelection) { PdfSelectionTableItem tableItem = null; PdfReader pdfReader = null; if (fileToAdd != null) { tableItem = new PdfSelectionTableItem(); tableItem.setInputFile(fileToAdd); tableItem.setPassword(password); tableItem.setPageSelection(pageSelection); try { //fix 04/11/08 for memory usage pdfReader = new PdfReader(new RandomAccessFileOrArray(fileToAdd.getAbsolutePath()), (password != null) ? password.getBytes() : null); tableItem.setEncrypted(pdfReader.isEncrypted()); tableItem.setFullPermission(pdfReader.isOpenedWithFullPermissions()); if (tableItem.isEncrypted()) { tableItem.setPermissions(getPermissionsVerbose(pdfReader.getPermissions())); int cMode = pdfReader.getCryptoMode(); switch (cMode) { case PdfWriter.STANDARD_ENCRYPTION_40: tableItem.setEncryptionAlgorithm(EncryptionUtility.RC4_40); break; case PdfWriter.STANDARD_ENCRYPTION_128: tableItem.setEncryptionAlgorithm(EncryptionUtility.RC4_128); break; case PdfWriter.ENCRYPTION_AES_128: tableItem.setEncryptionAlgorithm(EncryptionUtility.AES_128); break; default: break; } } tableItem.setPagesNumber(Integer.toString(pdfReader.getNumberOfPages())); tableItem.setFileSize(fileToAdd.length()); tableItem.setPdfVersion(pdfReader.getPdfVersion()); tableItem.setSyntaxErrors(pdfReader.isRebuilt()); initTableItemDocumentData(pdfReader, tableItem); } catch (Exception e) { tableItem.setLoadedWithErrors(true); LOG.error(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(), "Error loading ") + fileToAdd.getAbsolutePath() + " :", e); } finally { if (pdfReader != null) { pdfReader.close(); pdfReader = null; } } } return tableItem; }