We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Add JavaScript in PDF document using Aspose.PDF for Java - Make HTTP call when open PDF

Hi,

I want to make an Http request when Pdf open (or On some event) and handle the response of the request, based on the response we will take some decision on pdf (hide/show). If the Internet is not working, then I will show a layer,

Thanks

@sbl1234

You may please specify a JavaScript action on OpenAction of the document to make HTTP request. Please consider using following code snippet:

Document doc = new Document(dataDir + "SampleText.pdf");
doc.OpenAction = new JavascriptAction("{JavaScript Action}");
doc.Save(dataDir + "Output.pdf");

@asad.ali, thanks for reply,

I have tried to send a http request through soap. The code is below

String docPath = "/home/deepak/Downloads/SSE.pdf";
Document doc = new Document(docPath);
JavascriptAction fieldJavaScript=new JavascriptAction("try{ SOAP.connect('http://payment.com');}catch(e){app.alert(e);}");
doc.setOpenAction(fieldJavaScript);
doc.save("/home/deepak/Downloads/ValueFirst_new.pdf");

But it’s returning exception on SOAP.connect(‘http://payment.com’);
NotAllowedError: Security settings prevent access to this property or method.

And I also try to make http call using “Net.HTTP.request”, it is also return exception
NotAllowedError: Security settings prevent access to this property or method.

String docPath = "/home/deepak/Downloads/SSE.pdf";
Document doc = new Document(docPath);
JavascriptAction fieldJavaScript=new JavascriptAction("var params ={"
        + " cVerb: 'MKCOL',"
        + " cURL: cURL,"
        + " oHandler:{ "
        + "     response: function(msg, uri, e) {"
        + "         app.alert('1');       "
        + "     }"
        + " }"
        + "};"
        + "Net.HTTP.request(params);");
doc.setOpenAction(fieldJavaScript);
doc.save("/home/deepak/Downloads/ValueFirst_new.pdf");

@sbl1234

Would you please share the sample PDF document as well. We will test the scenario in our environment and address it accordingly.

I am  sharing code and file..

SSE.pdf (154.2 KB)

package asposepoc;

import com.aspose.pdf.ButtonField;
import com.aspose.pdf.Color;
import com.aspose.pdf.Document;
import com.aspose.pdf.JavascriptAction;
import com.aspose.pdf.Rectangle;
import com.aspose.pdf.TextBoxField;

public class AposePDFJavaScript {

  public static void main(String[] args) {
    
    String docPath = "/home/deepak/Downloads/SSE.pdf";
    Document doc = new Document(docPath);
/////////Using this text box for printing error -- temporary ///////////////////////////
    TextBoxField textBoxField1 = new TextBoxField(doc.getPages().get_Item(2), 
        new Rectangle(0, 0, doc.getPages().get_Item(1).getRect().getHeight(), doc.getPages().get_Item(1).getRect().getWidth()));
   
    // Set the field name
    textBoxField1.setPartialName("textbox11");
    
    // Set the field value
    textBoxField1.setValue("show resume");
    textBoxField1.setColor(Color.getBlack());
    textBoxField1.setHeight(doc.getPages().get_Item(1).getRect().getHeight());
    textBoxField1.setWidth(doc.getPages().get_Item(1).getRect().getWidth());
    
 

    
    
    
    doc.getForm().add(textBoxField1);
    
    
    JavascriptAction fieldJavaScript=new JavascriptAction("app.alert('ddddd');var params ={"
            + " cVerb: 'POST',"
            + " cURL: 'https://www.naukri.com',"
            + " oHandler:{ "
            + "     response: function(msg, uri, e) {"
            + "         app.alert('1');       "
            + "     }"
            + " }"
            + "};"
            + "try{app.alert('11');Net.HTTP.request(params);}catch(e){this.getField('textbox11').value=e;}");
    doc.setOpenAction(fieldJavaScript);
    doc.save("/home/deepak/Downloads/ValueFirst_new.pdf");

@sbl1234

We have tested the scenario in our environment and found that the issue is not related to Aspose.PDF API but with Adobe Reader specifications. Because of updated secturity, adobe reader does not allow unsafe code execution. You may further get help on Adobe Reader official forums in this regard and write trusted function to perform the required functionality. Following are some useful topics which you can visit: