public ByteArrayOutputStream getFilledPdf(Person person) throws IOException, DocumentException { InputStream istream = getClass().getResourceAsStream(BPI_AEIST_CARD_PDF_PATH); PdfReader reader = new PdfReader(istream); reader.getAcroForm().remove(PdfName.SIGFLAGS); ByteArrayOutputStream output = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(reader, output); form = stamper.getAcroFields(); setField("BI/CC", person.getDocumentIdNumber()); setField("Nome", person.getName()); setField( "topmostSubform[0].Page1[0].Datavalidade[0]", person .getExpirationDateOfDocumentIdYearMonthDay() .toString(DateTimeFormat.forPattern("dd/MM/yyyy"))); setField( "dia", String.valueOf(person.getExpirationDateOfDocumentIdYearMonthDay().getDayOfMonth())); setField( "Mês", String.valueOf(person.getExpirationDateOfDocumentIdYearMonthDay().getMonthOfYear())); setField("Ano", String.valueOf(person.getExpirationDateOfDocumentIdYearMonthDay().getYear())); LocalDate today = new LocalDate(); setField("dia_1", String.valueOf(today.getDayOfMonth())); setField("Mês_1", String.valueOf(today.getMonthOfYear())); setField("Ano_1", String.valueOf(today.getYear())); stamper.setFormFlattening(true); stamper.close(); return output; }
public ByteArrayOutputStream getFilledPdf(Person person) throws IOException, DocumentException { InputStream istream = getClass().getResourceAsStream(BPI_PERSONAL_INFORMATION_PDF_PATH); PdfReader reader = new PdfReader(istream); reader.getAcroForm().remove(PdfName.SIGFLAGS); reader.selectPages("1,2"); ByteArrayOutputStream output = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(reader, output); form = stamper.getAcroFields(); setField("Nome completo_1", person.getName()); setField("NIF", person.getSocialSecurityNumber()); setField("Nº", person.getDocumentIdNumber()); setField("Nacionalidade", person.getCountryOfBirth().getCountryNationality().toString()); setField("Naturalidade", person.getCountryOfBirth().getName()); setField("Distrito", person.getDistrictOfBirth()); setField("Concelho", person.getDistrictSubdivisionOfBirth()); setField("Freguesia", person.getParishOfBirth()); setField("Nome do Pai", person.getNameOfFather()); setField("Nome da Mãe", person.getNameOfMother()); setField("Morada de Residencia_1", person.getAddress()); setField("Localidade", person.getAreaOfAreaCode()); setField("Designação Postal", person.getAreaOfAreaCode()); setField("País", person.getCountryOfResidence().getName()); String postalCode = person.getPostalCode(); int dashIndex = postalCode.indexOf('-'); setField("Código Postal4", postalCode.substring(0, 4)); String last3Numbers = postalCode.substring(dashIndex + 1, dashIndex + 4); setField("Código Postal_5", last3Numbers); setField("Móvel", person.getDefaultMobilePhoneNumber()); setField("E-mail", getMail(person)); YearMonthDay emissionDate = person.getEmissionDateOfDocumentIdYearMonthDay(); if (emissionDate != null) { setField("Dia_1", String.valueOf(emissionDate.getDayOfMonth())); setField("Mês_1", String.valueOf(emissionDate.getMonthOfYear())); setField("Ano_1", String.valueOf(emissionDate.getYear())); } YearMonthDay expirationDate = person.getExpirationDateOfDocumentIdYearMonthDay(); setField("Dia_2", String.valueOf(expirationDate.getDayOfMonth())); setField("Mês_2", String.valueOf(expirationDate.getMonthOfYear())); setField("Ano_2", String.valueOf(expirationDate.getYear())); YearMonthDay birthdayDate = person.getDateOfBirthYearMonthDay(); setField("Dia3", String.valueOf(birthdayDate.getDayOfMonth())); setField("Mês3", String.valueOf(birthdayDate.getMonthOfYear())); setField("Ano_3", String.valueOf(birthdayDate.getYear())); stamper.setFormFlattening(true); stamper.close(); return output; }
/** * 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("SimpleRegistrationForm.pdf"); PdfStamper stamp = new PdfStamper(pdfreader, new FileOutputStream("registered_xfdf.pdf")); XfdfReader fdfreader = new XfdfReader("register.xfdf"); AcroFields form = stamp.getAcroFields(); form.setFields(fdfreader); stamp.close(); } catch (Exception e) { e.printStackTrace(); } }
public ByteArrayOutputStream getFilledPdf(Person person) throws IOException, DocumentException { InputStream istream = getClass().getResourceAsStream(BPI_PRODUCTS_SERVICES_PDF_PATH); PdfReader reader = new PdfReader(istream); reader.getAcroForm().remove(PdfName.SIGFLAGS); reader.selectPages("1"); ByteArrayOutputStream output = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(reader, output); form = stamper.getAcroFields(); setField("Nome_1", person.getName()); stamper.setFormFlattening(true); stamper.close(); return output; }
public ByteArrayOutputStream getFilledPdf(Person person) throws IOException, DocumentException { InputStream istream = getClass().getResourceAsStream(SANTANDER_APPLICATION_PDF_PATH); PdfReader reader = new PdfReader(istream); reader.getAcroForm().remove(PdfName.SIGFLAGS); reader.selectPages("1,2"); ByteArrayOutputStream output = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(reader, output); form = stamper.getAcroFields(); setField("topmostSubform[0].Page1[0].Nomecompleto[0]", person.getName()); String documentIdNumber = person.getDocumentIdNumber(); if (person.getIdDocumentType().equals(IDDocumentType.CITIZEN_CARD) || person.getIdDocumentType().equals(IDDocumentType.IDENTITY_CARD)) { setField("topmostSubform[0].Page1[0].NumBICartaoCidadaooutro[0]", documentIdNumber); setField( "topmostSubform[0].Page1[0].Checkdigit[0]", person.getIdentificationDocumentSeriesNumberValue()); } else { setField("topmostSubform[0].Page1[0].Outrotipodocidentificacao[0]", documentIdNumber); } YearMonthDay emissionDate = person.getEmissionDateOfDocumentIdYearMonthDay(); if (emissionDate != null) { setField( "topmostSubform[0].Page1[0].Dataemissao[0]", emissionDate.toString(DateTimeFormat.forPattern("dd/MM/yyyy"))); } setField( "topmostSubform[0].Page1[0].Datavalidade[0]", person .getExpirationDateOfDocumentIdYearMonthDay() .toString(DateTimeFormat.forPattern("dd/MM/yyyy"))); setField("topmostSubform[0].Page1[0].NIF[0]", person.getSocialSecurityNumber()); setField( "topmostSubform[0].Page1[0].Nacionalidade[0]", person.getCountryOfBirth().getCountryNationality().toString()); setField( "topmostSubform[0].Page1[0].Datanascimento[0]", person.getDateOfBirthYearMonthDay().toString(DateTimeFormat.forPattern("dd/MM/yyyy"))); YearMonthDay dateOfBirthYearMonthDay = person.getDateOfBirthYearMonthDay(); Period periodBetween = new Period(dateOfBirthYearMonthDay, new YearMonthDay()); setField( "topmostSubform[0].Page1[0].Idadeactual[0]", String.valueOf(periodBetween.get(DurationFieldType.years()))); if (person.isFemale()) { setField("topmostSubform[0].Page1[0].Sexo[0]", "F"); // female } else if (person.isMale()) { setField("topmostSubform[0].Page1[0].Sexo[0]", "M"); // male } switch (person.getMaritalStatus()) { case CIVIL_UNION: setField("topmostSubform[0].Page1[0].Uniaofacto[0]", "1"); break; case DIVORCED: setField("topmostSubform[0].Page1[0].Divorciado[0]", "1"); break; case MARRIED: setField("topmostSubform[0].Page1[0].Casado[0]", "1"); break; case SEPARATED: setField("topmostSubform[0].Page1[0].Separado[0]", "1"); break; case SINGLE: setField("topmostSubform[0].Page1[0].Solteiro[0]", "1"); break; case WIDOWER: setField("topmostSubform[0].Page1[0].Viuvo[0]", "1"); break; } setField("topmostSubform[0].Page1[0].Telemovel[0]", person.getDefaultMobilePhoneNumber()); setField("topmostSubform[0].Page1[0].E-mail[0]", getMail(person)); setField("topmostSubform[0].Page1[0].Moradaprincipal[0]", person.getAddress()); setField("topmostSubform[0].Page1[0].localidade[0]", person.getAreaOfAreaCode()); String postalCode = person.getPostalCode(); int dashIndex = postalCode.indexOf('-'); setField("topmostSubform[0].Page1[0].CodPostal[0]", postalCode.substring(0, 4)); String last3Numbers = person.getPostalCode().substring(dashIndex + 1, dashIndex + 4); setField("topmostSubform[0].Page1[0].ExtensaoCodPostal[0]", last3Numbers); setField( "topmostSubform[0].Page1[0].Nacionalidade[0]", person.getCountryOfBirth().getCountryNationality().toString()); setField( "topmostSubform[0].Page1[0].Nacionalidade[0]", person.getCountryOfBirth().getCountryNationality().toString()); stamper.setFormFlattening(true); stamper.close(); return output; }
public ByteArrayOutputStream getFilledPdf(Person person, StudentCandidacy candidacy) throws IOException, DocumentException { InputStream istream = getClass().getResourceAsStream(SANTANDER_APPLICATION_CARD_PDF_PATH); PdfReader reader = new PdfReader(istream); reader.getAcroForm().remove(PdfName.SIGFLAGS); ByteArrayOutputStream output = new ByteArrayOutputStream(); PdfStamper stamper = new PdfStamper(reader, output); form = stamper.getAcroFields(); setField("StudentIdentification", person.getIstUsername()); setField("Phone", person.getDefaultMobilePhoneNumber()); setField("Email", getMail(person)); setField( "CurrentDate", new DateTime().toString(DateTimeFormat.forPattern("dd/MM/yyyy HH:mm"))); SantanderPhotoEntry photoEntryForPerson = SantanderPhotoEntry.getOrCreatePhotoEntryForPerson( candidacy.getRegistration().getPerson()); if (photoEntryForPerson != null) { setField("Sequence", photoEntryForPerson.getPhotoIdentifier()); try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BarcodeImageHandler.writeJPEG( BarcodeFactory.createCode39(photoEntryForPerson.getPhotoIdentifier(), false), baos); Jpeg sequenceBarcodeImg = new Jpeg(baos.toByteArray()); float[] sequenceFieldPositions = form.getFieldPositions( "SequenceBarcode"); // 1-lowerleftX, 2-lly, 3-upperRightX, 4-ury sequenceBarcodeImg.setAbsolutePosition( sequenceFieldPositions[1], sequenceFieldPositions[2]); sequenceBarcodeImg.scalePercent(45); stamper.getOverContent(1).addImage(sequenceBarcodeImg); } catch (OutputException e) { logger.error(e.getMessage(), e); } catch (BarcodeException be) { logger.error(be.getMessage(), be); } Jpeg photo = new Jpeg(photoEntryForPerson.getPhotoAsByteArray()); float[] photoFieldPositions = form.getFieldPositions("Photo"); // 1-lowerleftX, 2-lly, 3-upperRightX, 4-ury photo.setAbsolutePosition(photoFieldPositions[1], photoFieldPositions[2]); photo.scalePercent(95); stamper.getOverContent(1).addImage(photo); } try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); BarcodeImageHandler.writeJPEG(BarcodeFactory.createCode128(person.getIstUsername()), baos); Jpeg studentIdBarcodeImg = new Jpeg(baos.toByteArray()); float[] studentIdFieldPositions = form.getFieldPositions( "StudentIdentificationBarcode"); // 1-lowerleftX, 2-lly, 3-upperRightX, 4-ury studentIdBarcodeImg.setAbsolutePosition( studentIdFieldPositions[1], studentIdFieldPositions[2]); studentIdBarcodeImg.scalePercent(45); stamper.getOverContent(1).addImage(studentIdBarcodeImg); } catch (OutputException e) { logger.error(e.getMessage(), e); } catch (BarcodeException be) { logger.error(be.getMessage(), be); } stamper.setFormFlattening(true); stamper.close(); return output; }
/** * Process the specified HTTP request, and create the corresponding HTTP response (or forward to * another web component that will create it). Return an <code>ActionForward</code> instance * describing where and how control should be forwarded, or <code>null</code> if the response has * already been completed. * * @param mapping The ActionMapping used to select this instance * @param form The optional ActionForm bean for this request (if any) * @param request The HTTP request we are processing * @param response The HTTP response we are creating * @exception Exception if business logic throws an exception */ public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { // Extract attributes we will need MessageResources messages = getResources(request); // save errors ActionMessages errors = new ActionMessages(); // START check for login (security) if (!SecurityService.getInstance().checkForLogin(request.getSession(false))) { return (mapping.findForward("welcome")); } // END check for login (security) // START get id of current project from either request, attribute, or cookie // id of project from request String projectId = null; projectId = request.getParameter("projectViewId"); // check attribute in request if (projectId == null) { projectId = (String) request.getAttribute("projectViewId"); } // id of project from cookie if (projectId == null) { projectId = StandardCode.getInstance().getCookie("projectViewId", request.getCookies()); } // default project to last if not in request or cookie if (projectId == null) { java.util.List results = ProjectService.getInstance().getProjectList(); ListIterator iterScroll = null; for (iterScroll = results.listIterator(); iterScroll.hasNext(); iterScroll.next()) {} iterScroll.previous(); Project p = (Project) iterScroll.next(); projectId = String.valueOf(p.getProjectId()); } Integer id = Integer.valueOf(projectId); // END get id of current project from either request, attribute, or cookie // get project Project p = ProjectService.getInstance().getSingleProject(id); // get user (project manager) User u = UserService.getInstance() .getSingleUserRealName( StandardCode.getInstance().getFirstName(p.getPm()), StandardCode.getInstance().getLastName(p.getPm())); // START process pdf try { PdfReader reader = new PdfReader("C://templates/CL01_001.pdf"); // the template // save the pdf in memory ByteArrayOutputStream pdfStream = new ByteArrayOutputStream(); // the filled-in pdf PdfStamper stamp = new PdfStamper(reader, pdfStream); // stamp.setEncryption(true, "pass", "pass", PdfWriter.AllowCopy | PdfWriter.AllowPrinting); AcroFields form1 = stamp.getAcroFields(); Date cDate = new Date(); Integer month = cDate.getMonth(); Integer day = cDate.getDate(); Integer year = cDate.getYear() + 1900; String[] monthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; // set the field values in the pdf form // form1.setField("", projectId) form1.setField("currentdate", monthName[month] + " " + day + ", " + year); form1.setField( "firstname", StandardCode.getInstance().noNull(p.getContact().getFirst_name())); form1.setField("pm", p.getPm()); form1.setField("emailpm", u.getWorkEmail1()); if (u.getWorkPhoneEx() != null && u.getWorkPhoneEx().length() > 0) { // ext present form1.setField( "phonepm", StandardCode.getInstance().noNull(u.getWorkPhone()) + " ext " + StandardCode.getInstance().noNull(u.getWorkPhoneEx())); } else { // no ext present form1.setField("phonepm", StandardCode.getInstance().noNull(u.getWorkPhone())); } form1.setField("faxpm", StandardCode.getInstance().noNull(u.getLocation().getFax_number())); form1.setField("postalpm", StandardCode.getInstance().printLocation(u.getLocation())); // START add images // if(u.getPicture() != null && u.getPicture().length() > 0) { // PdfContentByte over; // Image img = Image.getInstance("C:/Program Files (x86)/Apache Software // Foundation/Tomcat 7.0/webapps/logo/images/" + u.getPicture()); // img.setAbsolutePosition(200, 200); // over = stamp.getOverContent(1); // over.addImage(img, 54, 0,0, 65, 47, 493); // } // END add images form1.setField("productname", StandardCode.getInstance().noNull(p.getProduct())); form1.setField("project", p.getNumber() + p.getCompany().getCompany_code()); form1.setField("description", StandardCode.getInstance().noNull(p.getProductDescription())); form1.setField("additional", p.getProjectRequirements()); // get sources and targets StringBuffer sources = new StringBuffer(""); StringBuffer targets = new StringBuffer(""); if (p.getSourceDocs() != null) { for (Iterator iterSource = p.getSourceDocs().iterator(); iterSource.hasNext(); ) { SourceDoc sd = (SourceDoc) iterSource.next(); sources.append(sd.getLanguage() + " "); if (sd.getTargetDocs() != null) { for (Iterator iterTarget = sd.getTargetDocs().iterator(); iterTarget.hasNext(); ) { TargetDoc td = (TargetDoc) iterTarget.next(); if (!td.getLanguage().equals("All")) targets.append(td.getLanguage() + " "); } } } } form1.setField("source", sources.toString()); form1.setField("target", targets.toString()); form1.setField( "start", (p.getStartDate() != null) ? DateFormat.getDateInstance(DateFormat.SHORT).format(p.getStartDate()) : ""); form1.setField( "due", (p.getDueDate() != null) ? DateFormat.getDateInstance(DateFormat.SHORT).format(p.getDueDate()) : ""); if (p.getCompany().getCcurrency().equalsIgnoreCase("USD")) { form1.setField( "cost", (p.getProjectAmount() != null) ? "$ " + StandardCode.getInstance().formatDouble(p.getProjectAmount()) : ""); } else { form1.setField( "cost", (p.getProjectAmount() != null) ? "€ " + StandardCode.getInstance() .formatDouble(p.getProjectAmount() / p.getEuroToUsdExchangeRate()) : ""); } // stamp.setFormFlattening(true); stamp.close(); // write to client (web browser) response.setHeader( "Content-disposition", "attachment; filename=" + p.getNumber() + p.getCompany().getCompany_code() + "-Order-Confirmation" + ".pdf"); OutputStream os = response.getOutputStream(); pdfStream.writeTo(os); os.flush(); } catch (Exception e) { System.err.println("PDF Exception:" + e.getMessage()); throw new RuntimeException(e); } // END process pdf // Forward control to the specified success URI return (mapping.findForward("Success")); }
/** 入力票の項目を作成する */ public File printKekka() { /* Modified 2008/07/25 若月 */ /* --------------------------------------------------- */ // templatePath = PropertyUtil.getProperty("filePath"); // outputPath = PropertyUtil.getProperty("filePath"); // templatePath = templatePath + infilename; // outputPath = ".\\Data\\PDF\\" + outfilename; /* --------------------------------------------------- */ PropertyUtil util = new PropertyUtil(JPath.PATH_FILE_PROPERTIES); templatePath = util.getProperty("filePath"); outputPath = templatePath; templatePath = templatePath + infilename; /* --------------------------------------------------- */ /* Modified 2008/09/11 薮 */ /* --------------------------------------------------- */ // outputPath = ".\\Data\\PDF\\" + outfilename; outputPath = "." + File.separator + "Data" + File.separator + "PDF" + File.separator + "" + outfilename; /* --------------------------------------------------- */ /* --------------------------------------------------- */ /* Modified 2008/09/11 薮 */ /* --------------------------------------------------- */ // File outfile = new File(".\\Data\\PDF"); File outfile = new File("." + File.separator + "Data" + File.separator + "PDF"); /* --------------------------------------------------- */ outfile.mkdirs(); // File n = new File(".\\Data\\PDF\\outNyuryokuTemplate.pdf"); File file = null; try { /* * 個人情報 PrintDataから個人情報を抽出 */ Kojin tmpKojin = (Kojin) printData.get("Kojin"); Hashtable<String, String> kojinData = tmpKojin.getPrintKojinData(); /* --------------------------------------------------- */ /* Added 2008/09/11 薮 */ /* --------------------------------------------------- */ String strKensaYmd = ""; if (kojinData.get("KENSA_NENGAPI") == null) { // edit stop s.inoue 2009/10/15 // strKensaYmd = String.valueOf(FiscalYearUtil.getSystemDate()); // edit s.inoue 2009/10/16 // strKensaYmd = null; strKensaYmd = getKenshinDate(); } else { strKensaYmd = replaseNenGaPii(kojinData.get("KENSA_NENGAPI")); // strKensaYmd = kojinData.get("KENSA_NENGAPI").replaceAll("[^0-9]+",""); } // ファイル作成 PdfReader reader = new PdfReader(templatePath); PdfStamper stamp = new PdfStamper( reader, new FileOutputStream( outputPath + kojinData.get("UKETUKE_ID") /* --------------------------------------------------- */ /* Modified 2008/09/11 薮 */ /* --------------------------------------------------- */ // + kojinData.get("KENSA_NENGAPI"))); + strKensaYmd)); /* --------------------------------------------------- */ AcroFields form = stamp.getAcroFields(); /* --------------------------------------------------- */ /* Modified 2008/09/11 薮 */ /* --------------------------------------------------- */ // file = new File(".\\Data\\PDF\\" + outfilename file = new File( "." + File.separator + "Data" + File.separator + "PDF" + File.separator + "" + outfilename + kojinData.get("UKETUKE_ID") // + kojinData.get("KENSA_NENGAPI")); + strKensaYmd); /* --------------------------------------------------- */ // 健診年月日 form.setField("T1", kojinData.get("KENSA_NENGAPI")); form.setField("T19", kojinData.get("KENSA_NENGAPI")); // 受診券番号 form.setField("T2", kojinData.get("JYUSHIN_SEIRI_NO")); form.setField("T20", kojinData.get("JYUSHIN_SEIRI_NO")); // 保険者番号 form.setField("T3", kojinData.get("HKNJANUM")); form.setField("T21", kojinData.get("HKNJANUM")); // 被保険者証等記号 form.setField("T4", kojinData.get("HIHOKENJYASYO_KIGOU")); form.setField("T22", kojinData.get("HIHOKENJYASYO_KIGOU")); // 被保険者証等番号 form.setField("T5", kojinData.get("HIHOKENJYASYO_NO")); form.setField("T23", kojinData.get("HIHOKENJYASYO_NO")); // フリガナ form.setField("T6", kojinData.get("KANANAME")); form.setField("T24", kojinData.get("KANANAME")); // 氏名 form.setField("T7", kojinData.get("NAME")); form.setField("T26", kojinData.get("NAME")); // 生年月日 form.setField("T8", kojinData.get("BIRTHDAY")); form.setField("T25", kojinData.get("BIRTHDAY")); // 性別 form.setField("T9", kojinData.get("SEX")); form.setField("T27", kojinData.get("SEX")); // 年齢 form.setField("T10", kojinData.get("AGE")); form.setField("T28", kojinData.get("AGE")); // del s.inoue 2009/11/16 // if (kojinData.get("UKETUKE_ID") != null) { // Hashtable<String, String> ResultItem = null; // // 追加の項目 // // 項目名 // int setCnt = 11; // List tuika = this.tuika(kojinData); // int tuikaSize = tuika.size(); // // if (tuikaSize > 0) { // for (int i = 0; i < tuikaSize; i++) { // ResultItem = (Hashtable<String, String>) tuika.get(i); // form.setField("S" + Integer.toString(setCnt), // ResultItem.get("KOUMOKU_NAME")); // setCnt++; // } // } // // // 項目名 // int setTani = 19; // if (tuikaSize > 0) { // for (int i = 0; i < tuikaSize; i++) { // ResultItem = (Hashtable<String, String>) tuika.get(i); // form.setField("S" + Integer.toString(setTani), // ResultItem.get("TANI")); // setTani++; // } // } // // } stamp.setFormFlattening(true); stamp.close(); } catch (Exception e) { e.printStackTrace(); } return file; }