Create Action on Page Open Add submit button in PDF using Java via Aspose.PDF

I am trying to find java examples on how to add to a PDF document I have created in Aspose to do the following:

  1. On PAGE Open Trigger
    execute View->Zoom->Actual size
  2. On PAGE Open Trigger
    execute View->Page Display -> Single Page View

I can’t seem to determine how I would accomplish this in my java code when generating my PDF. I have many pages in my document so how would I apply this trigger to all my pages. I am using Aspose Total for Java 18.9

Thanks In Advanced
Tom

@apose_user_tnr

Thanks for contacting support.

You may use GoToAction object that allows you to set the zoom value associated with a PDF file. Please check Get-Set Zoom Factor article in API documentation for sample code snippet. In case you face any issue, please feel free to let us know by sharing a sample PDF document. We will test the scenario in our environment and address it accordingly.

Thanks for your reply. I have seen and use that functionality. However I am looking for the capability “to assign an on page event to execute a menu command”. This will be invoked when the use click on a page of the PDF. You can do this through the Adobe PDF product. Hopefully, I have explained what I need. It is like running a macro when you click on each PDF page.

Thanks,
Tom

@apose_user_tnr

Thanks for writing back.

Would you please share a sample PDF document which you want to generate using Aspose.PDF. We will try to create similar with Aspose.PDF API and share our findings with you.

This can be any PDF. I just don’t know context of the JAVA api to use. If I create a PDF that has this in it would that be what you want ?

Tom

@apose_user_tnr

We requested you to share your desired PDF document in case you have created one using Adobe Reader. We will try to create similar PDF with Aspose.PDF API and share our findings with you.

Here is the functionality I would like to be able to replicate using Aspose API for java. Please inspect the page properties of the enclosed PDF. On Page Open Trigger menu item commands are executed. Can I simulate this functionality using Aspose for Java for all my pages.

Thanks,
TomAspose_document.pdf (48.6 KB)

@apose_user_tnr

Thanks for providing sample PDF document.

Your requirements can be achieved by setting JavaScript Actions on Page events inside PDF. Aspose.PDF for Java offers setOnOpen() method for PDF Page in order to set JavaScript Action. For an instance, we have modified your PDF using following code and set zoom level to 200% using JavaScript:

Document doc = new Document(dataDir + "Aspose_document.pdf");
doc.getPages().get_Item(1).getActions().setOnOpen(new JavascriptAction("this.zoom = 200"));
doc.save(dataDir + "Saved.pdf");

Saved.pdf (54.0 KB)

You may also set Actions on document level as per your requirements. In case you need any further assistance, please feel free to let us know.

Hi,

How would I be able to perform action “SubmitFormAction.SUBMIT_PDF”, on PDF button’s click event ?

I am looking for a way for a while.

Thanks

@darshpatel

Please check “Set Submit Button URL and JavaScript for Push Button” article in API documentation.

Hi Asad,

Thanks for the reply.

I already went through above article and that is not the requirement for us.

The requirement is “send the whole pdf to web service end point”. In other words, once user clicks the submit button on PDF, PDF sends the stream of PDF to end point as per defined URL.

It seems to me that action “SubmitFormAction.SUBMIT_PDF” will do the trick but not sure how to set action to that. So please provide me example of this one please.

On top of this, I am looking for a example in Java.

If you need any more clarification, please let me know.

Thanks

@darshpatel

Thanks for getting back to us.

As per our understandings, you want to create a PDF with a button upon which press, you want to send the entire PDF to a web service (Not only data but entire PDF). Please note that Aspose.PDF offers the feature to fill the form and as far as defining JavaScript actions for form fields are concerned, it can also be achieved via Aspose.PDF.

Would you please clarify more if you want to achieve something like this: Publishing interactive PDF web forms OR you only need to add a JavaScript action behind Button which will send PDF Form Data to some URL. We will further share our feedback with you accordingly.

PS: It would also be helpful if you can please share some sample PDF containing similar functionality which you want to achieve using our API.

@asad.ali
Thanks for replying quickly.

“As per our understandings, you want to create a PDF with a button upon which press, you want to send the entire PDF to a web service ( Not only data but entire PDF ).”

That is what we want, the reason behind that we need the physical file on server with filled data on PDF.

https://helpx.adobe.com/acrobat/using/publishing-interactive-pdf-web-forms.html
If you go to the above link, you will find an option with " Submit Form Selections options - PDF The Complete Document", that is what we want to achieve while PDF generation with ASPOSE java.

Unfortunately, I am unable to share the PDF as it has confidential data in it, but you understood the requirement spot on.

Just to make it more clear, I have attached a png of Button and when hover on it, it says “send data to ‘url’”. On that click, we have Action of SUBMIT_PDF, which sends the entire PDF to provided web service.

Thanks.
Darshit Patelsubmit_pdf_hover.png (16.3 KB)

@darshpatel

Thanks for your response.

You can add a submit button in PDF using Aspose.PDF for Java and define its submit flag as XFDF and PDF as per your requirements. Please consider using following code snippet:

Document doc = new Document(dataDir + "inputForm.pdf");
com.aspose.pdf.facades.FormEditor add = new com.aspose.pdf.facades.FormEditor(doc);
add.addSubmitBtn("btnSubmit", 1, "Submit", "mailto:myemail@mail.com?subject=My Subject&body=Here is my form data.", 400, 20, 500, 50);
add.setFieldAppearance("btnSubmit", AnnotationFlags.Print);
add.setSubmitFlag("btnSubmit", com.aspose.pdf.facades.SubmitFormFlag.Pdf);
doc.save(dataDir + "AddTextBox.pdf");

In the above code snippet, we used an email address to send PDF to. You may replace it with the URL where you want to send the PDF document. In case you need further assistance, please feel free to let us know.

@asad.ali

Thanks for snippet, that worked for me.

Thanks again for your help.

@darshpatel

Thanks for your kind feedback.

Please keep using our API and in case you face any issue, please feel free to let us know.

Hello @asad.ali,

I need the same functionality as given below but Button click event is not working with Chrome Browser.

I am using .Net to save the PDF form to server when Submit button is clicked on PDF form using Chrome browser.

Can you please help me for the same?

FYI.

Document doc = new Document(dataDir + “inputForm.pdf”);
com.aspose.pdf.facades.FormEditor add = new com.aspose.pdf.facades.FormEditor(doc);
add.addSubmitBtn(“btnSubmit”, 1, “Submit”, "service/savePDF, 400, 20, 500, 50);
add.setFieldAppearance(“btnSubmit”, AnnotationFlags.Print);
add.setSubmitFlag(“btnSubmit”, com.aspose.pdf.facades.SubmitFormFlag.Pdf);
doc.save(dataDir + “AddTextBox.pdf”);

Thanks,
Lalmuni

@Lalmuni.Singh

Would you please share if the submit button is working correctly in Adobe Reader? Please share the output PDF with us for our reference so that we can further investigate the scenario and share our feedback with you accordingly.