1

I have some data which I want to save in a pdf file. I don't want to create a pdf from source code. A client gives me many different templates and I want to fill these with data which he needs. For example I have an information about "name", "surname", "address", "phone number" etc. A client gives me a template where there are columns only for "name", "surname", he doesn't care about "address" and "phone number".

I've tried something like this(from: Creating complex pdf using java)

    PdfReader reader = new PdfReader("C:/folder/template/template.pdf");
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("C:/folder/template/filledTemplate.pdf"));
    AcroFields form = stamper.getAcroFields();

    boolean column = form.setField("column", "insert data");
    boolean key = form.setField("key", "insert key");
    boolean value = form.setField("value", "insert value");

    stamper.setFormFlattening(true);
    stamper.close();
    reader.close();

The problem is it doesn't do anything. A form variable is null. I don't have any requirements about tools. It doesn't need to be itext, It can be PDFBox(which I haven't used yet). Example pdf file: pdf file

Edit: I've just asked my colleague about this. Sorry for my mistake, we have some data in our application and I'm doing a converter to various types of files like excel or pdf. We have many columns so there's no problem with excel files but I can't put all data in pdf. So one of the solution is a client would choose a specific form of pdf with columns which he needs and he gets a pdf filled with data. So wee need different templates.

Community
  • 1
  • 1
steeve
  • 27
  • 6
  • You should share the PDF instead of a screen shot of the PDF. You are using code that requires the PDF to be a form. Looking at the screen shot, I don't see a form. I can't test if the PDF is interactive. For instance: if you open the PDF in Adobe Reader and you click on a cell in the table, can you add content to that cell? If not, your so-called template isn't a form. – Bruno Lowagie Aug 10 '16 at 09:15
  • @BrunoLowagie I can't add content to any cell(in AR). I thought I could just create a table with headers and it could be filled by a java program. – steeve Aug 10 '16 at 09:21
  • When you say you *could just create a table with headers and it could be filled by a Java program*, it isn't clear what you mean. That sounds as if you want to create the PDF from scratch, which is in contradication with what you write in your question. – Bruno Lowagie Aug 10 '16 at 09:28
  • @BrunoLowagie I've created a pdf (for tests) with a table because I don't have any templates from clients yet. – steeve Aug 10 '16 at 09:36
  • @BrunoLowagie so is it possible to get a pdf template with a table and fill it by my program or I have to create pdf from source code? – steeve Aug 10 '16 at 09:39
  • PDF is not quite easy as template. Something like **JasperReports** might be more suited. But first an actual scenario is missing for me dummy. The clients give PDF templates? Or forms? And data are filled in, per client somewhat differently? – Joop Eggen Aug 10 '16 at 09:41
  • 1
    Yes and no. There is no clear answer to your question because you aren't asking us a clear question. You don't have sufficient knowledge about AcroForm technology versus the XML Forms Architecture (XFA). As @JoopEggen explains: we don't know anything about the scenario. This is not an invitation to post a scenario as a question. Such a question would immediately be closed as "Too broad." It's an invitation for you to start getting an education about form technology. – Bruno Lowagie Aug 10 '16 at 09:43
  • 1
    @steeve Please be aware that a table in a PDF usually is merely a collection of lines or rectangles and some text fragments. It is **not** a table object as such anymore which one can *select* and *fill*. If you want to **fill** some such table cells, you should put an AcroForm form field there to fill in and probably flatten after the fact. Other agreed-upon annotations may give rise to a solution, too. – mkl Aug 10 '16 at 10:01
  • Using an AcroForm for this type of job isn't a good idea. XFA could be a solution, but it's too complex for most users, it requires buying Adobe LiveCycle Designer, and on top of that all, XFA will be deprecated in PDF 2.0 (ISO-32000-2). I would suggest that you ask your customers to create a simple HTML and CSS file, that you populate the HTML with data, and use iText's XML worker to create the PDF. – Bruno Lowagie Aug 10 '16 at 11:27

0 Answers0