List of usage examples for com.itextpdf.text.pdf PdfReader close
public void 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 w w w. j a va2s . 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 w w w . ja v 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[] 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 ww w .j ava 2 s . c o m*/ 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(); } }
From source file:com.tommontom.pdfsplitter.PdfSplit.java
public void pdfSplitDropCopy(File[] files) throws IOException, DocumentException { // TODO Instead of hard code path, pass in as argument String path;// w w w .j a v a2 s .c o m path = files[0].getParent(); File[] listOfFiles = files; /* Stores the listing of the files */ System.out.println(directoryField); 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(".")); String[] fileNameNum = fileNameWithoutExt.split("-"); int fileNameNumOne = Integer.parseInt(fileNameNum[0]); int fileNameNumTwo = Integer.parseInt(fileNameNum[1]); int constant = 1; int numPages = fileNameNumTwo - fileNameNumOne; PdfReader pdfFileReader = new PdfReader(file.getPath()); Document document = new Document(PageSize.LETTER, 0, 0, 0, 0); ; /* instantiates a new document to be made */ // 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 = 0; j < numPages + 1; j++) { String FileName = fileNameWithoutExt; /* Dynamic file name */ PdfCopy copy = new PdfCopy(document, new FileOutputStream(path + "\\" + FileName + "(" + j + 1 + ")" + ".pdf")); 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(); } pdfFileReader.close(); } }
From source file:com.vectorprint.report.itext.SigningOutputStream.java
License:Open Source License
@Override public void secondPass(InputStream firstPass, OutputStream orig) throws IOException { PdfReader reader = null; try {// w w w. j a va2s . c o m reader = new PdfReader(firstPass); PdfStamper stamper = PdfStamper.createSignature(reader, orig, '\0'); outer.getDocumentStyler().configureVisualSignature(stamper.getSignatureAppearance()); stamper.close(); } catch (DocumentException | KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException | VectorPrintException ex) { throw new VectorPrintRuntimeException(ex); } finally { if (reader != null) { reader.close(); } } }
From source file:com.vectorprint.report.itext.TocOutputStream.java
License:Open Source License
@Override public void secondPass(InputStream firstPass, OutputStream orig) throws IOException { PdfReader reader = null; VectorPrintDocument vpd = (VectorPrintDocument) outer.getDocument(); try {/* w w w . j a va 2 s. c o m*/ reader = new PdfReader(firstPass); prepareToc(); // init fresh components for second pass styling StylerFactory _stylerFactory = outer.getStylerFactory().getClass().newInstance(); StylerFactoryHelper.SETTINGS_ANNOTATION_PROCESSOR.initSettings(_stylerFactory, outer.getSettings()); _stylerFactory.setLayerManager(outer.getElementProducer()); _stylerFactory.setImageLoader(outer.getElementProducer()); outer.getStyleHelper().setStylerFactory(_stylerFactory); EventHelper event = outer.getEventHelper().getClass().newInstance(); event.setItextStylerFactory(_stylerFactory); event.setElementProvider(outer.getElementProducer()); ((DefaultElementProducer) outer.getElementProducer()).setPh(event); Document d = new VectorPrintDocument(event, _stylerFactory, outer.getStyleHelper()); PdfWriter w = PdfWriter.getInstance(d, orig); w.setPageEvent(event); outer.getStyleHelper().setVpd((VectorPrintDocument) d); _stylerFactory.setDocument(d, w); DocumentStyler ds = _stylerFactory.getDocumentStyler(); outer.getStyleHelper().style(d, null, StyleHelper.toCollection(ds)); d.open(); ds.styleAfterOpen(d, null); List outline = SimpleBookmark.getBookmark(reader); if (!ds.getValue(DocumentSettings.TOCAPPEND, Boolean.class)) { printToc(d, w, vpd); if (outline != null) { int cur = w.getCurrentPageNumber(); SimpleBookmark.shiftPageNumbers(outline, cur, null); } d.newPage(); } outer.getSettings().put(ReportConstants.DEBUG, Boolean.FALSE.toString()); for (int p = 1; p <= reader.getNumberOfPages(); p++) { Image page = Image.getInstance(w.getImportedPage(reader, p)); page.setAbsolutePosition(0, 0); d.setPageSize(page); d.newPage(); Chunk i = new Chunk(" "); if (vpd.getToc().containsKey(p)) { Section s = null; for (Map.Entry<Integer, List<Section>> e : vpd.getToc().entrySet()) { if (e.getKey() == p) { s = e.getValue().get(0); break; } } i.setLocalDestination(s.getTitle().getContent()); } d.add(i); w.getDirectContent().addImage(page); w.freeReader(reader); } if (_stylerFactory.getDocumentStyler().getValue(DocumentSettings.TOCAPPEND, Boolean.class)) { printToc(d, w, vpd); } w.setOutlines(outline); if (outer.isWasDebug()) { event.setLastPage(outer.getWriter().getCurrentPageNumber()); d.setPageSize(new Rectangle(ItextHelper.mmToPts(297), ItextHelper.mmToPts(210))); d.setMargins(5, 5, 5, 5); d.newPage(); outer.getSettings().put(ReportConstants.DEBUG, Boolean.TRUE.toString()); event.setDebugHereAfter(true); DebugHelper.appendDebugInfo(w, d, outer.getSettings(), _stylerFactory); } d.close(); } catch (VectorPrintException | DocumentException | InstantiationException | IllegalAccessException ex) { throw new VectorPrintRuntimeException(ex); } finally { if (reader != null) { reader.close(); } } }
From source file:com.wabacus.system.assistant.PdfAssistant.java
License:Open Source License
private ByteArrayOutputStream showReportOneRowDataOnPdf(ReportRequest rrequest, Map<String, AbsReportType> mReportTypeObjs, IComponentConfigBean ccbean, PDFExportBean pdfbean, int rowidx) throws Exception { PdfReader pdfTplReader = null; InputStream istream = null;/*from w w w . j ava 2 s. co m*/ ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { istream = WabacusAssistant.getInstance().readFile(pdfbean.getPdftemplate()); if (istream == null) return null; pdfTplReader = new PdfReader(new BufferedInputStream(istream)); PdfStamper stamp = new PdfStamper(pdfTplReader, baos); /* ?? */ AcroFields form = stamp.getAcroFields(); form.addSubstitutionFont(bfChinese); boolean flag = true; if (pdfbean != null && pdfbean.getInterceptorObj() != null) { flag = pdfbean.getInterceptorObj().beforeDisplayPdfPageWithTemplate(ccbean, mReportTypeObjs, rowidx, stamp); } if (flag) { Map<String, Item> mFields = form.getFields(); if (mFields != null) { String fieldValueTmp = null; for (String fieldNameTmp : mFields.keySet()) { fieldValueTmp = getPdfFieldValueByName(rrequest, mReportTypeObjs, ccbean, fieldNameTmp, rowidx);//? if (pdfbean != null && pdfbean.getInterceptorObj() != null) { fieldValueTmp = pdfbean.getInterceptorObj().beforeDisplayFieldWithTemplate(ccbean, mReportTypeObjs, rowidx, stamp, fieldNameTmp, fieldValueTmp); } if (fieldValueTmp != null) form.setField(fieldNameTmp, fieldValueTmp); } } } if (pdfbean != null && pdfbean.getInterceptorObj() != null) { pdfbean.getInterceptorObj().afterDisplayPdfPageWithTemplate(ccbean, mReportTypeObjs, rowidx, stamp); } stamp.setFormFlattening(true); stamp.close(); } finally { try { if (istream != null) istream.close(); } catch (IOException e) { e.printStackTrace(); } if (pdfTplReader != null) pdfTplReader.close(); } return baos; }
From source file:com.wabacus.system.assistant.PdfAssistant.java
License:Open Source License
public void addPdfPageToDocument(PdfCopy pdfCopy, ByteArrayOutputStream baos) { if (baos == null) return;/*from w ww . jav a2s . co m*/ PdfReader readerTmp = null; try { readerTmp = new PdfReader(baos.toByteArray()); for (int i = 1, len = readerTmp.getNumberOfPages(); i <= len; i++) { pdfCopy.addPage(pdfCopy.getImportedPage(readerTmp, i)); } } catch (Exception e) { throw new WabacusRuntimeException("PDF", e); } finally { try { if (baos != null) baos.close(); } catch (IOException e) { e.printStackTrace(); } readerTmp.close(); } }
From source file:controller.CCInstance.java
License:Open Source License
public final int getNumberOfSignatures(final String pdfPath) { final KeyStore keystore = KeyStoreUtil.loadCacertsKeyStore(); try {/* ww w .ja va2 s . co m*/ keystore.load(null, null); final PdfReader reader = new PdfReader(pdfPath); final int numSigs = reader.getAcroFields().getSignatureNames().size(); reader.close(); return numSigs; } catch (IOException ex) { controller.Logger.getLogger().addEntry(ex); } catch (NoSuchAlgorithmException ex) { controller.Logger.getLogger().addEntry(ex); } catch (CertificateException ex) { controller.Logger.getLogger().addEntry(ex); } return -1; }
From source file:controller.CCInstance.java
License:Open Source License
public final int getCertificationLevel(final String filename) throws IOException { final PdfReader reader = new PdfReader(filename); final int certLevel = reader.getCertificationLevel(); reader.close(); return certLevel; }