Create document with data that can be filled in from Java

1

I am developing a program in Javafx and in this I have a client registry. In each entered record there is the possibility of printing a document related to it, in which I will have to fill in certain data automatically (name, address ...) and send it to the printer. Therefore, I need to create a template for the document and from Java to pass the necessary data to it.

The closest thing I've ever done is creating reports using Jasperreports, but I do not have much experience with it either. With my little experience I have discarded this possibility since the document is not a list of data.

I thought that maybe using pdf or word could do it. I have found something about pdf called fdf that I thought was a bit what I was looking for but I am not sure and I have not found any explanation or any example where what I want to do is done. I also felt that in Word you can create fields or variables that I can then replace from Java with the real value. But looking in google I have not found anything to help me see how it is done.

Any idea how I can do it and what tool do I have to use for it?

    
asked by Nessarose 27.09.2016 в 15:54
source

2 answers

1

Well ... I've finally managed to do it using the openoffice and passing it to pdf ... I created my template with the openoffice and from this you can add form fields and give them a name. Openoffice allows you to export the document to pdf including the forms you have. Once I have it in pdf, I use PDFBox from Java to read it and I can get the fields of the form using the name I have given them and assign them a value. As I saw, you can also use itext to do this. With PDFBox the code has been as follows:

PDDocument pdfDocument;
    try {
        pdfDocument = PDDocument.load(rutaPDF);

        PDDocumentCatalog docCatalog = pdfDocument.getDocumentCatalog();
        PDAcroForm acroForm = docCatalog.getAcroForm();

        rellenarCampo(acroForm, "nombre", "Pepito Perez");
        rellenarCampo(acroForm, "calle", "C/ Grande");

        pdfDocument.save(rutaFinalDocPDF);
        pdfDocument.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (COSVisitorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private void rellenarCampo(PDAcroForm acroForm, String nombreCampo, String valor) throws IOException{
    PDField field = acroForm.getField(nombreCampo);
    if( field != null ) {
        field.setValue(valor);
    }
    else {
        System.err.println( "No se ha encontrado el campo "+nombreCampo+"!");
    }
}

To print the document I used the code provided by @jasilva and @Mayuso

    
answered by 29.09.2016 / 15:21
source
0

Ok for what you comment at the end, if it can be very complicated to reproduce it in jasper report

This is an example in iText of how to build a PDF with refillable fields

link

Or you could make an RTF file that will be in Word, to be filled in by the user, exporting it in the following way:

Document document = new Document();

File file = new File(path+ ".rtf");
FileOutputStream fos = new FileOutputStream(file);
RtfWriter  writer=RtfWriter.getInstance(document, fos);

The main disadvantage against Jasper Reports is that it has no designer and it has to do everything in code

This would be the structure to generate a document

    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream(dest));
    document.open();
    Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLDITALIC);
    Font paragraphFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL);
    Chunk chunk = new Chunk("This is the title", chapterFont);
    Chapter chapter = new Chapter(new Paragraph(chunk), 1);
    chapter.setNumberDepth(0);
    chapter.add(new Paragraph("This is the paragraph", paragraphFont));
    document.add(chapter);
    document.close();

Now for printing it could be like this

FileInputStream fis = new FileInputStream(path);
Doc doc = new SimpleDoc(fis, null, null);
DocPrintJob printJob = getPrintService().createPrintJob();
printJob.print(pdfDoc, new HashPrintRequestAttributeSet());
fis.close();


PrintService getPrintService(){


    DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT;
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
    aset.add(MediaSizeName.ISO_A4);
    PrintService[] pservices =
                 PrintServiceLookup.lookupPrintServices(flavor, aset);
    if (pservices.length > 0) {
       return pservices[0];

    }
    else return null;// o quiza lanza excepcion
}
    
answered by 27.09.2016 в 16:38