Problems with ButtonField

Hi,
I am trying to add ButtonField. I have two problems.

First I can not set text with non english characters (e.g. žšč). I thought it is a problem with font, but when i used the PageInfo to set different font it did not help. Also the text with such characters (TextFragments) rendered just fine. What am I doing wrong?

ButtonField buttonField = new ButtonField(page, rectangle);

//Font font = FontRepository.findFont("Arial");
//buttonField.setDefaultAppearance(new DefaultAppearance(font, 10, java.awt.Color.black));

buttonField.setNormalCaption("Uložit");
page.getDocument().getForm().add(buttonField, page.getNumber());
return buttonField;
The second problem is that I can not find a way how to attach some javascript code. PdfContentEditor.createJavaScriptLink is probably useless for buttons. What class methods need I have to use?

Thanks



Hi,


Thank you for contacting support. We are working over your query and will get back to you soon.

Hi,


Thank you for being patient. Kindly send us the complete details of your first test case, including input PDF document, code and the version number of Aspose.Pdf for Java API. It will help us to investigate the root cause, and we will share our findings with you. Your response is awaited.

For your second problem, please use following code example:

[Java]
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// Open document<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
PdfContentEditor contentEditor = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> PdfContentEditor();<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
contentEditor.bindPdf(“C:\Pdf\test29\EmptyPDF.pdf”); <span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
java.awt.Rectangle rectangle = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> java.awt.Rectangle(100, 100, 200, 200);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// Create application link<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>contentEditor.createJavaScriptLink(“app.alert(<span class=“str” style=“color: rgb(0, 96, 128); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>‘Welcome to Aspose!’<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>);”, rectangle, 1, java.awt.Color.RED);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// Save updated PDF<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
contentEditor.save(“C:\Pdf\test29\Output.pdf”);
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>

Here is the complete code:

@Test
public void buttonTest() throws Exception {
com.aspose.pdf.License license = new com.aspose.pdf.License();
license.setLicense(“data/Aspose.Pdf.lic”);
Document pdfDocument = new Document();

Page page = pdfDocument.getPages().add();

ButtonField buttonField = new ButtonField(page, new Rectangle(50, 600, 250, 650));

//Here I tried to set font, but it throws nullpointer exception on!!
// java.lang.NullPointerException
// at com.aspose.pdf.Form.m3(Unknown Source)
// at com.aspose.pdf.WidgetAnnotation.m1(Unknown Source)
// at com.aspose.pdf.WidgetAnnotation.setDefaultAppearance(Unknown Source)
// at cz.sukl.test.AsposeTest.buttonTest(AsposeTest.java:455)

//Font font = FontRepository.findFont(“Arial”);
[//buttonField.setDefaultAppearance](https://buttonfield.setdefaultappearance/)(new DefaultAppearance(font, 10, java.awt.Color.black));

[//buttonField.setName](https://buttonfield.setname/)(“btnSave”);
buttonField.setValue(“Uložit pro odeslání do DS”);
//OR? buttonField.setNormalCaption(“Uložit”);

page.getDocument().getForm().add(buttonField);

pdfDocument.save("./testdata/buttonTest.pdf");

}

Please note few things:

  • I tried to set font, but exception is thrown

  • Not sure if setValue or setNormalCaption should be used to set the caption, but the result is the same

  • I have attached the resulting pdf

I am using 17.2 SDK

To your proposed solution … this is what I have now, but I want to set the javascript action on a button (ButtonField). Or am I supposed to use the rectangle over the button a thus simulate the action on the button?

Hi,


Thank you for the details. We managed to replicate the said problems using the latest version 17.4 of Aspose.Pdf for Java API and have logged tickets in our bug tracking system as below:

PDFJAVA-36735: Incorrect display of non-English characters in the caption of a button
PDFJAVA-36736: ButtonField.setDefaultAppearance method shows an error on setting font

We have linked your post to these tickets and will keep you informed regarding any available updates. We are sorry for the inconvenience caused.

opeterka:
To your proposed solution … this is what I have now, but I want to set the javascript action on a button (ButtonField). Or am I supposed to use the rectangle over the button a thus simulate the action on the button?
Please use the following code to apply Javascript action on a button. We have attached an output PDF to this reply.
[Java]
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>Document doc = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> Document();<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// add page to pages collection of PDF document<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
doc.getPages().add();<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// create Button Field instance<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
ButtonField button = <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> ButtonField(doc.getPages().get_Item(1), <span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”> Rectangle(50, 600, 250, 650));<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
button.setName(“Button 1”);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// caption of button<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
button.setValue(“Button 1”);<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>button.setOnActivated(<span class=“kwrd” style=“color: rgb(0, 0, 255); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>new<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”> JavascriptAction("<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>/* test script */<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”> app.alert(<span class=“str” style=“color: rgb(0, 96, 128); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>‘Hello World!’<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>);"));<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>
<span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// add button to forms collection of PDF file<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>doc.getForm().add(button);<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span class=“rem” style=“color: rgb(0, 128, 0); font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”>// save PDF document<br style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre;”><span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>doc.save(“C:\Pdf\test29\Out_17.4_Java.pdf”);
<span style=“font-family: “Courier New”, Consolas, Courier, monospace; font-size: small; white-space: pre; background-color: rgb(255, 255, 255);”>