List of usage examples for com.lowagie.text.pdf PdfCopy copyAcroForm
public void copyAcroForm(PdfReader reader) throws IOException, BadPdfFormatException
From source file:QMSMultiQuoteController.java
License:Open Source License
private byte[] concatPDF(ArrayList pdfFilesList) { byte[] buffer = null; FileOutputStream filestream = null; try {// w w w .j a v a 2 s .c o m int i = 1; Document document = null; PdfCopy pdfcopy = null; if (pdfFilesList.size() > 1) { String s = (String) pdfFilesList.get(0); int pdfFilListSize = pdfFilesList.size(); for (; i < pdfFilListSize; i++) { PdfReader pdfreader = new PdfReader((String) pdfFilesList.get(i)); int j = pdfreader.getNumberOfPages(); if (i == 1) { document = new Document(pdfreader.getPageSizeWithRotation(1)); filestream = new FileOutputStream(s); pdfcopy = new PdfCopy(document, filestream); document.open(); } PdfImportedPage pdfimportedpage = null; for (int k = 0; k < j;) { k++; pdfimportedpage = pdfcopy.getImportedPage(pdfreader, k); if (pdfimportedpage != null) { pdfcopy.addPage(pdfimportedpage); } } com.lowagie.text.pdf.PRAcroForm pracroform = pdfreader.getAcroForm(); if (pracroform != null) pdfcopy.copyAcroForm(pdfreader); } if (document != null) { document.close(); } FileInputStream inputStream = new FileInputStream(s); buffer = new byte[inputStream.available()]; inputStream.read(buffer); } // buffer = pdfcopy.getDirectContent(); } catch (Exception exception) { exception.printStackTrace(); } return buffer; }
From source file:br.gov.jfrj.itextpdf.Documento.java
License:Open Source License
public static byte[] getDocumento(ExMobil mob, ExMovimentacao mov, boolean completo, boolean estampar, String hash, byte[] certificado) throws Exception { final ByteArrayOutputStream bo2 = new ByteArrayOutputStream(); PdfReader reader;//from w w w .j av a 2s . c om int n; int pageOffset = 0; ArrayList master = new ArrayList(); int f = 0; Document document = null; PdfCopy writer = null; int nivelInicial = 0; // if (request.getRequestURI().indexOf("/completo/") == -1) { // return getPdf(docvia, mov != null ? mov : docvia.getExDocumento(), // mov != null ? mov.getNumVia() : docvia.getNumVia(), null, // null, request); // } List<ExArquivoNumerado> ans = mob.filtrarArquivosNumerados(mov, completo); if (!completo && !estampar && ans.size() == 1) { if (certificado != null) { CdService cdService = Service.getCdService(); return cdService.produzPacoteAssinavel(certificado, null, ans.get(0).getArquivo().getPdf(), true, ExDao.getInstance().getServerDateTime()); } else if (hash != null) { // Calcula o hash do documento String alg = hash; MessageDigest md = MessageDigest.getInstance(alg); md.update(ans.get(0).getArquivo().getPdf()); return md.digest(); } else { return ans.get(0).getArquivo().getPdf(); } } try { for (ExArquivoNumerado an : ans) { // byte[] ab = getPdf(docvia, an.getArquivo(), an.getNumVia(), // an // .getPaginaInicial(), an.getPaginaFinal(), request); String sigla = mob.getSigla(); if (an.getArquivo() instanceof ExMovimentacao) { ExMovimentacao m = (ExMovimentacao) an.getArquivo(); if (m.getExTipoMovimentacao().getId() == ExTipoMovimentacao.TIPO_MOVIMENTACAO_JUNTADA) sigla = m.getExMobil().getSigla(); } else { sigla = an.getMobil().getSigla(); } byte[] ab = !estampar ? an.getArquivo().getPdf() : stamp(an.getArquivo().getPdf(), sigla, an.getArquivo().isRascunho(), an.getArquivo().isCancelado(), an.getArquivo().isSemEfeito(), an.getArquivo().isInternoProduzido(), an.getArquivo().getQRCode(), an.getArquivo().getMensagem(), an.getPaginaInicial(), an.getPaginaFinal(), an.getOmitirNumeracao(), SigaExProperties.getTextoSuperiorCarimbo(), mob.getExDocumento().getOrgaoUsuario().getDescricao()); // we create a reader for a certain document reader = new PdfReader(ab); reader.consolidateNamedDestinations(); // we retrieve the total number of pages n = reader.getNumberOfPages(); // List bookmarks = SimpleBookmark.getBookmark(reader); // master.add(new Bookmark) // if (bookmarks != null) { // if (pageOffset != 0) // SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, // null); // master.addAll(bookmarks); // } if (f == 0) { // step 1: creation of a document-object document = new Document(reader.getPageSizeWithRotation(1)); // step 2: we create a writer that listens to the // document writer = new PdfCopy(document, bo2); writer.setFullCompression(); // writer.setViewerPreferences(PdfWriter.PageModeUseOutlines); // step 3: we open the document document.open(); nivelInicial = an.getNivel(); } // PdfOutline root = writer.getDirectContent().getRootOutline(); // PdfContentByte cb = writer.getDirectContent(); // PdfDestination destination = new // PdfDestination(PdfDestination.FITH, position); // step 4: we add content PdfImportedPage page; for (int j = 0; j < n;) { ++j; page = writer.getImportedPage(reader, j); writer.addPage(page); if (j == 1) { // PdfContentByte cb = writer.getDirectContent(); // PdfOutline root = cb.getRootOutline(); // PdfOutline oline1 = new PdfOutline(root, // PdfAction.gotoLocalPage("1", false),"Chapter 1"); HashMap map = new HashMap(); map.put("Title", an.getNome()); map.put("Action", "GoTo"); map.put("Page", j + pageOffset + ""); map.put("Kids", new ArrayList()); ArrayList mapPai = master; for (int i = 0; i < an.getNivel() - nivelInicial; i++) { mapPai = ((ArrayList) ((HashMap) mapPai.get(mapPai.size() - 1)).get("Kids")); } mapPai.add(map); } } PRAcroForm form = reader.getAcroForm(); if (form != null) writer.copyAcroForm(reader); pageOffset += n; f++; } if (!master.isEmpty()) writer.setOutlines(master); // PdfDictionary info = writer.getInfo(); // info.put(PdfName.MODDATE, null); // info.put(PdfName.CREATIONDATE, null); // info.put(PdfName.ID, null); document.close(); } catch (Exception e) { e.printStackTrace(); } return bo2.toByteArray(); }
From source file:com.benfante.minimark.blo.AssessmentPdfBuilder.java
License:Apache License
/** * Build the contatenated PDF for a list of assessments. * * @param assessments The assessments/*from ww w. jav a 2 s .c om*/ * @param baseUrl The base URL for retrieving images and resource. If null it will not be set. * @param locale The locale for producing the document. Id null, the default locale will be used. * @return The PDF document. */ public byte[] buildPdf(List<AssessmentFilling> assessments, String baseUrl, Locale locale) throws Exception { ByteArrayOutputStream pdfos = new ByteArrayOutputStream(); List master = new ArrayList(); Document document = null; PdfCopy writer = null; PdfOutline rootOutline = null; try { int f = 0; int pageOffset = 0; for (AssessmentFilling assessment : assessments) { // we create a reader for a certain document PdfReader reader = new PdfReader(buildPdf(assessment, baseUrl, locale)); reader.consolidateNamedDestinations(); // we retrieve the total number of pages int n = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { if (pageOffset != 0) { SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); } master.addAll(bookmarks); } pageOffset += n; if (f == 0) { // step 1: creation of a document-object document = new Document(reader.getPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document writer = new PdfCopy(document, pdfos); writer.setViewerPreferences(PdfWriter.PageModeUseOutlines); // step 3: we open the document document.open(); // initialize rootOutline for assessment bookmarks creation PdfContentByte cb = writer.getDirectContent(); rootOutline = cb.getRootOutline(); } // step 4: we add content new PdfOutline(rootOutline, new PdfDestination(PdfDestination.FIT), assessment.getLastName() + " " + assessment.getFirstName()); PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } PRAcroForm form = reader.getAcroForm(); if (form != null) { writer.copyAcroForm(reader); } f++; } } finally { if (pdfos != null) { try { pdfos.close(); } catch (IOException ioe) { } } if (!master.isEmpty()) { writer.setOutlines(master); } // step 5: we close the document if (document != null && document.isOpen()) { document.close(); } } return pdfos.toByteArray(); }
From source file:com.sapienter.jbilling.server.invoice.PaperInvoiceBatchBL.java
License:Open Source License
/** * Takes a list of invoices and replaces the individual PDF files for one * single PDF in the destination directory. * @param destination// w w w . j a v a2s .co m * @param prefix * @param entityId * @param invoices * @throws PdfFormatException * @throws IOException */ public void compileInvoiceFiles(String destination, String prefix, Integer entityId, Integer[] invoices) throws DocumentException, IOException { String filePrefix = Util.getSysProp("base_dir") + "invoices/" + entityId + "-"; String outFile = destination + prefix + "-batch.pdf"; int pageOffset = 0; ArrayList master = new ArrayList(); Document document = null; PdfCopy writer = null; for (int f = 0; f < invoices.length; f++) { // we create a reader for a certain document PdfReader reader = new PdfReader(filePrefix + invoices[f] + "-invoice.pdf"); reader.consolidateNamedDestinations(); // we retrieve the total number of pages int numberOfPages = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { if (pageOffset != 0) SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); master.addAll(bookmarks); } pageOffset += numberOfPages; if (f == 0) { // step 1: creation of a document-object document = new Document(reader.getPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document writer = new PdfCopy(document, new FileOutputStream(outFile)); // step 3: we open the document document.open(); } // step 4: we add content PdfImportedPage page; for (int i = 0; i < numberOfPages;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } PRAcroForm form = reader.getAcroForm(); if (form != null) writer.copyAcroForm(reader); //release and delete writer.freeReader(reader); reader.close(); File file = new File(filePrefix + invoices[f] + "-invoice.pdf"); file.delete(); } if (!master.isEmpty()) writer.setOutlines(master); // step 5: we close the document if (document != null) { document.close(); } else { LOG.warn("document == null"); } LOG.debug("PDF batch file is ready " + outFile); }
From source file:com.servoy.extensions.plugins.pdf_output.PDFProvider.java
License:Open Source License
/** * Combine multiple protected PDF docs into one. * Note: this function may fail when creating large PDF files due to lack of available heap memory. To compensate, please configure the application server with more heap memory via -Xmx parameter. * * @sample/*from w w w .ja va 2 s . c o m*/ * pdf_blob_column = combineProtectedPDFDocuments(new Array(pdf_blob1,pdf_blob2,pdf_blob3), new Array(pdf_blob1_pass,pdf_blob2_pass,pdf_blob3_pass)); * * @param pdf_docs_bytearrays the array of documents to combine * @param pdf_docs_passwords an array of passwords to use */ public byte[] js_combineProtectedPDFDocuments(Object[] pdf_docs_bytearrays, Object[] pdf_docs_passwords) { if (pdf_docs_bytearrays == null || pdf_docs_bytearrays.length == 0) return null; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { int pageOffset = 0; List master = new ArrayList(); Document document = null; PdfCopy writer = null; for (int f = 0; f < pdf_docs_bytearrays.length; f++) { if (!(pdf_docs_bytearrays[f] instanceof byte[])) continue; byte[] pdf_file = (byte[]) pdf_docs_bytearrays[f]; // we create a reader for a certain document byte[] password = null; if (pdf_docs_passwords != null && pdf_docs_passwords.length > f && pdf_docs_passwords[f] != null) { if (pdf_docs_passwords[f] instanceof String) password = pdf_docs_passwords[f].toString().getBytes(); } PdfReader reader = new PdfReader(pdf_file, password); reader.consolidateNamedDestinations(); // we retrieve the total number of pages int n = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { if (pageOffset != 0) { SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); } master.addAll(bookmarks); } pageOffset += n; if (writer == null) { // step 1: creation of a document-object document = new Document(reader.getPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document writer = new PdfCopy(document, baos); // step 3: we open the document document.open(); } // step 4: we add content PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } PRAcroForm form = reader.getAcroForm(); if (form != null) writer.copyAcroForm(reader); } if (writer != null && document != null) { if (master.size() > 0) writer.setOutlines(master); // step 5: we close the document document.close(); } return baos.toByteArray(); } catch (Throwable e) { Debug.error(e); throw new RuntimeException("Error combinding pdf documents: " + e.getMessage(), e); //$NON-NLS-1$ } }
From source file:com.silverpeas.importExport.control.ImportExport.java
License:Open Source License
/** * @param userDetail/*w ww . j a v a 2s. c o m*/ * @param itemsToExport * @return * @throws ImportExportException */ public ExportPDFReport processExportPDF(UserDetail userDetail, List<WAAttributeValuePair> itemsToExport, NodePK rootPK) throws ImportExportException { ExportPDFReport report = new ExportPDFReport(); report.setDateDebut(new Date()); PublicationsTypeManager pubTypeManager = new PublicationsTypeManager(); String fileExportName = generateExportDirName(userDetail, "fusion"); String tempDir = FileRepositoryManager.getTemporaryPath(); File fileExportDir = new File(tempDir + fileExportName); if (!fileExportDir.exists()) { try { FileFolderManager.createFolder(fileExportDir); } catch (UtilException ex) { throw new ImportExportException("ImportExport", "importExport.EX_CANT_CREATE_FOLDER", ex); } } File pdfFileName = new File(tempDir + fileExportName + ".pdf"); try { // cration des rpertoires avec le nom des thmes et des publications List<AttachmentDetail> pdfList = pubTypeManager.processPDFExport(report, userDetail, itemsToExport, fileExportDir.getPath(), true, rootPK); try { int pageOffset = 0; List master = new ArrayList(); Document document = null; PdfCopy writer = null; if (!pdfList.isEmpty()) { boolean firstPage = true; for (AttachmentDetail attDetail : pdfList) { PdfReader reader = null; try { reader = new PdfReader( fileExportDir.getPath() + File.separatorChar + attDetail.getLogicalName()); } catch (IOException ioe) { // Attached file is not physically present on disk, ignore it and log event SilverTrace.error("importExport", "PublicationTypeManager.processExportPDF", "CANT_FIND_PDF_FILE", "PDF file '" + attDetail.getLogicalName() + "' is not present on disk", ioe); } if (reader != null) { reader.consolidateNamedDestinations(); int nbPages = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { if (pageOffset != 0) { SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); } master.addAll(bookmarks); } pageOffset += nbPages; if (firstPage) { document = new Document(reader.getPageSizeWithRotation(1)); writer = new PdfCopy(document, new FileOutputStream(pdfFileName)); document.open(); firstPage = false; } for (int i = 1; i <= nbPages; i++) { try { PdfImportedPage page = writer.getImportedPage(reader, i); writer.addPage(page); } catch (Exception e) { // Can't import PDF file, ignore it and log event SilverTrace.error("importExport", "PublicationTypeManager.processExportPDF", "CANT_MERGE_PDF_FILE", "PDF file is " + attDetail.getLogicalName(), e); } } PRAcroForm form = reader.getAcroForm(); if (form != null) { writer.copyAcroForm(reader); } } } if (!master.isEmpty()) { writer.setOutlines(master); } writer.flush(); document.close(); } else { return null; } } catch (BadPdfFormatException e) { // Erreur lors de la copie throw new ImportExportException("ImportExport", "root.EX_CANT_WRITE_FILE", e); } catch (DocumentException e) { // Impossible de copier le document throw new ImportExportException("ImportExport", "root.EX_CANT_WRITE_FILE", e); } } catch (IOException e) { // Pb avec le rpertoire de destination throw new ImportExportException("ImportExport", "root.EX_CANT_WRITE_FILE", e); } report.setPdfFileName(pdfFileName.getName()); report.setPdfFileSize(pdfFileName.length()); report.setPdfFilePath(FileServerUtils.getUrlToTempDir(pdfFileName.getName())); report.setDateFin(new Date()); return report; }
From source file:lucee.runtime.text.pdf.PDFUtil.java
License:Open Source License
/** * @param docs/*from w w w .j av a2 s .c o m*/ * @param os * @param removePages if true, pages defined in PDFDocument will be removed, otherwise all other pages will be removed * @param version * @throws PageException * @throws IOException * @throws DocumentException */ public static void concat(PDFDocument[] docs, OutputStream os, boolean keepBookmark, boolean removePages, boolean stopOnError, char version) throws PageException, IOException, DocumentException { Document document = null; PdfCopy writer = null; PdfReader reader; Set pages; boolean isInit = false; PdfImportedPage page; try { int pageOffset = 0; ArrayList master = new ArrayList(); for (int i = 0; i < docs.length; i++) { // we create a reader for a certain document pages = docs[i].getPages(); try { reader = docs[i].getPdfReader(); } catch (Throwable t) { if (!stopOnError) continue; throw Caster.toPageException(t); } reader.consolidateNamedDestinations(); // we retrieve the total number of pages int n = reader.getNumberOfPages(); List bookmarks = keepBookmark ? SimpleBookmark.getBookmark(reader) : null; if (bookmarks != null) { removeBookmarks(bookmarks, pages, removePages); if (pageOffset != 0) SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); master.addAll(bookmarks); } if (!isInit) { isInit = true; document = new Document(reader.getPageSizeWithRotation(1)); writer = new PdfCopy(document, os); if (version != 0) writer.setPdfVersion(version); document.open(); } for (int y = 1; y <= n; y++) { if (pages != null && removePages == pages.contains(Integer.valueOf(y))) { continue; } pageOffset++; page = writer.getImportedPage(reader, y); writer.addPage(page); } PRAcroForm form = reader.getAcroForm(); if (form != null) writer.copyAcroForm(reader); } if (master.size() > 0) writer.setOutlines(master); } finally { IOUtil.closeEL(document); } }
From source file:lucee.runtime.text.pdf.PDFUtil.java
License:Open Source License
public static void encrypt(PDFDocument doc, OutputStream os, String newUserPassword, String newOwnerPassword, int permissions, int encryption) throws ApplicationException, DocumentException, IOException { byte[] user = newUserPassword == null ? null : newUserPassword.getBytes(); byte[] owner = newOwnerPassword == null ? null : newOwnerPassword.getBytes(); PdfReader pr = doc.getPdfReader();// ww w .j a v a2 s. c om List bookmarks = SimpleBookmark.getBookmark(pr); int n = pr.getNumberOfPages(); Document document = new Document(pr.getPageSizeWithRotation(1)); PdfCopy writer = new PdfCopy(document, os); if (encryption != ENCRYPT_NONE) writer.setEncryption(user, owner, permissions, encryption); document.open(); PdfImportedPage page; for (int i = 1; i <= n; i++) { page = writer.getImportedPage(pr, i); writer.addPage(page); } PRAcroForm form = pr.getAcroForm(); if (form != null) writer.copyAcroForm(pr); if (bookmarks != null) writer.setOutlines(bookmarks); document.close(); }
From source file:org.jaffa.modules.printing.services.MultiFormPrintEngine.java
License:Open Source License
/** * Merge a list of generated Pdf Documents together * @param documents //from ww w.ja v a 2 s . co m * @throws java.io.IOException * @throws com.lowagie.text.DocumentException * @return byte[] */ public static byte[] mergePdf(List<byte[]> documents) throws IOException, DocumentException { int pageOffset = 0; ArrayList master = new ArrayList(); ByteArrayOutputStream output = new ByteArrayOutputStream(); Document document = null; PdfCopy writer = null; boolean first = true; for (Iterator<byte[]> it = documents.iterator(); it.hasNext();) { // we create a reader for a certain document PdfReader reader = new PdfReader(it.next()); reader.consolidateNamedDestinations(); // we retrieve the total number of pages int n = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { if (pageOffset != 0) SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset, null); master.addAll(bookmarks); } pageOffset += n; if (first) { first = false; // step 1: creation of a document-object document = new Document(reader.getPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document writer = new PdfCopy(document, output); // step 3: we open the document document.open(); } // step 4: we add content PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } PRAcroForm form = reader.getAcroForm(); if (form != null) writer.copyAcroForm(reader); } if (master.size() > 0) writer.setOutlines(master); // step 5: we close the document if (document != null) document.close(); return output.toByteArray(); }
From source file:org.jpedal.examples.simpleviewer.utils.ItextFunctions.java
License:Open Source License
public void extractPagesToNewPDF(SavePDF current_selection) { final boolean exportIntoMultiplePages = current_selection.getExportType(); final int[] pgsToExport = current_selection.getExportPages(); if (pgsToExport == null) return;/*from ww w . j av a2s . c o m*/ final int noOfPages = pgsToExport.length; // get user choice final String output_dir = current_selection.getRootDir() + separator + fileName + separator + "PDFs" + separator; File testDirExists = new File(output_dir); if (!testDirExists.exists()) testDirExists.mkdirs(); final ProgressMonitor status = new ProgressMonitor(currentGUI.getFrame(), Messages.getMessage("PdfViewerMessage.GeneratingPdfs"), "", 0, noOfPages); final SwingWorker worker = new SwingWorker() { public Object construct() { if (exportIntoMultiplePages) { boolean yesToAll = false; for (int i = 0; i < noOfPages; i++) { int page = pgsToExport[i]; if (status.isCanceled()) { currentGUI.showMessageDialog(Messages.getMessage("PdfViewerError.UserStoppedExport") + i + " " + Messages.getMessage("PdfViewerError.ReportNumberOfPagesExported")); return null; } try { PdfReader reader = new PdfReader(selectedFile); File fileToSave = new File(output_dir + fileName + "_pg_" + page + ".pdf"); if (fileToSave.exists() && !yesToAll) { if (pgsToExport.length > 1) { int n = currentGUI.showOverwriteDialog(fileToSave.getAbsolutePath(), true); if (n == 0) { // clicked yes so just carry on for this // once } else if (n == 1) { // clicked yes to all, so set flag yesToAll = true; } else if (n == 2) { // clicked no, so loop round again status.setProgress(page); continue; } else { currentGUI.showMessageDialog( Messages.getMessage("PdfViewerError.UserStoppedExport") + i + " " + Messages.getMessage( "PdfViewerError.ReportNumberOfPagesExported")); status.close(); return null; } } else { int n = currentGUI.showOverwriteDialog(fileToSave.getAbsolutePath(), false); if (n == 0) { // clicked yes so just carry on } else { // clicked no, so exit return null; } } } Document document = new Document(); PdfCopy writer = new PdfCopy(document, new FileOutputStream(fileToSave)); document.open(); PdfImportedPage pip = writer.getImportedPage(reader, page); writer.addPage(pip); PRAcroForm form = reader.getAcroForm(); if (form != null) { writer.copyAcroForm(reader); } document.close(); } catch (Exception de) { de.printStackTrace(); } status.setProgress(i + 1); } } else { try { PdfReader reader = new PdfReader(selectedFile); File fileToSave = new File(output_dir + "export_" + fileName + ".pdf"); if (fileToSave.exists()) { int n = currentGUI.showOverwriteDialog(fileToSave.getAbsolutePath(), false); if (n == 0) { // clicked yes so just carry on } else { // clicked no, so exit return null; } } Document document = new Document(); PdfCopy copy = new PdfCopy(document, new FileOutputStream(fileToSave.getAbsolutePath())); document.open(); PdfImportedPage pip; for (int i = 0; i < noOfPages; i++) { int page = pgsToExport[i]; pip = copy.getImportedPage(reader, page); copy.addPage(pip); } PRAcroForm form = reader.getAcroForm(); if (form != null) { copy.copyAcroForm(reader); } List bookmarks = SimpleBookmark.getBookmark(reader); copy.setOutlines(bookmarks); document.close(); } catch (Exception de) { de.printStackTrace(); } } status.close(); currentGUI.showMessageDialog( Messages.getMessage("PdfViewerMessage.PagesSavedAsPdfTo") + " " + output_dir); return null; } }; worker.start(); }