Details
Description
There's a problem with acroform fields filled using Java.
Setting a string containing characters like óá, etc. in an Acroform field,
when sending the pdf to the printer these characters are replaced by
another ones. But if the pdf is saved to the disk, when I open the pdf the
characters are correct not replaced!
Here it is the pdf I'm using https://drive.google.com/file/d/0B1_3_sVPnBolbjc3U21LTUFTeDg/view?usp=sharing
Also I have tried to save the document in a temp file, read that
file using pdfbox and send it to the printer and the characters like ó, é, etc. don't appear well neither.
Here's the code I'm using
PDDocument pdfDocument; try { pdfDocument = PDDocument.load(PrintUtils.class.getClassLoader(). getResourceAsStream("\\application\\vistes\\clients\\contractes\\informes\\ CONTRACTE.pdf")); PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog(); PDAcroForm acroForm = docCatalog.getAcroForm(); acroForm.setNeedAppearances(false); AcroForm.omplirCamp(acroForm, "num_contracte", contracte.getId()); AcroForm.omplirCamp(acroForm, "identificacio_caldera", (contracte.getMarca() + " " + contracte.getAparell() + " " + contracte.getNumero_fabricacio()).trim()); AcroForm.omplirCamp(acroForm, "data_instalacio", dataFormatter.format(contracte.getDataInstalacio())); AcroForm.omplirCamp(acroForm, "nom_usuari", contracte.getNom_usuari()); AcroForm.omplirCamp(acroForm, "nom_usuari2", contracte.getNom_usuari()); AcroForm.omplirCamp(acroForm, "nif", contracte.getNif()); AcroForm.omplirCamp(acroForm, "carrer", contracte.getDireccio()); AcroForm.omplirCamp(acroForm, "num_casa", contracte.getNumeroCasa()); AcroForm.omplirCamp(acroForm, "telefon", contracte.getTelefon()); AcroForm.omplirCamp(acroForm, "municipi", contracte.getPoblacio()); AcroForm.omplirCamp(acroForm, "euros", decimalFormatter.format( contracte.getCost_anual())); //Create AttributeSet PrintRequestAttributeSet pset = new HashPrintRequestAttributeSet(); //Add Duplex Option to AttributeSet pset.add(Sides.DUPLEX); PrinterJob job = PrinterJob.getPrinterJob(); job.setJobName("Contracte " + (contracte.getId() != null ? contracte.getId() : "nou")); PageFormat format = job.getPageFormat(pset); format.setOrientation(PageFormat.PORTRAIT); Paper paper = new Paper(); paper.setSize(20, 14.5); format.setPaper(paper); job.defaultPage(format); job.setPageable(new PDFPageable(pdfDocument)); if (job.printDialog(pset)) { job.print(pset); } pdfDocument.close(); } catch (IOException e) { e.printStackTrace(); } catch (PrinterException e) { e.printStackTrace(); } public static void omplirCamp(PDAcroForm acroForm, String nomCamp, String valor) throws IOException{ PDField field = acroForm.getField(nomCamp); if( field != null ) { field.setValue(valor != null ? valor : ""); } else { System.err.println( "No s'ha trobat el camp "+nomCamp+"!"); } }