I have a PDF and FDF file and want to merge them into a single PDF. The .pdf has no fields, these are all included in the .fdf file, the contents of the fields also.
I have several solutions found, but these only works when the fields are stored in the pdf.
I would like to use iText with Java.
Does anyone have some demo code how I can solve my problem? Thank you!
Here's some code which only works when the fields are already present in the PDF
PdfReader pdfreader = new PdfReader(pdfInPath);
PdfStamper stamp = new PdfStamper(pdfreader, new FileOutputStream(pdfOutPath));
FdfReader fdfreader = new FdfReader(fdfInPath);
AcroFields form = stamp.getAcroFields();
form.setFields(fdfreader);
stamp.close();
Related
What I am trying to achieve is to replace a text in pdf file. I have the following code:
PdfReader reader = new PdfReader("test.pdf");
PdfDictionary dict = reader.getPageN(1);
PdfObject object = dict.getDirectObject(PdfName.CONTENTS);
if (object instanceof PRStream)
{
PRStream stream = (PRStream) object;
byte[] data = PdfReader.getStreamBytes(stream);
System.out.println(new String(data));
stream.setData(new String(data).replace("application", "HELLO WORLD").getBytes());
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("test-output.pdf"));
stamper.close();
reader.close();
When I trying to print out to see the data (System.out.println(new String(data))), "application" is showing as "ap)-4(plica)-3(tion", that's the reason why I failed to replace the text, any idea or other method that can achieve what I trying to achieve?
You will not be able to do this with iText.
Believe me, this is one of the most frustrating discoveries about PDFs: you can build them with iText, but you cannot go back later and replace text with something else, as you have in your example.
There really is not much you can do about it. Once text is there, you can't modify it.
All that notwithstanding, you can usually ADD new content (text, images, etc.) to an existing PDF. So... if you can alter the universe slightly and create a PDF with empty space in the correct size, you can go back later and use the PdfStamper class to "stamp" on another layer of graphical content.
More on this can be found in the iText documentation, and in this fine question:
How to add Content to a PDF using iText PdfStamper
I want to create non-printable pdf with using jasper, however users can display pdf but they can't print pdf. I don't want any pdf password restriction. I've researched jasper documentation but i couldn't found anything about it.
Is there anyway to solve my issue? I've already thank to you for your helps.
You can use iText's PdfStamper to post-process your PDF :
byte[] pdfContent = /* your PDF content here */;
ByteArrayOutputStream stampedPdfContent = new ByteArrayOutputStream();
PdfReader reader = new PdfReader(pdfContent);
PdfStamper stamper = new PdfStamper(reader, stampedPdfContent);
stamper.setEncryption(null, null, ~(PdfWriter.ALLOW_PRINTING), PdfWriter.STANDARD_ENCRYPTION_128);
stamper.close();
pdfContent = stampedPdfContent.toByteArray(); // your PDF is now non printable
I create a pdf document and specify few acro-fields. These acro-fields used to be filled by java itext library. This document is digitally signed after adding all required acro-fields.
We already set form filling property in digital signature but we have requirement to fill these acro-fields and make them read only after one time filling without invalidating digital signature.
Here is the code that I am using to fill this document -
String FILE = "/Users/xyz/Desktop/test1.pdf";
PdfReader reader = new PdfReader(pdfReader, new FileOutputStream(pdfTemplatePath), '\0', true);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(FILE), );
AcroFields form = stamper.getAcroFields();
form.setField("Name", "test 123");
stamper.close();
reader.close();
I am using lowagie itext library for filling pdf form.
Is there any way to fix this issue.
Thanks.
I'm learning about Java and iText API to generate PDF reports. What I want is very simple: add a Paragraph to my PDF file (Report.pdf). It works perfectly in my code:
doc = new Document(PageSize.A4.rotate());
PdfWriter.getInstance(doc, new FileOutputStream("Report.pdf"));
doc.open();
doc.add(new Paragraph("One!"));
Now I want to add my Paragraph in the end of file if Report.pdf exists. I've tried to use true in FileOutputStream as a 2nd parameter but it didn't work. I suspect is because of my line one generates an empty document, but I'm not sure. Does anyone knows how to solve it?
I have a XFA based pdf form that we need to be populate using java.
Can you suggest the best approach.
I was able to generate the xfa xml for the pdf using iText.
public void readXfa(String srcPdfFilename, String destXMLFilename) throws IOException, ParserConfigurationException, SAXException, TransformerFactoryConfigurationError, TransformerException {
PdfReader reader = new PdfReader(srcPdfFilename);
XfaForm xfa = new XfaForm(reader);
Document doc = xfa.getDomDocument();
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
FileOutputStream os = new FileOutputStream(destXMLFilename);
tf.transform(new DOMSource(doc), new StreamResult(os));
reader.close();
}
I have the Pdf and Xfa XML generated from code above.
Can you please suggest me how to proceed further as I seem to be out of ideas. I tried to check the XFA documentation but does not seem right.
I do not have an xml and the pdf is very complex as it has many fields and is a dynamic XFA pdf form.
Your help and suggestions will be sincerely appreciated.
Please take a look at the FillXFA example. This example has a form (purchase_order.pdf) that contains a dynamic table:
Now we add data.xml, which is a set of data in the form of an XML file, we get this result: purchase_order_filled.pdf
As you can see, the data present in the XML file was added, as well as a number of empty rows (which correspond with empty data rows in the XML). As we added many rows, we even triggered a new page to be added to the PDF.
This is how it's done:
public void manipulatePdf(String src, String dest)
throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
XfaForm xfa = form.getXfa();
xfa.fillXfaForm(new FileInputStream(XML));
stamper.close();
reader.close();
}
There's another example here: XfaMovies. The xfa_movies.pdf document is a single page PDF without any buttons. We inject the data of 120 movies: movies.xml. The result is the 23-page PDF, xfa_filled_in.pdf.
NOTE: reading your code, you get an XML that contains the complete XFA stream. You don't need that, you only need the data. That's why I added the links to the XML files: they don't contain any XFA specific tags.