List of usage examples for com.lowagie.text.pdf PdfReader PdfReader
public PdfReader(PdfReader reader)
From source file:org.mnsoft.pdfocr.CreatorSetter.java
License:Open Source License
/** * @param args/* w ww .j a v a 2 s.co m*/ * @throws DocumentException * @throws IOException * @throws IOException * @throws BadPdfFormatException */ @SuppressWarnings("rawtypes") public static void main(String[] args) throws DocumentException, IOException { /* * Verify arguments */ if ((args == null) || (args.length < 2)) { System.err.println("Usage: first parameter: Creator to set, following parameters: Files to work on."); System.exit(1); } final String creator = args[0]; for (int i = 1; i < args.length; i++) { final File f = new File(args[i]); if ((f == null) || !f.exists() || !f.isFile() || !f.getName().endsWith(".pdf")) { System.err.println("! ERROR: Could not read " + args[i] + " or this is not a .pdf"); continue; } final String p = f.getAbsolutePath(); /* * Open the reader */ PdfReader reader; try { reader = new PdfReader(p); } catch (Exception e) { System.err.println("! ERROR: " + e.getMessage() + " File: " + p); continue; } /* * Get the document information */ Map info = reader.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 (creator.equals(doc_creator)) { System.out.println("+ INFO: File " + p + " had already the right creator."); continue; } /* * Get the document time stamp so that we can set it later. */ final Date doc_timestamp = new Date(f.lastModified()); /* * Get the number of pages in the original file */ int nOri = reader.getNumberOfPages(); System.out.print("+ INFO: Working on: " + p + " (" + nOri + " pages) ... "); /* * 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"); reader.close(); /* * Set the creator to our marker */ doc_creator = creator; /* * Merge the new document with the meta * data from the original document */ try { reader = new PdfReader(p); } catch (Exception e) { System.err.println("! ERROR: " + e.getMessage() + " File: " + p); continue; } /* * Get the document information */ info = reader.getInfo(); /* * Get the document creator. If the document * has already been worked on, we assume we * have had a successful output from the OCR * engine */ String doc_creator_copy = (String) info.get("Creator"); if (creator.equals(doc_creator_copy)) { System.out.println(); continue; } /* * 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 */ PdfCopy writer = new PdfCopy(document, new FileOutputStream(p + ".tmp")); /* * Step 3: we add the meta data */ document.addTitle(doc_title); document.addSubject(doc_subject); document.addKeywords(doc_keywords); document.addCreator(creator); document.addAuthor(doc_author); /* * Step 4: we open the document */ document.open(); PdfImportedPage page; int j = 0; /* * Step 5: we add content */ while (j < nOri) { j++; page = writer.getImportedPage(reader, j); writer.addPage(page); System.out.print("[" + j + "] "); } PRAcroForm form = reader.getAcroForm(); if (form != null) { writer.copyAcroForm(reader); } System.out.println(); /* * Step 6: we close the document */ document.close(); reader.close(); /* * Set the file access time and * rename the file. */ File file = new File(p + ".tmp"); if (file.exists()) { deleteFile(p); file.setLastModified(doc_timestamp.getTime()); file.renameTo(new File(p)); } } }
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 2 s . 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 w w w . ja v a 2 s. c om*/ * * @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.mnsoft.pdfocr.Wrapper.java
License:Open Source License
@SuppressWarnings({ "unchecked", "rawtypes" }) private void mergePDFs(File foreground, File background, File newFile, String title, String subject, String keywords, String author, String creator) { log.debug("Merge " + foreground + " (FG) and " + background + " (BG) to " + newFile); final double threshold = ((Integer) StringUtility.StringToInteger(getAttribute("THRESHOLD"), 2)) .doubleValue();/* www . j av a2 s .c om*/ try { /* * Foreground: Original Image. * Background: OCR'd Text */ final PdfReader fg = new PdfReader(foreground.getAbsolutePath()); final PdfReader bg = new PdfReader(background.getAbsolutePath()); /* * Count pages for foreground and background */ final int fg_num_pages = fg.getNumberOfPages(); final int bg_num_pages = bg.getNumberOfPages(); if (fg_num_pages != bg_num_pages) { log.error( "! Foreground and background have different number of pages. This should really not happen."); } /* * The output document */ final PdfStamper fg_writer = new PdfStamper(fg, new FileOutputStream(newFile)); /* * Create a PdfTemplate from the first page of mark * (PdfImportedPage is derived from PdfTemplate) */ PdfImportedPage bg_page = null; for (int i = 0; i < fg_num_pages;) { ++i; System.out.print(" [" + i + "]"); final byte[] fg_page_content = fg.getPageContent(i); final byte[] bg_page_content = bg.getPageContent(i); final int bg_size = bg_page_content.length; final int fg_size = fg_page_content.length; /* * If we're not explicitly merging, we're merging * the document with itself only anyway. */ if (!"true".equals(getAttribute("mergefiles"))) { continue; } /* * Modification 20130904 * * We want to scan only what's not been generated by a number of * generators. So, until now, the generator of whom we wanted to * ignore files was ocr, i.e. the one we set ourselves. Now, we * have seen that when we run an OCR on a "pdf+text" file, as we * collate in post the file with its image, we get an overlapping * text which is not pixel correct, i.e. which makes the PDF appear * not nicely. * * If the background image is not at least threshold times as large as * the foreground image, we assume we've been working on a * page that was plain text already, and don't add the image * to the background. */ if ((bg_size / fg_size) <= threshold) { log.debug("! Not adding background for page " + i + " since background size (" + bg_size + ") not different enough from foreground size (" + fg_size + ")."); continue; } bg_page = fg_writer.getImportedPage(bg, i); final PdfContentByte contentByte = fg_writer.getUnderContent(i); contentByte.addTemplate(bg_page, 0, 0); } HashMap map = fg_writer.getMoreInfo(); if (map == null) { map = new HashMap(); } if (title != null) { map.put("Title", title); } if (subject != null) { map.put("Subject", subject); } if (keywords != null) { map.put("Keywords", keywords); } if (author != null) { map.put("Author", author); } if (creator != null) { map.put("Creator", creator); } fg_writer.setMoreInfo(map); fg_writer.close(); System.out.println(""); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.nabucco.framework.template.impl.service.pdf.util.MergePdfUtil.java
License:Open Source License
public static void concatPDFs(List<InputStream> streamOfPDFFiles, OutputStream outputStream, boolean paginate) { Document document = new Document(); try {// www . j a va 2 s . c o m List<InputStream> pdfs = streamOfPDFFiles; List<PdfReader> readers = new ArrayList<PdfReader>(); int totalPages = 0; Iterator<InputStream> iteratorPDFs = pdfs.iterator(); // Create Readers for the pdfs. while (iteratorPDFs.hasNext()) { InputStream pdf = iteratorPDFs.next(); PdfReader pdfReader = new PdfReader(pdf); readers.add(pdfReader); totalPages += pdfReader.getNumberOfPages(); } // Create a writer for the outputstream PdfWriter writer = PdfWriter.getInstance(document, outputStream); document.open(); PdfContentByte cb = writer.getDirectContent(); // Holds the PDF // data PdfImportedPage page; int currentPageNumber = 0; int pageOfCurrentReaderPDF = 0; Iterator<PdfReader> iteratorPDFReader = readers.iterator(); // Loop through the PDF files and add to the output. while (iteratorPDFReader.hasNext()) { PdfReader pdfReader = iteratorPDFReader.next(); // Create a new page in the target for each source page. while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) { document.newPage(); pageOfCurrentReaderPDF++; currentPageNumber++; page = writer.getImportedPage(pdfReader, pageOfCurrentReaderPDF); cb.addTemplate(page, 0, 0); } pageOfCurrentReaderPDF = 0; } outputStream.flush(); document.close(); outputStream.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (document.isOpen()) document.close(); try { if (outputStream != null) outputStream.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } }
From source file:org.nuxeo.ecm.platform.signature.core.sign.SignatureServiceImpl.java
License:Open Source License
@Override public Blob signPDF(Blob pdfBlob, DocumentModel user, String keyPassword, String reason) throws ClientException { CertService certService = Framework.getLocalService(CertService.class); CUserService cUserService = Framework.getLocalService(CUserService.class); try {//ww w . j ava 2s. c o m File outputFile = File.createTempFile("signed-", ".pdf"); Blob blob = Blobs.createBlob(outputFile, MIME_TYPE_PDF); Framework.trackFile(outputFile, blob); PdfReader pdfReader = new PdfReader(pdfBlob.getStream()); List<X509Certificate> pdfCertificates = getCertificates(pdfReader); // allows for multiple signatures PdfStamper pdfStamper = PdfStamper.createSignature(pdfReader, new FileOutputStream(outputFile), '\0', null, true); PdfSignatureAppearance pdfSignatureAppearance = pdfStamper.getSignatureAppearance(); String userID = (String) user.getPropertyValue("user:username"); AliasWrapper alias = new AliasWrapper(userID); KeyStore keystore = cUserService.getUserKeystore(userID, keyPassword); Certificate certificate = certService.getCertificate(keystore, alias.getId(AliasType.CERT)); KeyPair keyPair = certService.getKeyPair(keystore, alias.getId(AliasType.KEY), alias.getId(AliasType.CERT), keyPassword); if (certificatePresentInPDF(certificate, pdfCertificates)) { X509Certificate userX509Certificate = (X509Certificate) certificate; String message = ALREADY_SIGNED_BY + userX509Certificate.getSubjectDN(); log.debug(message); throw new AlreadySignedException(message); } List<Certificate> certificates = new ArrayList<Certificate>(); certificates.add(certificate); Certificate[] certChain = certificates.toArray(new Certificate[0]); pdfSignatureAppearance.setCrypto(keyPair.getPrivate(), certChain, null, PdfSignatureAppearance.SELF_SIGNED); if (StringUtils.isBlank(reason)) { reason = getSigningReason(); } pdfSignatureAppearance.setReason(reason); pdfSignatureAppearance.setAcro6Layers(true); Font layer2Font = FontFactory.getFont(FontFactory.TIMES, getSignatureLayout().getTextSize(), Font.NORMAL, new Color(0x00, 0x00, 0x00)); pdfSignatureAppearance.setLayer2Font(layer2Font); pdfSignatureAppearance.setRender(PdfSignatureAppearance.SignatureRenderDescription); pdfSignatureAppearance.setVisibleSignature(getNextCertificatePosition(pdfReader, pdfCertificates), 1, null); pdfStamper.close(); // closes the file log.debug("File " + outputFile.getAbsolutePath() + " created and signed with " + reason); return blob; } catch (IOException e) { throw new SignException(e); } catch (DocumentException e) { // iText PDF stamping throw new SignException(e); } catch (IllegalArgumentException e) { if (String.valueOf(e.getMessage()).contains("PdfReader not opened with owner password")) { // iText PDF reading throw new SignException("PDF is password-protected"); } throw new SignException(e); } }
From source file:org.nuxeo.ecm.platform.signature.core.sign.SignatureServiceImpl.java
License:Open Source License
protected List<X509Certificate> getCertificates(Blob pdfBlob) throws SignException { try {//from w w w . j a v a 2 s. co m PdfReader pdfReader = new PdfReader(pdfBlob.getStream()); return getCertificates(pdfReader); } catch (IOException e) { String message = ""; if (e.getMessage().equals("PDF header signature not found.")) { message = "PDF seems to be corrupted"; } throw new SignException(message, e); } }
From source file:org.obiba.onyx.marble.core.service.FdfProducer.java
License:Open Source License
/** * Builds a FDF file that can be used to merge with the a consent PDF template. * //from w ww . j av a 2 s.c om * @param pdfUrl the URL of the PDF file * @param acceptUrl the URL to submit the PDF when the consent is accepted * @param refuseUrl the URL to submit the PDF when the consent is refused * @return a FDF formated byte array. * @throws IOException when an unexpected error occurs */ public byte[] buildFdf(String pdfUrl, String acceptUrl, String refuseUrl) throws IOException { buildReplaceContext(acceptUrl, refuseUrl); PdfReader pdfReader = new PdfReader(getPdfTemplate()); log.debug("PDF template has {} fields.", pdfReader.getAcroFields().getFields().size()); CustomFdfWriter fdf = new CustomFdfWriter(); // Initialize the FDF with fields that already have a value. fdf.setFields(pdfReader); fdf.setFile(pdfUrl); try { setFields(pdfReader.getAcroFields(), fdf); } catch (DocumentException e) { log.error("An error occurred during FDF file generation.", e); throw new RuntimeException(e); } ByteArrayOutputStream output = new ByteArrayOutputStream(); fdf.writeTo(output); output.close(); pdfReader.close(); return output.toByteArray(); }
From source file:org.obiba.onyx.marble.core.service.impl.DefaultActiveConsentServiceImpl.java
License:Open Source License
@SuppressWarnings("unchecked") public boolean validateElectronicConsent() { byte[] pdfForm = consent.getPdfForm(); if (pdfForm != null) { // Access PDF content with IText library. PdfReader reader;/*from ww w . j a va 2 s .com*/ try { reader = new PdfReader(pdfForm); } catch (IOException ex) { log.error("Could not read PDF consent form", ex); throw new RuntimeException(ex); } // Get the PDF form fields. AcroFields form = reader.getAcroFields(); // Make sure that all mandatory fields have been filled. Collection<String> fieldNameList = form.getFields().keySet(); for (String fieldName : fieldNameList) { if (isMandatoryField(form, fieldName)) { if (Strings.isEmpty(form.getField(fieldName))) { log.debug("The following mandatory field was not filled in : {}", fieldName); return false; } } } return true; // Invalid if PDF form does not exist. } else { return false; } }
From source file:org.obiba.onyx.marble.domain.consent.Consent.java
License:Open Source License
private PdfReader getPdfReader() { if (cachedPdfReader == null) { byte[] pdfForm = getPdfForm(); // Access PDF content with IText library. try {//from w ww .jav a 2 s.com cachedPdfReader = new PdfReader(pdfForm); } catch (IOException ex) { throw new RuntimeException(ex); } } return cachedPdfReader; }