/** * Merges an XFDF file with a PDF form. */ @Test public void main() throws Exception { // merging the FDF file PdfReader pdfreader = new PdfReader(PdfTestBase.RESOURCES_DIR + "SimpleRegistrationForm.pdf"); PdfStamper stamp = new PdfStamper(pdfreader, PdfTestBase.getOutputStream("registered_xfdf.pdf")); XfdfReader fdfreader = new XfdfReader(PdfTestBase.RESOURCES_DIR + "register.xfdf"); AcroFields form = stamp.getAcroFields(); form.setFields(fdfreader); stamp.close(); }
@SuppressWarnings("unchecked") private List<DadesCertificat> obtenirDadesCertificatPdf( byte[] signatura) throws Exception { PdfReader reader = new PdfReader(new ByteArrayInputStream(signatura)); AcroFields af = reader.getAcroFields(); ArrayList<String> names = af.getSignatureNames(); for (String name: names) { PdfPKCS7 pk = af.verifySignature(name); Certificate pkc[] = pk.getCertificates(); List<DadesCertificat> dadesCertificats = new ArrayList<DadesCertificat>(); for (Certificate cert: pkc) { if (cert instanceof X509Certificate) { int basicConstraints = ((X509Certificate)cert).getBasicConstraints(); // Només afegeix els certificats que no son de CA if (basicConstraints == -1) dadesCertificats.add(getDadesCertificat((X509Certificate)cert)); } } return dadesCertificats; } return null; }
/** * * @param template the path to the original form * @param list the replacement list * @return * @throws IOException * @throws DocumentException */ protected byte[] renameFieldsIn(String template, Map<String, String> list) throws IOException, DocumentException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // Create the stamper PdfStamper stamper = new PdfStamper(new PdfReader(template), baos); // Get the fields AcroFields fields = stamper.getAcroFields(); // Loop over the fields for (String field : list.keySet()) { fields.setField(field, list.get(field)); } // close the stamper stamper.close(); return baos.toByteArray(); }
/** * Merges an XFDF file with a PDF form. * * @param args * no arguments needed */ public static void main(String[] args) { try { // merging the FDF file PdfReader pdfreader = new PdfReader(PdfTestRunner.getActivity().getResources().openRawResource(R.raw.simpleregistrationform)); String path = android.os.Environment.getExternalStorageDirectory() + java.io.File.separator + "droidtext" + java.io.File.separator + "registered_xfdf.pdf"; PdfStamper stamp = new PdfStamper(pdfreader, new FileOutputStream(path)); InputStream inputStream = PdfTestRunner.getActivity().getResources().openRawResource(R.raw.register); byte[] buffer = new byte[inputStream.available()]; inputStream.read(buffer); XfdfReader fdfreader = new XfdfReader(buffer); AcroFields form = stamp.getAcroFields(); form.setFields(fdfreader); stamp.close(); } catch (Exception e) { e.printStackTrace(); } }
/** * Returns all meta data for a smart tag. * @param tagName * @return */ private Map<SmartTagMeta, Integer> getSmartTagMeta(String tagName) { AcroFields acroFields = getStamper().getAcroFields(); int fieldType; Map<SmartTagMeta, Integer> smartTagMetaMap = new HashMap<SmartTagMeta, Integer>(); fieldType = acroFields.getFieldType(tagName); _Logger.debug("Field Type: " + tagName + " = " + fieldType); // so far only field type. smartTagMetaMap.put(SmartTagMeta.FIELD_TYPE, fieldType); return smartTagMetaMap; }
@SuppressWarnings({ "rawtypes", "unchecked" }) private void setSmartTags() { AcroFields acroFields = getStamper().getAcroFields(); Map acroFieldsMap = acroFields.getFields(); Iterator<String> acroFieldsIt = acroFieldsMap.keySet().iterator(); while( acroFieldsIt.hasNext() ) { addSmartTag( acroFieldsIt.next() ); } }
private static void fill(AcroFields form, String name, LocalDate startDate) throws IOException, DocumentException { form.setField(INITIATIVE_NAME, name); form.setField(INITIATIVE_DAY, startDate.toString("dd")); form.setField(INITIATIVE_MONTH, startDate.toString("MM")); form.setField(INITIATIVE_YEAR, startDate.toString("yy")); }
/** * Inspect PDF, find input / editable fields in the interactive form * * @param pdfReader * @param pdfStamper */ private void inspect(PdfReader pdfReader, PdfStamper pdfStamper) { // Get the fields from the reader (read-only!!!) AcroFields form = pdfReader.getAcroFields(); // Loop over the fields and get info about them Set<String> fields = form.getFields().keySet(); for (String field : fields) { switch (form.getFieldType(field)) { case AcroFields.FIELD_TYPE_CHECKBOX: LOG.debug("[" + field + "] : Check box"); break; case AcroFields.FIELD_TYPE_COMBO: LOG.debug("[" + field + "] : Combo box"); break; case AcroFields.FIELD_TYPE_LIST: LOG.debug("[" + field + "] : List"); break; case AcroFields.FIELD_TYPE_NONE: LOG.debug("[" + field + "] : None"); break; case AcroFields.FIELD_TYPE_PUSHBUTTON: LOG.debug("[" + field + "] : Push button"); break; case AcroFields.FIELD_TYPE_RADIOBUTTON: LOG.debug("[" + field + "] : Radio button"); break; case AcroFields.FIELD_TYPE_SIGNATURE: LOG.debug("[" + field + "] : Signature"); break; case AcroFields.FIELD_TYPE_TEXT: LOG.debug("[" + field + "] : Text"); break; default: LOG.debug("[" + field + "] : ?"); } findPossibleValues(pdfStamper, field); } }
/** * find out input field name in the interactive form * * @param pdfStamper * @param field */ private void findPossibleValues(PdfStamper pdfStamper, String field) { String[] states; String values = ""; AcroFields form = pdfStamper.getAcroFields(); states = form.getAppearanceStates(field); for (int i = 0; i < states.length; i++) { values += states[i] + ", "; } LOG.debug("Possible values for [" + field + "] : " + values); }
/** * Use iText <code>{@link PdfStamper}</code> to stamp information from <code>{@link CashReceiptDocument}</code> into field * values on a PDF Form Template. * * @param document The cash receipt document the values will be pulled from. * @param searchPath The directory path of the template to be used to generate the cover sheet. * @param returnStream The output stream the cover sheet will be written to. */ protected void stampPdfFormValues(CashReceiptDocument document, String searchPath, OutputStream returnStream) throws Exception { String templateName = CR_COVERSHEET_TEMPLATE_NM; try { // populate form with document values //KFSMI-7303 //The PDF template is retrieve through web static URL rather than file path, so the File separator is unnecessary final boolean isWebResourcePath = StringUtils.containsIgnoreCase(searchPath, "HTTP"); //skip the File.separator if reference by web resource PdfStamper stamper = new PdfStamper(new PdfReader(searchPath + (isWebResourcePath? "" : File.separator) + templateName), returnStream); AcroFields populatedCoverSheet = stamper.getAcroFields(); populatedCoverSheet.setField(DOCUMENT_NUMBER_FIELD, document.getDocumentNumber()); populatedCoverSheet.setField(INITIATOR_FIELD, document.getDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId()); populatedCoverSheet.setField(CREATED_DATE_FIELD, document.getDocumentHeader().getWorkflowDocument().getDateCreated().toString()); populatedCoverSheet.setField(AMOUNT_FIELD, document.getTotalDollarAmount().toString()); populatedCoverSheet.setField(ORG_DOC_NUMBER_FIELD, document.getDocumentHeader().getOrganizationDocumentNumber()); populatedCoverSheet.setField(CAMPUS_FIELD, document.getCampusLocationCode()); if (document.getDepositDate() != null) { // This value won't be set until the CR document is // deposited. A CR document is deposited only when it has // been associated with a Cash Management Document (CMD) // and with a Deposit within that CMD. And only when the // CMD is submitted and FINAL, will the CR documents // associated with it, be "deposited." So this value will // fill in at an arbitrarily later point in time. So your // code shouldn't expect it, but if it's there, then // display it. populatedCoverSheet.setField(DEPOSIT_DATE_FIELD, document.getDepositDate().toString()); } populatedCoverSheet.setField(DESCRIPTION_FIELD, document.getDocumentHeader().getDocumentDescription()); populatedCoverSheet.setField(EXPLANATION_FIELD, document.getDocumentHeader().getExplanation()); populatedCoverSheet.setField(CHECKS_FIELD, document.getTotalCheckAmount().toString()); populatedCoverSheet.setField(CURRENCY_FIELD, document.getTotalCashAmount().toString()); populatedCoverSheet.setField(COIN_FIELD, document.getTotalCoinAmount().toString()); populatedCoverSheet.setField(CHANGE_OUT_FIELD, document.getTotalChangeAmount().toString()); populatedCoverSheet.setField(TOTAL_RECONCILIATION_FIELD, document.getTotalDollarAmount().toString()); stamper.setFormFlattening(true); stamper.close(); } catch (Exception e) { LOG.error("Error creating coversheet for: " + document.getDocumentNumber() + ". ::" + e); throw e; } }
/** * Use iText <code>{@link PdfStamper}</code> to stamp information from <code>{@link CashReceiptDocument}</code> into field * values on a PDF Form Template. * * @param document The cash receipt document the values will be pulled from. * @param searchPath The directory path of the template to be used to generate the cover sheet. * @param returnStream The output stream the cover sheet will be written to. */ protected void stampPdfFormValues(CashReceiptDocument document, String searchPath, OutputStream returnStream) throws Exception { String templateName = CR_COVERSHEET_TEMPLATE_NM; try { // populate form with document values //KFSMI-7303 //The PDF template is retrieved through web static URL rather than file path, so the File separator is unnecessary final boolean isWebResourcePath = StringUtils.containsIgnoreCase(searchPath, "HTTP"); //skip the File.separator if reference by web resource PdfStamper stamper = new PdfStamper(new PdfReader(searchPath + (isWebResourcePath? "" : File.separator) + templateName), returnStream); AcroFields populatedCoverSheet = stamper.getAcroFields(); populatedCoverSheet.setField(DOCUMENT_NUMBER_FIELD, document.getDocumentNumber()); populatedCoverSheet.setField(INITIATOR_FIELD, document.getDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId()); populatedCoverSheet.setField(CREATED_DATE_FIELD, document.getDocumentHeader().getWorkflowDocument().getDateCreated().toString()); populatedCoverSheet.setField(AMOUNT_FIELD, document.getTotalDollarAmount().toString()); populatedCoverSheet.setField(ORG_DOC_NUMBER_FIELD, document.getDocumentHeader().getOrganizationDocumentNumber()); populatedCoverSheet.setField(CAMPUS_FIELD, document.getCampusLocationCode()); if (document.getDepositDate() != null) { // This value won't be set until the CR document is // deposited. A CR document is deposited only when it has // been associated with a Cash Management Document (CMD) // and with a Deposit within that CMD. And only when the // CMD is submitted and FINAL, will the CR documents // associated with it, be "deposited." So this value will // fill in at an arbitrarily later point in time. So your // code shouldn't expect it, but if it's there, then // display it. populatedCoverSheet.setField(DEPOSIT_DATE_FIELD, document.getDepositDate().toString()); } populatedCoverSheet.setField(DESCRIPTION_FIELD, document.getDocumentHeader().getDocumentDescription()); populatedCoverSheet.setField(EXPLANATION_FIELD, document.getDocumentHeader().getExplanation()); /* * We should print original amounts before cash manager approves the CR; after that, we should print confirmed amounts. * Note that, in CashReceiptAction.printCoverSheet, it always retrieves the CR from DB, rather than from the current form. * Since during CashManagement route node, the CR can't be saved until CM approves/disapproves the document; this means * that if CM prints during this route node, he will get the original amounts. This is consistent with our logic here. */ boolean isConfirmed = document.isConfirmed(); KualiDecimal totalCheckAmount = !isConfirmed ? document.getTotalCheckAmount() : document.getTotalConfirmedCheckAmount(); KualiDecimal totalCurrencyAmount = !isConfirmed ? document.getTotalCurrencyAmount() : document.getTotalConfirmedCurrencyAmount(); KualiDecimal totalCoinAmount = !isConfirmed ? document.getTotalCoinAmount() : document.getTotalConfirmedCoinAmount(); KualiDecimal totalCashInAmount = !isConfirmed ? document.getTotalCashInAmount() : document.getTotalConfirmedCashInAmount(); KualiDecimal totalMoneyInAmount = !isConfirmed ? document.getTotalMoneyInAmount() : document.getTotalConfirmedMoneyInAmount(); KualiDecimal totalChangeCurrencyAmount = !isConfirmed ? document.getTotalChangeCurrencyAmount() : document.getTotalConfirmedChangeCurrencyAmount(); KualiDecimal totalChangeCoinAmount = !isConfirmed ? document.getTotalChangeCoinAmount() : document.getTotalConfirmedChangeCoinAmount(); KualiDecimal totalChangeAmount = !isConfirmed ? document.getTotalChangeAmount() : document.getTotalConfirmedChangeAmount(); KualiDecimal totalNetAmount = !isConfirmed ? document.getTotalNetAmount() : document.getTotalConfirmedNetAmount(); populatedCoverSheet.setField(CHECKS_FIELD, totalCheckAmount.toString()); populatedCoverSheet.setField(CURRENCY_FIELD, totalCurrencyAmount.toString()); populatedCoverSheet.setField(COIN_FIELD, totalCoinAmount.toString()); populatedCoverSheet.setField(CASH_IN_FIELD, totalCashInAmount.toString()); populatedCoverSheet.setField(MONEY_IN_FIELD, totalMoneyInAmount.toString()); populatedCoverSheet.setField(CHANGE_CURRENCY_FIELD, totalChangeCurrencyAmount.toString()); populatedCoverSheet.setField(CHANGE_COIN_FIELD, totalChangeCoinAmount.toString()); populatedCoverSheet.setField(CHANGE_OUT_FIELD, totalChangeAmount.toString()); populatedCoverSheet.setField(RECONCILIATION_TOTAL_FIELD, totalNetAmount.toString()); stamper.setFormFlattening(true); stamper.close(); } catch (Exception e) { LOG.error("Error creating coversheet for: " + document.getDocumentNumber() + ". ::" + e); throw e; } }