Lock digitally signed PDF acro-field after one time filling - java

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.

Related

Java iText PDF annotation/stamp not visible on Chrome and Firefox (browsers)

We have been trying to add annotation/stamp to an existing SIGNED PDF without invalidating the Signature but unfortunately browsers wont show the stamp/annotation. When opened in Adobe Reader the annotation can be seen there.
Any other idea is welcome. All we want is a little text on existing signed PDF that wont invalidate the signature.
Here is our code:
PdfReader reader = new PdfReader(pdf1);
PdfStamper pdfStamper = new PdfStamper(reader, new FileOutputStream(RESULT), '\0', true);
PdfContentByte pcb = new PdfContentByte(pdfStamper.getWriter());
PdfAnnotation annot = PdfAnnotation.createFreeText(pdfStamper.getWriter(), new Rectangle(150, 150, 200, 200), "Annotation 1", pcb);
annot.setFlags(PdfAnnotation.FLAGS_PRINT);
pdfStamper.addAnnotation(annot, 1);
pdfStamper.close();
You use the incremental update mode ('\0', true) thus (certain) additions to PDF documents are possible. That is the correct way
WebBrowsers provide only limited support for certain PDF features like Formfields or annotions. If you can see the annotation in the adobe reader but not in some webbrowers than those browsers do not support it. You have limited options to change that.
One possibility you might have is to not add the stamp as an annotation but as an image. But you have to test afterwards if acrobat reader still considers the signature as intact. (As mentioned only certain additions are allowed)
PdfReader reader = new PdfReader(pdf1);
PdfStamper pdfStamper = new PdfStamper(reader, new FileOutputStream(RESULT), '\0', true);
Image img = Image.getInstance("URL or Filename.PathToImage.png");
//position on the page x,y
img.setAbsolutePosition(380f, 750f);
img.scalePercent(20);
//1=pagenumber and you might also try getOverContent(1)
PdfContentByte underContent = super.stamper.getUnderContent(1);
underContent.addImage(img);
pdfStamper.close();

How can I open a pdf after I create that with iText?

I created a pdf with iText and I want to open it, but when I do that Adobe Reader says me "Error opening document. This file is already open or is used by another application".
How can I resolve?
This is my code (sorry for the Houston println exception ;)):
try {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("c:/Users/Gabriel/Desktop/"+txtName.getText().toString()+".pdf"));
PdfReader reader = new PdfReader("src/file.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("c:/Users/Gabriel/Desktop/"+txtName.getText().toString()+".pdf"));
AcroFields form = stamper.getAcroFields();
..code..
stamper.close();
//document.close();
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + "c:/Users/Gabriel/Desktop/"+txtName.getText().toString()+".pdf");
}catch (Exception exc) {
System.out.println("Houston we got a problem! : "+exc);
}
}
You are using an old iText version. In the old days, we had PdfWriter to create documents from scratch and we had PdfStamper to manipulate existing documents.
It seems that you want to fill out a form, which requires PdfStamper, but for some mysterious reason you also use PdfWriter as if you want to create a new PDF from scratch.
If you insist on using that old iText version, you shoild drop a couple of lines:
try {
PdfReader reader = new PdfReader("src/file.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("c:/Users/Gabriel/Desktop/"+txtName.getText().toString()+".pdf"));
AcroFields form = stamper.getAcroFields();
..code..
stamper.close();
}
In the old iText, there's really no reason for you to use the Document and PdfWriter class for form filling.
To avoid the confusion you're in, we rewrote iText from scratch, and we released this rewrite about 2 years ago. It's amazing to see that you chose an old version being new at iText. Moreover: iText 5 is no longer supported, so why not use iText 7?
Please read the iText 7 jump-start tutorial to see how form filling is done today: https://developers.itextpdf.com/content/itext-7-jump-start-tutorial-net/chapter-4-making-pdf-interactive

Add watermark to pdf java iText

I want to add a image or text watermark to pdf file. I found some examples online, but my case is a little bit different.
I have an existing pdf template which is already populated with dynamic data and converted to byte[]. This generated bytes are later exported to pdf.
I would like to add the watermark to that generated bytes.
Something like:
byte[] addWatermark(byte[] generatedBytes){
byte[] bytesWithWatermark;
//add watermark to bytes
return bytesWithWatermark;
}
I just can't seem to figure out how to do this with iText.
You say you already have examples for applying watermarks using iText. As you already have a PDF, you should use code from an example that adds watermarks to existing PDFs. This should be an example that works with a PdfReader / PdfStamper pair, e.g. those here, which all have the structure
PdfReader reader = new PdfReader(SOME_SOURCE);
PdfStamper stamper = new PdfStamper(reader, SOME_TARGET_STREAM);
[... add watermark to all pages in stamper ...]
stamper.close();
reader.close();
To make these example fit into your addWatermark method, simply use your byte[] instead of SOME_SOURCE and a ByteArrayOutputStream instead of SOME_TARGET_STREAM:
byte[] addWatermark(byte[] generatedBytes) {
try (ByteArrayOutputStream target = new ByteArrayOutputStream()) {
PdfReader reader = new PdfReader(generatedBytes);
PdfStamper stamper = new PdfStamper(reader, target);
[... add watermark to all pages in stamper ...]
stamper.close();
reader.close();
return target.toByteArray();
}
}
PS As you used only the tag itext and not the tag itext7, I assumed you were looking for a solution for iText 5.5.x. But the same principle as applied here, i.e. using your byte[] as source argument and a ByteArrayOutputStream as target argument, will also allow you to make iText 7.x examples fit into your method frame.

Is it possible to create non-printable pdf with using jasper or itext in java?

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

Merge PDF with FDF output only PDF

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();

Categories

Resources