Save user input PDF Template with iText - java

I have a pdf form where user enters some info, like name and address.
Using iText, can I save new pdf file with user input? How can I get the text value of what user entered like this, stamper.getAcroFields().setField("name", nameTextBox.text);? Many sample codes use hard coded value but I want use what user entered in the textbox.
PdfReader reader = new PdfReader("C:/temp/Template.pdf");
FileOutputStream fileOutputStream = new FileOutputStream("C:/temp/TemplateTest.pdf");
PdfStamper stamper = new PdfStamper(reader, fileOutputStream);
//Can I do something like this?
stamper.getAcroFields().setField("name", nameTextBox.text);
stamper.getAcroFields().setField("address", addressTextBox.text);
stamper.getAcroFields().setField("city", cityTextBox.text);
stamper.getAcroFields().setField("zip", zipTextBox.text);

You have a PDF where the user enters some info.
Question 1: how can the user submit the form data?
[a.] The user can not safe the PDF locally, unless the document is Reader-enabled or if the user has a version of Adobe Acrobat (not Reader). Reader-enabling a document can only be done with Adobe software; see Editable PDF using itext (5.3.0) in java. Not able to save data in Adobe Reader X
[b.] The user can submit the fields to a server. This can be done in the form of a querystring, an FDF file, or an XFDF file. If the form is Reader-enabled or if the user has Acrobat, he can also submit the complete filled-in PDF to the server.
Question 2: how can you store the form data in a PDF?
If the user submits the complete PDF, then you do not need to store the form data in the PDF. It is already there. You can retrieve the info like this:
String name = reader.getAcroFields().getField("name");
If you get the information as a querystring, just examine the key/value pairs and use the code you already have in your question. If the information is stored in an FDF file, you need FdfReader. IF the information is stored in an XFDF file, you need XfdfReader.
Question 3: how do you use what the user entered in the textbox?
This question is unclear. What are you talking about?
If the text box is a text box in the PDF, see [a.] under question 1.
If the text box is a text box in a web page, see [b.] under question 1.
If the text box is a text box in some standalone application, see the answer to question 2.
All in all, your questions are of very poor quality. When you say 'many samples use hard coded value', it is clear that you didn't read the official documentation.

Related

How can i create a PDF form, than fill it with user generated data in Java, with iText?

Let's say if i have this PDF form. How can i fill it with user generated data, in Java?
For instance:
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
form.getField("test").setValue(value);
You can find a lot of examples there:
https://itextpdf.com/en/resources/examples/itext-7/filling-out-forms

i am trying to fill a pdf form through java code to generate dynamic filled form for different customers

My code is like :
// Open file
File file=new File("abc.pdf");
newdoc=new PDDocument();
doc=PDDocument.load(file);
//Select Page
int pageIndex=0;
newpage=newdoc.getPage(pageIndex);
content=new PDPageContentStream(newdoc, newpage);
content.drawString(text);
Why details which i filled, are reflecting to all pages
I need to tick few checkMarks in form. How to do that in java?
for ex: 1.java 2.c++ 3.Python I need to make a tick mark on java for all users.
For this, I am using pdfbox-app library. the problem is changes are reflecting for all pages, but I want to edit only first page.??
Help me with this, please.
If you are not able to understand my question then please let me know.

Have PDF appear in HTML rather than loading with Adobe or into separate window

I have a java web application that in theory, when working correctly, will display a PDF in the browser window next to some input text boxes so that an employee could index the document and store it away.
JSP URL: localhost:1234/Application
What is currently happening is this:
->User tells JSP to tell servlet to go into filesystem and grab a batch of PDFs
-->JSP displays which batch(folder) was grabbed, and the files contained inside the folder to the screen
EXAMPLE:
Folder1:
-document1 (button)
-document2 (button)
-document3 (button)
--->Person clicks on button that appears next to document name on the JSP and the servlet serves up the PDF data in a byte array as follows:
File file = new File(Dir + "\\" + batchName + "\\" + fileName);
byte[] by = new byte[(int) file.length()];
FileInputStream fis = new FileInputStream(file);
fis.read(by);
fis.close();
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment; filename=TheDocument." + "pdf");
response.getOutputStream().write(by);
response.getOutputStream().flush();
response.getOutputStream().close();
The PDF then opens in a SEPARATE WINDOW than what the application is running on, and the PDF file is downloaded to the machine into the downloads folder
What happens after this does not matter in the scope of this question.
What I WANT to happen is this:
User tells JSP to tell servlet to go into filesystem and grab a batch of PDFs
JSP displays which batch(folder) was grabbed, and the files contained inside the folder to the screen
Person clicks on button next to document name in the JSP and the servlet serves up the PDF document and loads it into the html page so that the person can view it in real time on the same window and do their indexing.
Changing the header to inline does not give me the desired effect as it loads the PDF in the same browser window, but only by clicking back can I get back to my JSP. It essentially loads up a whole new HTML/JSP window. The browser actually points right to my servlet in this case:
localhost:1234/Application/Servlet
Below is what I want the application to look like:
I've looked into several jquery plugins such as PDFObject, Colorbox and many others. I do not seem to get the desired effect that I want.
You could use pdf.js to render the pdf, with a Flash fallback for browser that aren't supported by pdf.js.
The helloworld example renders a pdf in a <canvas> element.
Whether a PDF opens in a separate application or in the browser is a setting in the browser, in Adobe, and in Windows Explorer (assuming Windows). You have no control over it, short of displaying your PDF in a Flash app, and then the user might not have Flash.
Not sure if this is what you are looking for but typically you can put a PDF in an iframe and have it opened up that way. The PDF just needs to be published on your server.
This answer may help you:
How to open a PDF file in an <iframe>?

Creating a dynamic PDF in Java

This is not a duplicate question. I had searched and tried many options before posting this question.
We have a web page, in which user should be able to input data in text boxes, text areas, images and also Rich Text editors. This data has to be filled in an existing report, like filling the blanks.
I was able to achieve the functionality using Apache FOP when the user input is simple text. But Apache FOP doesn't work if the user input is Rich Text(html format). FOP will not render html, and it just pushes the html code(ex: <strong> XYZ /strong>) into the pdf.
I tried using iText, but the setback here is that even though iText supports rendering of html to pdf, it is not able to place the images, that are included in <img> tags, in the pdf file.
I can try to create a pdf using iText api block by block, but the problem is rich text data entered by the user can not be embedded between the code since building pdf block by block and html to pdf can not be done together in iText. Or at least that is what I think from my experience.
Is there any other way to create a pdf file from java with images, rich text rendering as it is, headers and footers?
iText provides the capability to convert HTML Data to Pdf. Below is the snippet to do it :
Lets assume the html data is available as Input Stream (If its a String then we can convert it to InputStream using Apache Commons - IOUtils)
InputStream htmlData; // Html Data that needs to converted to Pdf
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Document document = new Document();
PdfWriter pdfWriter = PdfWriter.getInstance(document, outputStream);
document.open();
// convert the HTML with the built-in convenience method
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document, htmlData);
document.close();
// outputStream now has the required pdf data
I am working as Social Media Developer for Aspose and to add rich text to a form field in PDF file, you can try our Aspose.Pdf for Java API. Check the following sample code:
// Open a PDF document
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("c:\\data\\input.pdf");
//Find Rich TextBox field using Field Name
RichTextBoxField textBoxField1 = (RichTextBoxField)pdfDocument.getForm().get("textbox1");
//Set the field value
textBoxField1.setValue("<strong> XYZ </strong>");
// Save the modified PDF
pdfDocument.save("c:\\data\\output2.pdf");
I am not trying to market or promote this product. This api actually solved our problem so thought of mentioning it as it might help fellow developers. please let me know if this is against your policy.
I finally realized that the solution for my requirement can not be achieved with either FOP, iText, Aspose, Flying Saucer, JODConverter.
I found a paid api Sferyx. This api allows to render a very complex html to pdf almost preserving the original style. It also renders the images included in the html. We are still exploring this api and will post what other features this api provides.

How to show the printable PDF document with in the browser?

I want show the printable document directly with in the browser when I clicked on print button in my application. I already checked the 'Display PDF in browser' in adobe reader preferences and other PDF files are also being opened in my browser other than that my printable documents. Can you please suggest me
Thanks,
Vara Kumar PJD
Check that PDF sent as response when clicken on print button has correct MIME type (application/pdf). THis will allow browers to recognize it as PDF content and handle it as you want.

Categories

Resources