Creating pdfs from predefined templates in java - java

My Spring application needs to create different types of PDF documents like invoices and certificates each having dynamic data .I would like to have some predefined templates(html/text file) from which I can generate the full PDF content. The predefined template holds the full content of the PDF document including the font size, alignments of each section etc and also have key values that need to be replaced with actual values form database. I know it is easy to create PDFs from html. Is anyone having any idea on how to accept an html template as input in a java program and then hook in the keys defined in it with actual values and finally creating the PDFs?

Related

Make existing PDF's in to templets - iText

I am trying to make some existing PDF's into templets.
Because these documents hold real data I am replaceing this data such as names and addrsss and making them into dummy place holders.
Examples
[[Name]]
[[Address1]]
When I alter the text via the iText version 5 library replace via a program I can use the template.
To speed things up I tried to use Adobe DC.
When using this method the template stops working.
Any ideas?
From what I understand of your question;
you have (or want to have) a template document
fill in the template with data from a program
turn this back into a pdf
You can easily achieve some of your goals with iText.
I suggest you look into http://developers.itextpdf.com/examples/form-examples/clone-filling-out-forms

Build PDF templates and populating then

I have the use case:
Build a PDF template and populate with variables with Java API iText.
But, I only saw examples with Acro Forms and my PDF it's not a form, it is a contract, like:
Rent to Own Contract
Whereas, ___________________ (hereafter Renter) desires to possess and have the use of certain property owned by
____________________ (hereafter Owner) and described in Attachment A, (...)
I need to build this template and populate with an Object, where '____________' is the place of variables.
The document template it can be constructed with HTML or XML too!!
Any Ideas or help?

Reading word template user entered values in java

I have a word template for a application Form (which contains text, dropdowns, checkboxes, date fields..). In my web application whenever user starts a new process they need to fill some high level data, here I need to fill some of the user entered data into the word template and let user download that document. Later user takes this document offline and fills empty lower level fields in that document, whenever they are done filling they will upload the document back into the application. Now I need to read the values of each field that user entered and store those values in Database.
Can someone give me the direction how to achieve this using java.
Or is there a better way to achieve the same with other than the word templates.
-----------------Update---------
Planning to use docx4j library. Following are the higher level steps that I may follow for my process
Creating a word template using locked content controls
A unique tag value (w:tag) will be assigned for each content control
Will populate any dropdown values and any other controls values using
docx4j library.
After user fills the form, will extract data from template using
docx4j, considering the unique tag value that I assigned in first
step.
The Apache OpenOffice API (based on the UNO component technology) allows you to read and manipulate OpenOffice documents. To .doc (not based on xml) formats you can use de API to convert the file to ODT, what turn you enabled to extract and process the data within the document.

how can I add textfield to the existing pdf template

I want create a pdf template from a another template,the result pdf is still the template then i can fill it with data。
I try to use PdfStamper but the result pdf is not template,any one can help me,thanks.
Let's distinguish two situations, depending on the nature of your PDF template:
You are talking about an XFA template:
In this case, the PDF is merely a container for an XML stream that defines your form. The only way to change it, is by editing the XML. This is best done manually using Adobe LiveCycle Designer, but if you really want to do it programmatically, you can extract the XML from the PDF using iText, manipulate the XML using any type of XML editing software, and finally put back the XML into the PDF using iText. The programmatical solution is very difficult as it requires you to be familiar with the XFA syntax and the specs for XFA consist of several hundreds of pages.
You are talking about an AcroForm template
In this case, the root dictionary has an /AcroForm dictionary of which one of the entries is a /Fields array that isn't empty. You can create a PdfReader instance for this template and pass the reader object to PdfStamper. You then create the extra fields you need (text fields, button fields,...) and add them to the stamper using the addAnnotation() method.
This is shown in the SubscribeForm example. We have an existing template subscribe.pdf and we add several buttons to it, resulting in the new template subscribe_me.pdf.
If this doesn't answer your question, please clarify, as it's generally not accepted to limit your question to saying "I try to use PdfStamper but the result pdf is not template", you should at least show what you've tried, otherwise you risk that your question will be closed.

convert pdf to xml

i want to convert a PDF file having few images into xml using java.
Is there any api though which it can be done so that all the images and text of pdf will be converted into xml file.
please help.
Use pdftohtml.
It can be installed with brew install pdftohtml. This adds pdftohtml to your path.
So, to convert pdf to xml, you can run pdftohtml -xml your_file.pdf your_file.xml
Then, just use java or any other language to execute this command.
PDF is one of the worst format to work with. It is designed for rendering 2D graphics and text documents. There are libraries which allow you to manipulate PDF objects in PDF document but it will not be able to tell you whether an image is related to which paragraph. You will not be able to extract the semantic of it easily.
On the other hand, XML is desinged to store text data in a well structured manner. This means it contains implicit semantic. In order to convert from a format which does not have semantic to a format which have implicit you will need to add your own logic into the conversion process otherwise you will just end up having a mess in your XML which contradicts the whole purpose of using XML.
Since each PDF document is very much different, it is almost impossible to automate this without human aids.
If you are really determine to do it, I suggest you use a library to read PDF into objects, and start writing a converter from there. You will have to take care of newpage, newline, page number, headers, images, graphics, tables, and many more by yourself. Since XML is made mainly for text data, you will have to deal with graphics somehow if you want to store in XML, e.g. convert graphics into Base64 string.
iText is a library that allows you to create and manipulate PDF documents. It enables developers looking to enhance web- and other applications with dynamic PDF document generation and/or manipulation.
Developers can use iText to:
* Serve PDF to a browser
* Generate dynamic documents from XML files or databases
* Use PDF's many interactive features
* Add bookmarks, page numbers, watermarks, etc.
* Split, concatenate, and manipulate PDF pages
* Automate filling out of PDF forms
* Add digital signatures to a PDF file
iText is available in Java as well as in C#.
You could Base64 encode the entire PDF file's byte stream and serialize it into an XML document like "<pdf><![CDATA[BASE64ENCODEDPDFFILECONTENTS...]]></pdf>". =)

Categories

Resources