Search code examples
pdfitextuitextfielditext7

IText7 How to call custom method on click of HyperLink


In Itext7, how to call my custom method helloWorld() on click of HyperLink ? Is it even possible to do in Itext ? I know we can open a url or run javascript

PdfAction action = PdfAction.createURI("https: // www.google.com/");

PdfAction action1 = PdfAction.createJavaScript("app.alert('This is JS alert on button click');");

but how to i similarly call my java function ?


Solution

  • but how to i similarly call my java function ?

    You can not.

    Your Java code is executed while you create the PDF file. But the action of the "click of HyperLink" is executed when someone views the PDF file you created. At the time of viewing the PDF (and clicking links) there isn't a Java environment to run your Java function in; even if you use a Java viewer, it usually does not allow executing in-document Java code...

    Of course you can set up a Java web service and have your link in the PDF trigger some JavaScript which in turn calls that web service. But that task essentially is unrelated to iText, it requires web application building and JavaScripting skills.