How to make a pdf non-modifiable yet signable using Java? - java

I am trying to create a pdf which is not editable but signable. The desired state should be something like this:
But currently I'm using itext, and to allow signatures I need to do PdfWriter.ALLOW_MODIFY_CONTENTS | PdfWriter.ALLOW_MODIFY_ANNOTATIONS but naturally this also makes the pdf editable.
int pdfPermissions = PdfWriter.ALLOW_MODIFY_CONTENTS | PdfWriter.ALLOW_MODIFY_ANNOTATIONS;
pdfStamper.setEncryption(null, null, pdfPermissions, PdfWriter.ENCRYPTION_AES_128);
Is there some other library which can help me achieve the desired restrictions?

Related

How to dynamically resize text fields in Adobe Acrobat using Apache PDFBox?

I'm using Apache PDFBox to populate an existing Adobe Acrobat PDF template.
In this template I have fixed sizes of text fields in which I insert data from database.
In one text field the amount of data may vary so it would be great if text field could get bigger or smaller to adjust to the amount of text inserted.
The main problem is that I have another text fields below the one I want to resize dynamically so in case of making it bigger or smaller other text fields have to move relatively
I was searching for the solution but found only offers to resize font dynamically, but it doesn't satisfy my needs. I could create one text field and insert all of my data in it, but some of those text fields must have different color of text so it's important to have different text fields to be able to specify their color in code (as far as I know).
Is that even possible and if not what can I do to achieve the result I'm looking for?
P.S. Our code is based on templates from Adobe Acrobat and Apache PDFBox, so it's more preferable to keep it that way. But if this is not possible with those two, I will be glad to hear your recommendations on what should I use (open source first).
PDF Producer: Adobe Acrobat Pro DC (32-bit) 21.7.20102,
PDF Version: 1.6 (Acrobat 7.x),
Apache PDFBox Version: 2.0.26
You can find an example of my PDF below

Obtaining an Apache POI XSSFRichTextString from a textbox

How do I get an XSSFRichTextString from an Textbox in an excel spreadsheet using POI?
The setText() method is overloaded to set either a String or XSSFRichTextString but the getText() method only returns a String.
My approaches were as follows:-
Change just the text in the textbox but leave the formatting unchanged. I was hoping to get the string and simply change the text but there seems to be no setText method in XSSFRichTextString. It appears that you set the text in the constructor then apply formatting using methods. Using this does put the text in the textbox but it loses all formatting.
Extract the entire XSSFRichTextString, extract the Formatting, create a new RTS with the new text and apply the formatting. The problem is that while there is a setFont(Font object) method the getFont() returns only a short so I cannot seem to get the Font object and change it.
3 My last option is to set the plain text in the Textbox and then programmatically set all the font and formatting elements but this means burying the formatting in the Java code which means re-coding if the user needs to tweak the format instead of just using Excel.
Any suggestions?
Promoting some comments to an answer
You can't get a RichTextString from an Excel textbox. The storage model for formatted text in a textbox was changed from that in plain cells, mostly it seems to support extra features and kinds of formatting. Textbox text is a bit more Word-like in how it's structured / stored
However, there's good news - you can do what you want, and change some specific text in a textbox without changing the formatting!
Firstly, from your XSSFTextBox, call getTextParagraphs() to get the paragraphs
Next, call getTextRuns() to get the individual runs of text sharing the same formatting. Search through those until you find the one(s) containing the text you want to change. Finally, call XSSFTextRun.setText(String) to change the text of that run. The formatting will be unchanged

Use iText to produce formatted TextFields?

Is there any way in iText to format a TextField's input? I want to have a TextField accept a phone number "(###)###-####", but I don't want the user to have to format it when they enter it. Pdf supports masks on form fields, is there any way to do this in iText?
My current solution is to create the pdf in Acrobat, then populate known fields through iText. But that isn't ideal for this deployment. Ideally I'll have iText generate the entire form.
Thanks for all assistance in advance.
You can add JavaScript to your form that changes the content of fields. See for instance the Calculator example for a fun PDF that acts as a Calculator (obviously this app only works in a PDF viewer that supports JavaScript).
When you create a text field, you need to add an additional action with the setAdditionalActions() method. You can choose between different events: K for keystroke (e.g. useful if you want to change every character to uppercase when somebody fills out a form), Bl for blurred (useful to process the content of a field as soon as the focus is lost), etc.
You can write your own document-level JavaScript to format the fields. See calculator.js for the JavaScript used in the Calculator example. Or you can use one of the many AF methods that are predefined Adobe Reader, such as AFNumber_Format (I don't find an overview of the available methods right now).

Jasper Reports - Custom Barcode Generation

Libraries/Tools used:
1) Jasper Reports
2) iReport
3) Java
I've already generated some standard barcodes for my reports, but this time, I'm trying to generate a custom barcode, for which I've a font file custom-barcode.ttf. As of now, iReport supports 2 barcode libraries - Barcode4j and Barbecue, which don't support the custom barcode that I need. Any ideas to get started, without much overhead of using some new library (I'm using Barcode4j already)?
BTW, I'm aware that a similar question (custom barcode font) exists on this site already.
Just tried #mdahlman's answer and it worked. I generated the value "CODE123" using a barcode39 font (free) and Jaspersoft Barbecue.
Setting the size is not very easy using the font but the result is the same. I verified the barcode using Barcode Scanner on my Android phone (can see it's visually similar too). The reason this worked for me, probably same reason #bchetty's test didn't work, is because Barcode39 doesn't have a check-digit. It is a 1-to-1 translation except with a leading and trailing asterisk (*) appended to the data. If you want to use a ttf to generate a barcode type that has a check-digit you'll need a function (external jar like you mentioned) to encode it. Barcode39 doesn't need a function since it's just "*" + V${data} + "*".
Given that you have custom-barcode.ttf, it really can be treated as text. So your steps are like this:
Create a font extension in iReport for custom-barcode.ttf.
Create a Text Field in the report with a relevant expression.
Set the font for the Text Field to "custom-barcode" (or whatever you call your font extension). Play with the font size to get the desired output.
Using a custom font for a barcode could be considered a bit of a hack. But what it lacks in flexibility it makes up for in simplicity.

PDF annotations in Java

Actually, still now I have not got the exact way to add different types of annotation/lines/boxes/highlights using the mouse directly on PDF.
I have tried different API (pdfbox, icepdf, iText, aspose) and successfully able to add text, text annotation, box annotation, images, but for each one i have some issue like:
when i add object, it add in a new page
when i add object it remove the old data
i want use the existing ICEPDF viewer to add text, but it does not allow it. and if i try to mix other API with ICEPDF, then I am not able to get the mouse click position where need to add.
There is only one API (ICE PDF) which allow to add box annotation direct on GUI by mouse but it does allow other things.
If not possible in pdf viewer then is there any class which will allow to do this?
I'm also open for any commercial library/class with a one time fee to do that.
I think BFO have added quite a few annotation tools. Have you looked at their tool?

Categories