I am using Itext to create a pdf and I cannot get the checkbox to uncheck. Here is my code:
RadioCheckField bt = new RadioCheckField(writer, new Rectangle(300, 300, 400, 400),
"check1", "Yes");
bt.setCheckType(RadioCheckField.TYPE_CHECK);
bt.setBorderWidth(BaseField.BORDER_WIDTH_THICK);
bt.setBorderColor(BaseColor.BLACK);
bt.setBackgroundColor(BaseColor.WHITE);
bt.setChecked(false);
PdfFormField ck = bt.getCheckField();
writer.addAnnotation(ck);
You can see that the bt.setChecked(false) is in the code, but the checkbox is still checked. I looked at the docs and it seems to me that it is supposed to work this way. What do I not understand?
First this:
You've posted the same question on Nabble which is not a site endorsed by iText. See http://lowagie.com/nabble for more info.
As you were not subscribed to the official mailing-list, I had to manually approve your question. Now I see that you posted the same question here. Cross-posting is usually not appreciated in a community.
As for your question, I've answered it here: http://article.gmane.org/gmane.comp.java.lib.itext.general/65407
Bottom line: I made a Short, Self Contained, Correct (Compilable), Example based on your code and I executed it. I couldn't reproduce the problem you reported. Maybe there is no problem. Maybe there was a problem in a previous version of iText that has now been fixed.
I also read that you shipped your code with the text color set to white. I don't understand: that doesn't make sense! Your PDF will have an interactive field, but people will never be able to see whether or not it's checked...
If you don't care, if all you wanted is to show a checkbox, then using an interactive field is overkill. You should have used a check box character, for instance from the ZapfDingbats font.
If you're using the AGPL version of iText, please show me the URL where I can find your code (as you know, the AGPL requires code using AGPL software to be distributed as AGPL too).
Try the following way, for me it is working:
public void addRadioGroup() throws Exception{
if(!this.doc.isOpen()){
this.doc.open();
}
PdfFormField radioGroup = PdfFormField.createRadioButton(this.writer, false);
radioGroup.setFieldName("numbers");
for(int i=0;i<3;i++){
Rectangle rect = new Rectangle(130+(40*i), 430, 160+(40*i), 455);
this.addRadioButtonKid(radioGroup, rect,String.valueOf(i));
}
this.writer.addAnnotation(radioGroup);
}
private void addRadioButtonKid(PdfFormField radio, Rectangle rect, String onValue) throws Exception{
RadioCheckField bt = new RadioCheckField(this.writer, rect, null, onValue);
bt.setBorderWidth(BaseField.BORDER_WIDTH_THICK);
bt.setBorderColor(Color.BLACK);
bt.setBackgroundColor(Color.WHITE);
bt.setCheckType(RadioCheckField.TYPE_CROSS);
bt.setChecked(false);
PdfFormField ck = bt.getCheckField();
ck.setPlaceInPage(1);
radio.addKid(ck);
}
The only problem I had was that the default "check style" wasn't changed. A user reported this problem back in 2011 on the mailinglist in 2011. If you need another style patch iText for yourself or use the workaround described by Mark.
Update: After 2 years they seem to have fixed the problem in the latest iText version 5.4.3 (cp. the change of Michaël Demey)
try using a checkbox instead of a radiobutton since you only have one
Related
I'm trying to write code to automatically DM people on Instagram. Im stuck on getting the code to click on the DM button.
In UIautomatorviewer, there is no text or a resource-id, so I tried using the class.
When I run the code it doesn't click on the right thing. It clicks on the button NEXT to it.
Here is the code -
By path2 = By.xpath("//android.widget.ImageView[#index='3']");
driver.findElement(path2).click();
Thread.sleep(5000);
Can anyone help? I'm new to this so i'm not very experienced.
Because I can not see the entire XML I can not really tell what you did wrong but probably your xpath is not correct.
With UIAutomatorViewer you can save the XML and then you can test your xpath on it. Either with XMLSpy or an online tool like https://www.freeformatter.com/xpath-tester.html.
Probably there are more android.widget.ImageView with index = 3 and I think appium selects the first one? So you could change your xpath to a more unique one like:
//android.widget.FrameLayout//android.widget.LinearLayout//android.widget.ImageView[#index='3']"
Based on UIAutomator Viewer screen you can also use content-desc as shown below
By path2 = By.xpath("//android.widget.ImageView[contains(#content-desc,'Message')]");
driver.findElement(path2).click();
I´ve already searched for related posts on stack, but didn´t find quite the right answer;
Im using THIS to display a .pdf file in my frame.
Now I want to use the JWebBrowser.navigate()+ (filePath + "#search=anyString") to search this .pdf file for the specific string.
Unfortunately I´m unable to reload the JWebBrowser afterwards. So the correct filepath is submitted (checked that in the pdf adress bar), but the JWebBrowser turns gray and nothing happens.
When using the navigate() to load another file and afterwards navigate to the old file again, it works just fine.
I tried revalidate(), repaint() and stuff like this but I cant get this to work.
Example:
btnTest.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
browser.navigate(filePath + "#search=flower");
browser.revalidate();
browser.repaint();
}
});
I appreciate any kind of advice!
Thanks!
meanwhile I've found a solution, although it might be not the perfect one:
You can just remove the JWebbrowser object from your current Frame/Panel and just add it again.
This way it will work just fine.
Just in case someone is having a related problem to this one.
Greetz
By using PDFBox, it is easy to create a link that goes a particular page or page view by using PDPageDestination. For example, the following code will make a link that goes to page 9:
PDAnnotationLink link = new PDAnnotationLink();
PDPageDestination destination = new PDPageFitWidthDestination();
PDActionGoTo action = new PDActionGoTo();
destination.setPage(document.getPage(9));
action.setDestination(destination);
link.setAction(action);
Problem:
Instead of going to a particular page, I would like to go to the previous view.
For example, suppose in a PDF file, each of P.1 and P.2 has a link that goes to P. 9. Now I would like to put on P. 9 a link that goes back to where the user started.
If the user started out at P.1 and clicked the link to P.9, he arrives at P.9. When he clicks the link on P. 9, he will go back to P.1, where he came from. But If he started out at P.2, then the link at P.9 will go back to P.2 instead.
Question: How do I achieve this with PDFBox?
FYI, with Adobe Acrobat, this can be achieved by adding an "execute a menu item" action to a link, and then choosing "Previous View" as the menu item, as shown in this screenshot:
Link to Acrobat screenshot
With the guidance of Tilman, I managed to solve my own problem.
I cannot find a PDAction subclass that gives me the capability to add a "named action", so I created my own subclass, "PDActionNamed":
class PDActionNamed extends PDAction {
public static final String SUB_TYPE = "Named";
public PDActionNamed() {
super();
setSubType( SUB_TYPE );
}
public void setN( String s ) {
action.setName( "N", s );
}
}
To use the subclass,
PDAnnotationLink link = new PDAnnotationLink();
PDActionNamed action = new PDActionNamed ();
action.setN("GoBack"); // this is one of Acrobat's default named action
link.setAction(action);
It seems to work even on non-Javascript-supported PDF readers (e.g. SumatraPDF).
What you're talking about is a viewer dependant action; I don't think there is a way to do this generically but there should be ways to do this in Adobe Acrobat / Adobe Reader.
One such way is to insert a link that triggers an Action. The action could be a Javascript action and the Javascript could be relatively simple as the Acrobat Javascript API contains an "app" method called "goBack".
So, insert a link as you're doing right now. Insert not a GoTo action but a Javascript action. And set the Javascript to: "app.goBack()".
This should work in Acrobat (they have a similar example with a button form field in the Acrobat Javascript API reference. The question is whether it will work in other viewers as well and thus whether it will fulfil your business case.
I'm trying to resize an existing PDF button. I want to amend the label from "Print" to "Print Amended".
PushbuttonField button = form.getNewPushbuttonFromField("HoldButton");
Rectangle box = button.getBox();
box.setRight(box.getRight() + 72); // Increase width by 1"
button.setBox(box);
button.setText("Print Amended");
form.replacePushbuttonField("HoldButton", button.getField());
The above code successfully changes the label, but not the size. The end result is a button with no change in width, and the label "Print Amended" squished together.
Is it possible to resize an existing button in iText?
I tried your example and I was surprised that I could reproduce your problem.
I looked into the iText code and I see that it is explicitly forbidden to change the /T value. This makes sense: if you want to replace an existing button, you don't want to change its name.
However, for some reason we also explicitly forbid changing the /Rect value. See the code of the AcroFields class:
for (Object element : button.getKeys()) {
PdfName key = (PdfName)element;
if (key.equals(PdfName.T) || key.equals(PdfName.RECT))
continue;
if (key.equals(PdfName.FF))
values.put(key, button.get(key));
else
widgets.put(key, button.get(key));
merged.put(key, button.get(key));
markUsed(values);
markUsed(widgets);
}
I am not sure why we made this decision when we wrote this code. If I remove || key.equals(PdfName.RECT), then your code works as expected.
As we deliberately excluded changing the dimensions of the button, I am in doubt if this is a bug or if we intentionally added that code there. Reading your requirement, I am inclined to remove || key.equals(PdfName.RECT) from the official source code.
PS: I know that this doesn't answer your question, but it does explain why your code doesn't work in spite of the fact that it looks perfectly OK. As I explained: I'm really surprised that it doesn't work, because I'm responsible for the iText code...
PS 2: I've changed the code in the official trunk.
Try something like:
newButton1 = new JButton("Print Amended") {
{
setSize(150, 75);
setMaximumSize(getSize());
}
};
or:
Try to use setMaximumSize() method
button.setMaximumSize(new Dimension(100,100));
Hi all this is my first Question here!
Im just making my first steps with (Ext-) GWT. I´m testing the Ext-GWT libraries and really: These are absolute great!
Now my question:
Is it possible to make a kind of "clear-Portal" or "hide all portles" for a defined Portal?
Or have i always manually clear the portal like in my example code above?
My sample code looks like this:
//define the Portal, 2 columns, each 50% auf width, with borders and Backgroundcolor
portal = new Portal(2);
portal.setBorders(true);
portal.setStyleAttribute("backgroundColor", "white");
portal.setColumnWidth(0, .50);
portal.setColumnWidth(1, .50);
//define a Portlet for showing all Users
portletUser = new Portlet();
portletUser.setHeading("Benutzer");
configPanel(portletUser);
portletUser.setLayout(new FitLayout());
CompUserList compUserList = new CompUserList();
portletUser.add(compUserList);
portletUser.setHeight(250);
//define a Portlet for showing all Vehicles
portletVehicles = new Portlet();
portletVehicles.setHeading("Fahrzeuge");
configPanel(portletVehicles);
portletVehicles.setLayout(new FitLayout());
CompVehicleList compVehicleList = new CompVehicleList();
portletVehicles.add(compVehicleList);
portletVehicles.setHeight(250);
//define a portlet for showing all countries
portletCountries = new Portlet();
portletCountries.setHeading("Länder");
configPanel(portletCountries);
portletCountries.setLayout(new FitLayout());
CompCountryList compCountryList = new CompCountryList();
portletCountries.add(compCountryList);
portletCountries.setHeight(250);
//add both Portlets to Portal
portal.add(portletUser, 0);
portal.add(portletVehicles, 1);
So first of all this works fine and looks great :-)
Now i have a a button in a accordeon menu. The Listener on this button should hide all portlets in the portal (at this time its the portletUser and portletVehicles) and then add another portlet (for example the portletCountries):
portletUser.hide();
portletVehicles.hide();
portal.add(portletCountries, 0)
Question from above again ;-)
Is it possible to make a kind of "clear-Portal" or "hide all portles" for a defined Portal?
Or have i always manually clear the portal like in my example code above?
What is the best practice for this functionallity?
Thanks all for your tips!
Lars.
I haven't used Ext-GWT -- but looking at the Javadoc for Portal there are two things I would try:
for (LayoutContainer c : portal.getItems()) {
c.hide();
}
or, more generally, wrap a Portal in your own class which records the Portlets which are in the Portal -- then you can get a List rather than List.