/** * * @param pdfOut * @param pdfIn * @param pagesToDelete * @throws Exception */ public static void extractPages(OutputStream pdfOut,InputStream pdfIn, int[] pages) throws Exception{ if (pages.length <= 0) { throw new Exception("Debe eliminar al menos una p�gina"); } List pagesToKeep = new ArrayList(); for (int i=0;i<pages.length;i++){ pagesToKeep.add(new Integer(pages[i])); } PdfCopyFields writer = new PdfCopyFields(pdfOut); int permission=0; PdfReader reader = new PdfReader(pdfIn); permission = reader.getPermissions(); if (permission != 0){ writer.setEncryption(null, null,permission, PdfWriter.STRENGTH40BITS); } writer.addDocument(reader,pagesToKeep); writer.close(); }
/** * Concatenates 2 PDF files with forms. The resulting PDF has 1 merged * AcroForm. * * @param args * no arguments needed */ public static void main(String[] args) { try { //Can't use filename directly // PdfReader reader1 = new PdfReader("SimpleRegistrationForm.pdf"); // PdfReader reader2 = new PdfReader("TextFields.pdf"); PdfReader reader1 = new PdfReader(PdfTestRunner.getActivity().getResources().openRawResource(R.raw.simpleregistrationform)); PdfReader reader2 = new PdfReader(PdfTestRunner.getActivity().getResources().openRawResource(R.raw.textfields)); PdfCopyFields copy = new PdfCopyFields(new FileOutputStream(android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "droidtext" + java.io.File.separator + "concatenatedforms.pdf")); copy.addDocument(reader1); copy.addDocument(reader2); copy.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * Concatenates 2 PDF files with forms. The resulting PDF has 1 merged AcroForm. */ @Test public void main() throws Exception{ PdfReader reader1 = new PdfReader(PdfTestBase.RESOURCES_DIR + "SimpleRegistrationForm.pdf"); PdfReader reader2 = new PdfReader(PdfTestBase.RESOURCES_DIR + "TextFields.pdf"); PdfCopyFields copy = new PdfCopyFields(PdfTestBase.getOutputStream("concatenatedforms.pdf")); copy.addDocument(reader1); copy.addDocument(reader2); copy.close(); }
/** * Concatena varios pdfs * @param pdfOut Outputstream al pdf de salida * @param pdfIn InputStreams de los pdfs a concatenar */ public static void concatenarPdf(OutputStream pdfOut,InputStream [] pdfIn) throws Exception{ if (pdfIn.length < 2) { throw new Exception("Debe concatenar al menos 2 PDFs"); } // Realizamos copia PDFs int f = 0; PdfCopyFields writer = new PdfCopyFields(pdfOut); int permission=0; while (f < pdfIn.length) { // we create a reader for a certain document PdfReader reader = new PdfReader(pdfIn[f]); // Establecemos permisos del primer documento if (f==0){ permission = reader.getPermissions(); if (permission != 0){ writer.setEncryption(null, null,permission, PdfWriter.STRENGTH40BITS); } } writer.addDocument(reader); f++; } writer.close(); }
/** * Generates the pdf file for printing the invoices. * * @param list * @param outputStream * @throws DocumentException * @throws IOException */ protected void generateCombinedPdfForInvoices(Collection<ContractsGrantsInvoiceDocument> list, OutputStream outputStream) throws DocumentException, IOException { PdfCopyFields copy = new PdfCopyFields(outputStream); boolean pageAdded = false; for (ContractsGrantsInvoiceDocument invoice : list) { // add a document List<InvoiceAddressDetail> invoiceAddressDetails = invoice.getInvoiceAddressDetails(); for (InvoiceAddressDetail invoiceAddressDetail : invoiceAddressDetails) { if (ArConstants.InvoiceTransmissionMethod.MAIL.equals(invoiceAddressDetail.getInvoiceTransmissionMethodCode())) { Note note = noteService.getNoteByNoteId(invoiceAddressDetail.getNoteId()); Integer numberOfCopiesToPrint = invoiceAddressDetail.getCustomerAddress().getCustomerCopiesToPrint(); if (ObjectUtils.isNull(numberOfCopiesToPrint)) { numberOfCopiesToPrint = 1; } if (!ObjectUtils.isNull(note)) { for (int i = 0; i < numberOfCopiesToPrint; i++) { if (!pageAdded) { copy.open(); } pageAdded = true; copy.addDocument(new PdfReader(note.getAttachment().getAttachmentContents())); } } invoiceAddressDetail.setInitialTransmissionDate(new Date(new java.util.Date().getTime())); } } documentService.updateDocument(invoice); } if (pageAdded) { copy.close(); } }
/** * Generates the pdf file for printing the invoices. * * @param list * @param outputStream * @throws DocumentException * @throws IOException */ protected void generateCombinedPdfForInvoices(Collection<ContractsGrantsInvoiceDocument> list, byte[] report, OutputStream outputStream) throws DocumentException, IOException { PdfCopyFields copy = new PdfCopyFields(outputStream); copy.open(); copy.addDocument(new PdfReader(report)); for (ContractsGrantsInvoiceDocument invoice : list) { for (InvoiceAddressDetail invoiceAddressDetail : invoice.getInvoiceAddressDetails()) { Note note = noteService.getNoteByNoteId(invoiceAddressDetail.getNoteId()); if (ObjectUtils.isNotNull(note) && note.getAttachment().getAttachmentFileSize() > 0) { copy.addDocument(new PdfReader(note.getAttachment().getAttachmentContents())); } } } copy.close(); }
/** * Use iText <code>{@link PdfStamper}</code> to stamp information into field values on a PDF Form Template. * * @param agency The award the values will be pulled from. * @param reportingPeriod * @param year * @param returnStream The output stream the federal form will be written to. */ protected void stampPdfFormValues425A(ContractsAndGrantsBillingAgency agency, String reportingPeriod, String year, OutputStream returnStream, Map<String, String> replacementList) { String federalReportTemplatePath = configService.getPropertyValueAsString(KFSConstants.EXTERNALIZABLE_HELP_URL_KEY); try { final String federal425ATemplateUrl = federalReportTemplatePath + ArConstants.FF_425A_TEMPLATE_NM + KFSConstants.ReportGeneration.PDF_FILE_EXTENSION; final String federal425TemplateUrl = federalReportTemplatePath + ArConstants.FF_425_TEMPLATE_NM + KFSConstants.ReportGeneration.PDF_FILE_EXTENSION; Map<String, Object> fieldValues = new HashMap<>(); fieldValues.put(KFSPropertyConstants.AGENCY_NUMBER, agency.getAgencyNumber()); fieldValues.put(KFSPropertyConstants.ACTIVE, Boolean.TRUE); List<ContractsAndGrantsBillingAward> awards = kualiModuleService.getResponsibleModuleService(ContractsAndGrantsBillingAward.class).getExternalizableBusinessObjectsList(ContractsAndGrantsBillingAward.class, fieldValues); Integer pageNumber = 1, totalPages; totalPages = (awards.size() / ArConstants.Federal425APdf.NUMBER_OF_SUMMARIES_PER_PAGE) + 1; PdfCopyFields copy = new PdfCopyFields(returnStream); // generate replacement list for FF425 populateListByAgency(awards, reportingPeriod, year, agency); contractsGrantsBillingUtilityService.putValueOrEmptyString(replacementList, ArPropertyConstants.FederalFormReportFields.TOTAL_PAGES, org.apache.commons.lang.ObjectUtils.toString(totalPages + 1)); KualiDecimal sumCashControl = KualiDecimal.ZERO; KualiDecimal sumCumExp = KualiDecimal.ZERO; while (pageNumber <= totalPages) { List<ContractsAndGrantsBillingAward> awardsList = new ArrayList<ContractsAndGrantsBillingAward>(); for (int i = ((pageNumber - 1) * ArConstants.Federal425APdf.NUMBER_OF_SUMMARIES_PER_PAGE); i < (pageNumber * ArConstants.Federal425APdf.NUMBER_OF_SUMMARIES_PER_PAGE); i++) { if (i < awards.size()) { awardsList.add(awards.get(i)); } } // generate replacement list for FF425 List<KualiDecimal> list = populateListByAgency(awardsList, reportingPeriod, year, agency); if (CollectionUtils.isNotEmpty(list)){ sumCashControl = sumCashControl.add(list.get(0)); if (list.size() > 1){ sumCumExp = sumCumExp.add(list.get(1)); } } // populate form with document values contractsGrantsBillingUtilityService.putValueOrEmptyString(replacementList, ArPropertyConstants.FederalFormReportFields.PAGE_NUMBER, org.apache.commons.lang.ObjectUtils.toString(pageNumber + 1)); if (pageNumber == totalPages){ contractsGrantsBillingUtilityService.putValueOrEmptyString(replacementList, ArPropertyConstants.FederalFormReportFields.TOTAL, contractsGrantsBillingUtilityService.formatForCurrency(sumCashControl)); contractsGrantsBillingUtilityService.putValueOrEmptyString(replacementList, ArPropertyConstants.FederalFormReportFields.CASH_RECEIPTS, contractsGrantsBillingUtilityService.formatForCurrency(sumCashControl)); contractsGrantsBillingUtilityService.putValueOrEmptyString(replacementList, ArPropertyConstants.FederalFormReportFields.CASH_DISBURSEMENTS, contractsGrantsBillingUtilityService.formatForCurrency(sumCumExp)); contractsGrantsBillingUtilityService.putValueOrEmptyString(replacementList, ArPropertyConstants.FederalFormReportFields.CASH_ON_HAND, contractsGrantsBillingUtilityService.formatForCurrency(sumCashControl.subtract(sumCumExp))); } // add a document copy.addDocument(new PdfReader(renameFieldsIn(federal425ATemplateUrl, replacementList))); pageNumber++; } contractsGrantsBillingUtilityService.putValueOrEmptyString(replacementList, ArPropertyConstants.FederalFormReportFields.PAGE_NUMBER, "1"); // add the FF425 form. copy.addDocument(new PdfReader(renameFieldsIn(federal425TemplateUrl, replacementList))); // Close the PdfCopyFields object copy.close(); } catch (DocumentException | IOException ex) { throw new RuntimeException("Tried to stamp the 425A, but couldn't do it. Just...just couldn't do it.", ex); } }