Changing the appearance of a checkbox field in iText - java

I need to be able to customize the checkbox fill type on demand as I render the pdf.
I must do this only with the AcroFields (pdfStamper.getAcroFields()), because I'm not creating any new fields (just modifying), and that's the only thing I have available to me in scope. I have tried about a hundred things, including the one listed below, which was my best guess on how to do this.
PdfDictionary dictionary = (PdfDictionary) acrofields.getFieldItem("ABCD").getWidget(0).get(PdfName.MK);
dictionary.put(PdfName.CA, new PdfString("8", PdfObject.TEXT_UNICODE));
ABCD is just for testing.
I am very stuck, and any help would be greatly appreciated. I am able to change the value in the dictionary, but it does not seem to have any affect when I write the pdf to a file. Other changes such as setting the checkbox to being checked/unchecked work, as well as populating text fields. So I as very surprised and confused why this is not working.

You're trying to change the caption of a check box, but it's unclear to me what you expect to see. Do you want to see the text "8"? In that case, changing the caption isn't sufficient. You also need to change the appearance. You can find the possible appearances under the /AP key. In the case of a check box, you'll find two possible appearance states under the normal appearance (/N). These XObjects define what you see when the PDF is rendered.

Related

How to get information about checkbox using itext7 library?

I need to get information about Checkbox for instance, checkbox mark(cross, circle, etc.), checked, unchecked and so forth. But I couldn't understand where this information has kept if in this file no XFA information. This file has been created via Adobe Acrobat Pro DC 19.21.20049. I tried to find out this information using pdfbox tool, but I didn't find it. That is a screenshot of checkbox information:
Can anyone explain to me how to get this information and where I must find it?
For any PdfFormField field with a value you can retrieve that value like this:
PdfObject value = field.getValue();
In case of a checkbox field this will return a PdfName. For an unchecked field, the name is Off. For a checked field it can be anything else even though the specification recommends Yes.
The appearances of these states are more difficult to determine because at least for the checked state a checkbox must provide an appearance stream containing instruction to create a visualization.
We have analyzed one such stream in response to your other recent question. In that case a ZapfDingbats tick symbol was used and you could apply text extraction to the stream and determine this.
In other cases, e.g. in case of a crossed check box, usually only vector graphics instructions are used.
It also is possible, though, to use a bitmap image here which may show anything.
Thus, while you can of course compare the appearance stream with the standard appearances generated by e.g. Adobe Acrobat and so identify the appearance of many checkbox fields, you won't be able to automatically identify all.

How to inject images into a Word template via docx4j without getting them resized

My program injects text and pictures into a Word template. This works great via content control data binding (thanks to docx4j and Content-Control-Toolkit).
My problem is, that images get resized after injection. What I actually want, is the behavoir that Jason decribed here: http://www.docx4java.org/forums/data-binding-java-f16/picture-content-control-size-t634.html
The current behaviour is to just let it be whatever its natural size is (at a given dpi), unless that is greater than page width, in which case it is scaled down.
According to that post, the behavoir of docx4j has been changed so that the pictures always fit the size of the content control with respect to the ratio.
Is it possible to get the "old" behavoir back? Do I have to do that on my own, or is the switch, that Jason wrote about, already implemented?
As the answer to How to force Docx4j to refresh a replaced image file states, the size of a picture is stored in the main document part. At the moment, I only use XPath to set content in the custom XML part. If there is any possibility to get what I need without touching the documents XML directly, I would really prefer that. A macro to set the size after opening the document in Word is no option for me.
The first thing to be aware of is that these days we prefer to have a picture in a rich text content control, as opposed to a picture content control.
This is because Word limits your ability to "float" a picture content control.
The handling for this is triggered by w:tag containing 'od:Handler=picture': datastorage/bind.xslt#L165
The basic behaviour is that if the w:sdtContent contains an existing w:drawing/wp:inline/a:graphic then reuse it, so any formatting thus configured is used.
But for a "legacy" picture content control which doesn't contain a:blip (when would this be?), xpathInjectImage is invoked with wp:extent passed in (see bind.xslt#L240).
At line 1143, if (cxl==0 || cyl==0) // Let BPAI work out size
So if you want the image at its natural size, you could try removing the when clause at bind.xslt#L212
By the way, we can also bind escaped XHTML. But there, we make an effort to fit any image not just to the page width, but if in a table cell, to that as well.

Find invisible text in iText

I am creating a PDF document of multiple pages using iText. I am adding some unique text on one of the pages in the middle of this document but making it invisible as-
Chunk chunk = new Chunk("invisible text here");
chunk.setTextRenderMode(PdfContentByte.TEXT_RENDER_MODE_INVISIBLE, 0f, null);
com.lowagie.text.Document iTextDoc.add(new Paragraph(Element.ALIGN_JUSTIFIED, chunk));
The reason for adding this invisible text is to identify this particular page at the time of onEndPage(). But it is failing.
To achieve in the onEndPage(), I have the following code -
boolean b = (pdfWriter.getDirectContent().toString()).contains("invisible text here");
I get the value of b as false.
If I compare any other text on that page(which is visible) results b as true.
I tried to manually search the invisible text in the PDF reader and it finds the text.
What could I modify to achieve this?
It is never a good idea to assume you can recognize text in the content without elaborate parsing. The text may be split into multiple segments, encoding might not be platform's default character encoding, etc... Thus don't try something like
boolean b = (pdfWriter.getDirectContent().toString()).contains("invisible text here");
You can achieve your goal
The reason for adding this invisible text is to identify this particular page at the time of onEndPage().
much more easily. Simply add a member to your PdfPageEvent implementation, i.e. the class with your onEndPage() method, and set it where you used to add the invisible page content to the text you used to add to the page.
Now you can test that member variable directly in your onEndPage(). Don't forget to reset the variable afterwards, preferably in onEndPage() itself!

Make String look like url in TableModel

Hello im making a program and using TableModel. I have 6 column filed with string's. I want the sixth column string make it look like URL. When i mean look like URL , i mean turn it into blue and be underlined. Is it possible to do that?
You need to write custom TableCellRenderer for your jtable. See this link.This may be helpful for you.
Sun had a very good tutorial (http://docs.oracle.com/javase/tutorial/uiswing/components/table.html#modelchange) on cerating jtable. just go through it before you start. hope this tutorial will help you.
But below is my suggestion.
("<html><b><u>link</u></html>");
<html><b>bold</b></html>
a similar kind of thing
However, I wouldn't recommend altering the data in your model just to effect display. Instead I would create a custom cell renderer which accomplishes this effect and set it on the table. You could either wrap your text in the HTML or manually set font color and style properties on the renderer to mimic html.
Now even if you were to include the url in the html you still can't click on it. There is no component in the table. You don't really want to go into edit mode when clicking on a url. You just want to open that link. To accomplish this you would add a mouse listener to the table itself. When you receive a click event, you would then programatically determine which cell it was over, go back to your model and get the url, and finally use other Java API calls to open that url.
I'm pretty sure you can simply create a string containing
<html>...</html>
and it will work. Just code your link inside the html tags as you would in html. You'll just have to add extra code if you want it to appear blue. I think:
<font color='blue'>
would do it

jsf Trinidad: Change tr:table header

I have a tr:table with the rowSelection property set to "single". It draws a extra column with a radio buttons inside. But I want to change the text displayed at that column's header. I don't find any property that allow me to accomplish this. I guess I should overwrite some properties file but I haven't found anything on this topic in the Trinidad documentation.
Thanks.
As Trinidad itself doesn't seem to offer this, you can do it with javascript. It is not a clean solution, but it will work.
First, open the generated html code and find the in which you are interested. If it has an ID - document.getElementById("yourformname:tdId"). If it doesn't, try accessing it with getElementsByTagName("td") called on the table. When you have it, set its innerHTML to whatever text you want.
It is an ugly workaround, but will possibly work

Categories