VintaSoft PDF .NET Plug-in Discussions

Questions, comments and suggestions concerning VintaSoft PDF .NET Plug-in.

Board index < VintaSoft Imaging < VintaSoft PDF .NET Plug-in Discussions

We are migrating to new forums engine, no new registration or posting currently available. TIA for your patience.

JavaScript Token Replacement Question



JavaScript Token Replacement Question

Post by IntegraMike »

I'm mostly just wondering if this is possible or how much work it would be to make it possible. We have some well known tokens in the application and it would be really nice if we could somehow add the replacement code to them in the JavaScript executor so that no matter where we evaluate JavaScript those tokens are replaced with our values. Is there a way to do this?

Ex:
Token: UserName
JavaScript = printd(UserName);

I'd like the JavaScript executor to replace that with:
printd("Mike");
before it executes the JavaScript, and then execute the JavaScript.

Thanks!


Re: JavaScript Token Replacement Question

Post by Alex »

Hello Mike,

Can you change PDF document? Do you need solution that will work only in your application or will work in any PDF viewer?

If you can change PDF document, you can add an open JavaScript action to a PDF document. Here is code snippet:
document.OpenAction = new PdfJavaScriptAction(document, "UserName=\"Mike\";");
This solution will work for any PDF viewer.


If you cannot change PDF document and you need to open PDF document in your application only, you can create JavaScript code and run it in your application. For solving the task in PdfEditorDemo project you need to add the ExecuteApplicationJavaScripts method to the WinFormsPdfJavaScriptActionExecutor class ("[InstallPath]\VintaSoft\Imaging .NET v9.1\Examples\WinForms\CSharp\PdfEditorDemo\DemosCommonCode.Pdf\JavaScript\WinFormsPdfJavaScriptActionExecutor.cs" file).
Here is code of ExecuteApplicationJavaScripts method:
protected override void ExecuteApplicationJavaScripts(Vintasoft.Imaging.Pdf.JavaScript.PdfJavaScriptEngine engine)
{
    base.ExecuteApplicationJavaScripts(engine);
            
    var ms = new System.IO.MemoryStream();
    var writer = new System.IO.StreamWriter(ms);
    writer.WriteLine("UserName = \"Mike\";");
    writer.Flush();
    ms.Position = 0;
            
    ExecuteApplicationJavaScript(engine, "MyInitalScript", ms);
}
This solution will work only in application, which uses VintaSoft Imaging .NET SDK.

Best regards, Alexander


Re: JavaScript Token Replacement Question

Post by IntegraMike »

I think the second option will do what we need or get me close enough to it, thanks!

For clarification, the goal is to have to JavaScript on the document, but not change the document. When the JavaScript executes we want to only in our application change the values for some well known names to be replaced with values in our application.

I'll let you know once I've had a chance to try it out!


Page 1 from 1: 1