iText release notes mention that signing of PDFs with XFA forms is supported from iText versions 5.4.2 and 5.4.3:
http://itextpdf.com/history/?branch=54&node=542
http://itextpdf.com/history/?branch=54&node=543
Is there a documentation somewhere how to do the signing in Java?
I am specifically interested in signing PDFs with XFA where there is a prepared field for signature.
This is a small example showing how to sign using XmlDSig:
PdfReader reader =new PdfReader(src);
FileOutputStream os =new FileOutputStream(dest);
PdfStamper stamper = PdfStamper.createXmlSignature(reader, os);
XmlSignatureAppearance appearance = stamper.getXmlSignatureAppearance();
appearance.setXmlLocator(new XfaXmlLocator(stamper));
appearance.setXpathConstructor(new XfaXpathConstructor(XfaXpathConstructor.XdpPackage.Datasets));
ExternalSignature signature =new PrivateKeySignature(pk, digestAlgorithm,"BC");
//use signXmlDSig or signXades
MakeXmlSignature.signXmlDSig(appearance, signature, chain);
You can also sign using XAdES, but then you won't be able to validate the signature in Adobe software because I don't think Adobe already supports XAdES (please correct me if I'm wrong).
Related
This question already has answers here:
RTL not working in pdf generation with itext 5.5 for Arabic text
(3 answers)
Closed 4 years ago.
For my website, I use itextpdf 5.5.4 to generate PDF downloads. The website is meant for people who speak English. Recently, a user from Egypt used the site, entered some Arabic content, and contacted me with the problem he has.
This is his Arabic content shown correctly in the browser:
This is incorrect display in PDF:
Here is the Java code I have. Please note that it is actually able to generate PDF with Chinese characters CORRECTLY:
BASE_FONT base = BaseFont.createFont("/fonts/ARIALUNI.ttf", BaseFont.IDENTITY_H , BaseFont.EMBEDDED);
Font f = new Font(base, 10f);
String htmlString = string_with_Arabic_text;
Paragraph p = new Paragraph(htmlString, f);
p.setSpacingBefore(20.0f);
p.setSpacingAfter(7.0f);
document.add(p);
How to fix the problem?
In Eclipse (the IDE I use), I am able to see Arabic characters display correctly in htmlString. At this moment, I cannot upgrade to use the latest version of itextpdf due to various project reasons.
iText 5 has limited support for non-Western writing systems. It support right-to-left writing but only in the context of ColumnText and PdfPCell objects.
This is an iText 5 example with ColumnText where p contains text in Arabic:
ColumnText canvas = new ColumnText(writer.getDirectContent());
canvas.setSimpleColumn(36, 750, 559, 780);
canvas.setRunDirection(PdfWriter.RUN_DIRECTION_LTR);
canvas.addElement(p);
canvas.go();
This is an iText 5 example with PdfPCell where p contains text in Arabic:
PdfPCell cell = new PdfPCell(p);
cell.setRunDirection(PdfWriter.RUN_DIRECTION_RTL);
This is very annoying, as it would mean that you have to rewrite your entire application so that all text is added either in a ColumnText or in a PdfPCell object. You'd also have to examine the content to check if you need to change the run direction.
As you have to rewrite the application anyway, it would be best to upgrade to iText 7, because iText 7 has an add-on that detects the writing system based on the UNICODE values of the content (see pdfCalligraph). When Arabic or Hebrew text is detected, the add-on changes the writing system for "left to right" to "right to left." See How to display Arabic strings from RTL in PDF generated using itext 7 API?
I see that you are coding your document. Please note that you can save yourself a lot of work by creating the content in HTML, and then converting it to PDF using the pdfHTML add-on. The PDF to HTML tutorial has some examples involving Arabic. See the section on internationalization in chapter 6, and the following FAQ entries:
Which languages are supported in pdfHTML?
How to convert HTML containing Arabic/Hebrew characters to PDF?
iText 7 is also the first version that supports more writing systems, such as Devanagari, Tamil, Telugu,... For more info, read the pdfCalligraph white paper.
Important: the pdfCalligraph add-on is closed source. You'll need a trial license to test it and a commercial license to use it in production. Note that the current version of iText that you are using is licensed as AGPL software, which implies that you can't use your project in a closed source context. You mention external users, which means that you are distributing your service. Did you open source all your own source code? If not, you should purchase a commercial license for your use of iText.
PDF created will be based on dynamic HTML page.
Using ITEXT 5 or 7 with XMLWORKERHELPERCLASS would be lengthy process.
If i am using pdfcrowd API it seems to be ok but not able to generate on localhost or any other private ip. I am ready to pay for their services if they achieve above issue.
First you need to get iText 7 (the core library) and the pdfHtml add-on (the part that will parse the HTML+CSS and convert it to iText objects). Go to github to find out how to download these.
Suppose that you have this HTML:
With this corresponding CSS:
Then you can use this code snippet:
ConverterProperties converterProperties =
new ConverterProperties().setBaseUri(resoureLocation);
HtmlConverter.convertToPdf(
new FileInputStream(HTMLSource),
new FileOutputStream(pdfDestination), converterProperties);
Where resourceLocation points at your base URI, HTMLSource is the path to your HTML file, and pdfDestination is the path where you want the resulting PDF to be written.
When you execute this code, you will get the following PDF:
Note that buying a commercial license may be necessary if you intend to use iText in the context of a proprietary software project.
Is there a way to sign electronically a PDF generated by JasperServer?
As we can see in the wiki:
http://community.jaspersoft.com/wiki/pdf-output-digital-signing-not-implemented-jasperreports
JasperReports itself does not provide a direct way to sign digital
certificates in the PDF report output file nor implements such a
feature from iText which is the library used by JasperReports to
generate a PDF file.
We would need to customize the iText library (wich is the one used to export de documents).
EDIT: Link to test files below.
As part of a project, I have created dynamic PDF forms in LiveCycle Designer 11.0. These forms are set to be reader enabled in Acrobat before being forwarded to the developer. He then populates the PDF using iTextSharp 5.5.6.
This form has many dynamic properties including hidden fields, dynamic subforms, etc.
When the generated form is opened in Adobe Reader 11.0.12 (my version, but others are affected), the form is a) read only and b) does not show all of the dynamic fields even though the criteria has been satisfied to display them and they have been filled with data. Some are shown.
When it is opened in Adobe Acrobat 11.0.9, the form renders as expected. Sections & fields are displayed/hidden as the form's logic dictates. The form is also fillable, it is not read only or flattened.
When I compare the Security settings of the forms in Acrobat, vs Reader, the "Filling of form fields" setting in Acrobat is set to "Allowed", while it is set to "Not Allowed" in Reader's Security settings.
Here is a link to a zip containing the following:
1) A dynamic PDF generated in iTextSharp
2) An auto-generated class file for the form
3) The data mapping Program.cs file
Link to Test file showcasing the issue
When the file is opened in Acrobat, it works as expected. When it is opened in Reader, it becomes read only and both SignatureA and SignatureB are displayed, when only one should be shown based on which radiobutton is selected in the Signature Test section.
I do not have access to the codebase, so I cannot test the application on my end. Is there any setting in iTextSharp that may be causing this problem?
Thanks for any help you can provide!
Your PDF is not updated by an incremental update, which destroys the "reader enabled" feature (the digital signature becomes invalid due to a full rewrite).
You shall create the PdfStamper instance in append mode:
iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, ms, '\0', true);
I'm currently using iText PDF Lib (5.4.3)
When I used iText PDF ver 5.1.x
It's working good export Korean Language.
But when I changed latest version(5.4.3) .
It can't export Korean Language.
I was tried set Chartset also.
XMLWorker worker = new XMLWorker(pipeline, true);
XMLParser xmlParser = new XMLParser(true, worker, Charset.forName("euc-kr"));
document.open();
xmlParser.parse(strReader);
document.close();
And I checked font(malgun.ttf) also.
But, still it's not working.
I don't know what I missed something... :(
Please help..
Thanks.!
please read the changelogs of iText, more specifically for version 5.2.0. From this version on, you need to replace the iTextAsian.jar (see extra jars directory) because we changed the way CJK fonts are used.