Remove page reference from annotation - java

I added a textfield with several kids similar as described here. Did that to use the autofill functionality of PDF...
Now my question is how am I able to remove the page reference from the parent element? The data field should not contain a parent reference since it is not related to any page. The widgets should contain those (which I added there however I can't remove the parent /P page reference)
I tried
PdfFormField parent = PdfFormField.createTextField(stamper.getWriter(), false, false, 0);
parent.setFieldName(fieldName);
for (int page = 1; page <= pages; page++) {
TextField textField = new TextField(stamper.getWriter(), new Rectangle(560, 600, 590, 800), null);
PdfFormField pff = textField.getTextField();
parent.addKid(pff);
// add widget to each page
pff.setPlaceInPage(page);
//iText workarounds
field.put(PdfName.P, stamper.getWriter().getPageReference(page));
field.remove(PdfName.FF);
field.remove(PdfName.FT);
}
//in addAnnotation() the page reference is written
stamper.addAnnotation(parent, 1);
//does not work
parent.remove(PdfName.P);
however it didn't work since I guess the page reference is already written. Is there a way to remove it afterwards?

Fact 1:
The PdfFormField class extends the PdfAnnotation class, because most of the times, it is possible to merge a field dictionary with an annotation dictionary.
In your case, you have a PdfFormField that is used as a hierarchical element and although that element is also an instance of PdfAnnotation, it isn't. You can check this by using the isAnnotation() method. It will return false.
Fact 2:
When you add an annotation to an existing PDF, you have to use PdfStamper's addAnnotation() method that not only accepts an annotation object, but also a page number. If you didn't add a page number, PdfStamper wouldn't know where to add the annotation.
When you add a real annotation, PdfStamper will add a /P key that refers to the page where the annotations is visualized.
Fact 3:
There is no other way than using addAnnotation() with an annotation and a page number when working with PdfStamper. When you add a PdfFormField object that is not a real annotation, it won't be treated differently, hence a /P entry will be added, although that doesn't really make sense (as you rightly point out in your question).
Fact 4:
The /P entry is optional. For the widget annotations to be displayed correctly, it is sufficient that they appear in the /Annots of a page.
Conclusion:
iText shouldn't add a /P entry in case you add a PdfFormField that is not a real annotation. Hence I have committed the following change: revision 6756
void addAnnotation(PdfAnnotation annot, int page) {
- annot.setPage(page);
+ if (annot.isAnnotation())
+ annot.setPage(page);
addAnnotation(annot, reader.getPageN(page));
}
I have tested this with the AddFieldAndKids example and it seems to work: the /P entry is no longer added.
This solves your problem in a more robust way. You shouldn't try to remove something that shouldn't have been added in the first place.

Related

JasperReports export to Excel uses only last set background color

Im pretty pretty new to Dynamic-Jasper, but due to work i had to add a new feature to our already implemented solution.
My Problem
The Goal is to add a Column to a report that consists only out of a background-color based on some Information. I managed to do that, but while testing I stumbled upon a Problem. While all my Columns in the html and pdf view had the right color, the Excel one only colored the fields in the last Color.
While debugging i noticed, that the same colored Fields had the same templateId, but while all Views run through mostly the same Code the Excel one showed different behavior and had the same ID in all fields.
My Code where I manipulate the template
for(JRPrintElement elemt : jasperPrint.getPages().get(0).getElements()) {
if(elemt instanceof JRTemplatePrintText) {
JRTemplatePrintText text = (JRTemplatePrintText) elemt;
(...)
if (text.getFullText().startsWith("COLOR_IDENTIFIER")) {
String marker = text.getFullText().substring(text.getFullText().indexOf('#') + 1);
text.setText("ID = " + ((JRTemplatePrintText) elemt).getTemplate().getId());
int rgb = TypeConverter.string2int(Integer.parseInt(marker, 16) + "", 0);
((JRTemplatePrintText) elemt).getTemplate().setBackcolor(new Color(rgb));
}
}
}
The html view
The Excel view
Temporary Conclusion
The same styles uses the same Objects in the background and the JR-Excel export messes something up by assigning the same Object to all the Fields that I manipulated there. If anyone knows of a misstake by me or potential Solutions to change something different to result the same thing please let me know.
Something different I tried earlier, was trying to set the field in an evaluate Method that was called by Jasper. In that method we assign the textvalue of each field. It contained a map with JRFillFields, but unfortunatelly the Map-Implementation denied access to them and just retuned the Value of those. The map was provided by dj and couldn't be switched with a different one.
Edit
We are using JasperReports 6.7.1
I found a Solution, where I replaced each template with a new one that was supposed to look exactly alike. That way every Field has its own ID guaranteed and its not up to chance, how JasperReports handles its Data internaly.
JRTemplateElement custom =
new JRTemplateText(((JRTemplatePrintText) elemt).getTemplate().getOrigin(),
((JRTemplatePrintText) elemt).getTemplate().getDefaultStyleProvider());
custom.setBackcolor(new Color(rgb));
custom.setStyle(((JRTemplatePrintText) elemt).getTemplate().getStyle());
((JRTemplatePrintText) elemt).setTemplate(custom);

Vaadin-8 ComboBox click has no effect

I have a ComboBox in my Vaadin 8 code.
allAtts here is a Set of Attendant-s, and the field theAtt in there is of type String:
private ComboBox<Attendant> theCB = new ComboBox<Attendant>(null,allAtts);
theCB.setEmptySelectionAllowed(false);
Binder<SearchArgs> binder = new Binder<SearchArgs>(SearchArgs.class);
binder.setBean(sas);
binder.forField(theCB).bind("theAtt");
..
VerticalLayout vl = new VerticalLayout(theCB, rb, deleteBtn);
What's more - this exact flow of theCB is a copy-paste from another class i wrote, on the very same type Attendant.
The problem here is the drop-down click on theCB is not working. That is, clicking that little chevron-down icon on the ComboBox is producing no effect. What could this be!?
I tried removing the other items, rb, deleteBtn in vl. Still didn't work.
Nothing else happening to theCB anywhere else.
TIA.
EDIT:
theCB is otherwise functional. typing in the field is allowed, brings the choices and returns the selection accurately.
EDIT-2
https://github.com/vaadin/vaadin-combo-box/issues/680 is not the issue. nothing changing when i pass a non-null label value to the constructor. Besides, it's working fine with null label in that other place in the code.
EDIT-3
Attendant contains several fields, one of which is String att.
SearchArgs composes Attendant in a its field theAtt. Attendant.toString() returns att only.
Once again - this exact same logic is working, as i'm typing these, in another part in the code.
Also note: i since also tried the following without success:
binding theCB on a separate binder, i.e. declare a second binder on sas and use that one for binding theCB
cloning the Attendant objects that go into theCB as data-provider.

iText : linked textField can only be change on first page

I'm new to itext, and I need to create a pdf, with the same field on every page so that when I change the value of one of them they all change and have the same value.
So far I'm creating a TextField on each page, use it to get a PdfFormField and then I add it to a global parent PdfFormField. I add annotation to the parent and to the children and it's almost working the way I want except for one thing :
When I want to select the field with the mouse, I can only do it on the first page, on the others, the cursor rotate 90° and I can't select the text in the field. I can get to the needed field with tab, and changing it change all the value in the pdf as I want it to, but I can't access it with the cursor.
I checked the itext doc and search for examples/tutos, didn't find anything that helped so far
Does anyone have an idea why it does that ? Is there problem with the way I'm doing it ? At the beginning of the document I rotate it (need to be landscape) can it cause this problem ?
Thx in advance,
Edit :
private void addChangeableField(String fieldName, TextField textField, PdfFormField parent) throws IOException,
DocumentException {
PdfFormField datafield = textField.getTextField();
datafield.setFieldName(fieldName);
datafield.setFieldFlags(PdfFormField.FF_EDIT | PdfFormField.FF_DONOTSCROLL);
datafield.setMKTextPosition(PdfFormField.Q_CENTER);
parent.addKid(datafield);
writer.addAnnotation(parent);
writer.addAnnotation(datafield);
}
Here is how I add a field that can be change and modify other fields
And here is the example http://docdro.id/To19NKQ

How to use Nebula NatTable's PreserveSelectionModel?

I am developing an RCP Application, and am using Nebula's NatTable for that.
When it comes to selection, I am failing to understand how I am supposed to use it.
What I want is:
I want to have entire Rows selected. I was able do that using the RowOnlySelectionConfiguration and the RowOnlySelectionBindings.
If I select a row, I want the selection to stay there and not be cleared when some data in that row gets updated. How do I do that?
If a row is selected, and the position of the element in that row changes (e.g. one of the previous elements is removed, and the position changes to index - 1), I want the selection to change the position with the element, so that the same element is selected after the change. How do I do that?
I have seen that the documentation talks about a PreserveSelectionModel that can be used for that:
If you used the PreserveSelectionStructuralChangeEventHandler workaround in previous versions for not clearing the selection on structural changes, you will notice that this workaround will not work anymore. If you still need that behavior, you are now able to achieve the same by configuring and setting a SelectionModel instance like this:
SelectionModel model = new SelectionModel(selectionLayer);
// configure to not clear the selection on structural changes
model.setClearSelectionOnChange(false);
selectionLayer.setSelectionModel(model);
If you expect that the selection should update and move with structural changes (e.g. sorting), try to use the PreserveSelectionModel.
https://www.eclipse.org/nattable/nandn/nandn_120.php
So I guess I have to use the PreserveSelectionModel? But there I can't call setClearSelectionOnChange(false). Does it do that by default?
And how do I use the PreserveSelectionModel? What do I pass in the constructor?
I implement my own BodyLayerStack, in a class called TableBodyLayerStack, where I tried this in the constructor:
public TableBodyLayerStack(IUniqueIndexLayer underlyingLayer) {
super(underlyingLayer);
columnReorderLayer = new ColumnReorderLayer(underlyingLayer);
columnHideShowLayer = new ColumnHideShowLayer(columnReorderLayer);
selectionLayer = new SelectionLayer(columnHideShowLayer, null, true, false);
PreserveSelectionModel<?> selectionModel = new PreserveSelectionModel<>(
selectionLayer, null, null);
selectionLayer.setSelectionModel(selectionModel);
selectionLayer.registerEventHandler(new SelectEventHandler(selectionLayer));
viewportLayer = new ViewportLayer(selectionLayer);
setUnderlyingLayer(viewportLayer);
registerCommandHandler(new CopyDataCommandHandler(selectionLayer));
}
Then, in the contructor of my implementation of the GridLayer, I do this:
// ...
bodyLayer = new TableBodyLayerStack(eventLayer);
// register different selection move command handler that always moves by row
bodyLayer.getSelectionLayer().addConfiguration(new RowOnlySelectionConfiguration<T>());
// register selection bindings that will perform row selections instead of cell selections
// registering the bindings on a layer that is above the SelectionLayer will consume the
// commands before they are handled by the SelectionLayer
bodyLayer.addConfiguration(new RowOnlySelectionBindings());
// ...
But this is giving me NullPointerExceptions in the PreserveSelectionModel.
Error while painting table: null
java.lang.NullPointerException
at org.eclipse.nebula.widgets.nattable.selection.preserve.PreserveSelectionModel.getRowPositionByRowObject(PreserveSelectionModel.java:520)
at org.eclipse.nebula.widgets.nattable.selection.preserve.PreserveSelectionModel.createMarkerPoint(PreserveSelectionModel.java:559)
at org.eclipse.nebula.widgets.nattable.selection.preserve.PreserveSelectionModel.getSelectionAnchor(PreserveSelectionModel.java:531)
at org.eclipse.nebula.widgets.nattable.selection.SelectionLayer.getSelectionAnchor(SelectionLayer.java:276)
at org.eclipse.nebula.widgets.nattable.selection.SelectionLayer.getConfigLabelsByPosition(SelectionLayer.java:415)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getConfigLabelsByPosition(AbstractLayerTransform.java:316)
at org.eclipse.nebula.widgets.nattable.layer.AbstractIndexLayerTransform.getConfigLabelsByPosition(AbstractIndexLayerTransform.java:318)
at org.eclipse.nebula.widgets.nattable.layer.CompositeLayer.getConfigLabelsByPosition(CompositeLayer.java:553)
at org.eclipse.nebula.widgets.nattable.layer.cell.AbstractLayerCell.getConfigLabels(AbstractLayerCell.java:48)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayer.getCellPainter(AbstractLayer.java:354)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getCellPainter(AbstractLayerTransform.java:336)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getCellPainter(AbstractLayerTransform.java:336)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getCellPainter(AbstractLayerTransform.java:336)
at org.eclipse.nebula.widgets.nattable.layer.AbstractIndexLayerTransform.getCellPainter(AbstractIndexLayerTransform.java:340)
at org.eclipse.nebula.widgets.nattable.layer.AbstractLayerTransform.getCellPainter(AbstractLayerTransform.java:336)
at org.eclipse.nebula.widgets.nattable.layer.AbstractIndexLayerTransform.getCellPainter(AbstractIndexLayerTransform.java:340)
at org.eclipse.nebula.widgets.nattable.layer.CompositeLayer.getCellPainter(CompositeLayer.java:586)
at org.eclipse.nebula.widgets.nattable.painter.layer.CellLayerPainter.paintCell(CellLayerPainter.java:171)
at org.eclipse.nebula.widgets.nattable.painter.layer.CellLayerPainter.paintLayer(CellLayerPainter.java:81)
at org.eclipse.nebula.widgets.nattable.painter.layer.GridLineCellLayerPainter.paintLayer(GridLineCellLayerPainter.java:106)
at org.eclipse.nebula.widgets.nattable.selection.SelectionLayerPainter.paintLayer(SelectionLayerPainter.java:95)
at org.eclipse.nebula.widgets.nattable.layer.CompositeLayer$CompositeLayerPainter.paintLayer(CompositeLayer.java:913)
at org.eclipse.nebula.widgets.nattable.painter.layer.NatLayerPainter.paintLayer(NatLayerPainter.java:43)
at org.eclipse.nebula.widgets.nattable.NatTable.paintNatTable(NatTable.java:408)
at org.eclipse.nebula.widgets.nattable.NatTable.paintControl(NatTable.java:403)
...
I guess it is because I pass null values in the constructor of my PreserveSelectionModel. But how do I use it instead? What do I have to pass as arguments for the constructor? Where do I get the values from?
Any help is appreciated.
you are on the wrong track to achieve your goals. First I will answer your questions:
So I guess I have to use the PreserveSelectionModel?
No, the PreserveSelectionModel is intended to preserve the selection for cell selections. You want to preserve the selection for whole rows. So you need to use the RowSelectionModel.
But there I can't call setClearSelectionOnChange(false). Does it do that by default?
Yes
But this is giving me NullPointerExceptions in the PreserveSelectionModel. I guess it is because I pass null values in the constructor of my PreserveSelectionModel.
Yes
What do I have to pass as arguments for the constructor? Where do I get the values from?
The second parameter is IRowDataProvider<T>, so it is the IDataProvider for the body.
The third parameter is IRowIdAccessor<T>. You need to create an implementation that provides an unique id, so a row can be identified without knowing the position or index in the underlying collection.
So what you need to do is something like this:
selectionLayer.setSelectionModel(new RowSelectionModel<Person>(
selectionLayer, bodyDataProvider, new IRowIdAccessor<Person>() {
#Override
public Serializable getRowId(Person rowObject) {
return rowObject.getId();
}
}));
But of course you need to provide the IDataProvider and also the IRowIdAccessor to your TableBodyLayerStack if you want to keep it generic.
Also note that you don't have to call SelectionLayer#registerEventHandler() yourself! This is done internally by calling SelectionLayer#setSelectionModel().
You can find several examples in the NatTable Examples Application at https://www.eclipse.org/nattable/ (the Try it! button on the right side).
For your question the Tutorial Examples -> Layers -> Selection -> RowSelectionExample seems to be the one to look at.

Is it possible to add 'Open a web link action' action to acroform field programmatically using iText?

In Adobe Acrobat there is a possibility to add 'Open a web link action' to acroform. Is it possible to do so with iText usind already existing acroform?
I was unable to find any mention about it at iText docs and therefore tried to create new acrofield programmatically and add this action to it, but without success. Here's my code:
PdfReader pdfReader = new PdfReader(templateStream);
PdfStamper stamper = new PdfStamper(pdfReader, new FileOutputStream("delivery.pdf"));
stamper.setFormFlattening(true);
stamper.getAcroFields().setField("package", packages);
stamper.getAcroFields().setField("purchase_id", purchaseId);
stamper.getAcroFields().setField("activation_code", activationCode);
if (partner != "") {
PdfFormField field = PdfFormField.createTextField(stamper.getWriter(), false,
false, 100);
field.setFieldName("partner");
PdfAction action = new PdfAction(partner);
field.setAction(action);
field.setColor(new BaseColor(0,0,255));
PdfAppearance appearance = stamper.getUnderContent(1).
createAppearance(200, 20);
appearance.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED), 12f);
appearance.setColorFill(BaseColor.BLUE);
field.setAppearance(PdfAnnotation.APPEARANCE_DOWN, appearance);
field.setDefaultAppearanceString(appearance);
stamper.getAcroFields().setField("partner", "Click here to show partner's web site");
}
The resulting PDF document is shown without partner field. Please point me to some docs or to mistake at my code.
You are trying to add interactivity to a form. However, you are also throwing away all interactivity by using this line:
stamper.setFormFlattening(true);
You also claim that you are adding an extra field. As far as I can see, that claim is false. You create a field name field (and you create it the hard way; I would expect you to use the TextField class instead). However, I don't see you adding that field anywhere. I miss the following line:
stamper.addAnnotation(field, 1);
Note that this line doesn't make sense:
stamper.getAcroFields().setField("partner", "Click here to show partner's web site");
Why would you create a field first (and possibly add a caption) and then change it immediately afterwards? Why not create the field correctly from the start?
Finally, it seems that you want to create a button that can be clicked by people. Then why are you creating a text field? Wouldn't it make more sense to create a push button?
This is an example of a question to which a machine would respond: Too many errors... Maybe you should consider reading the documentation before trying to fix your code.

Categories