JavaScript Token Replacement Question

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

Moderator: Alex

Post Reply
IntegraMike
Posts: 56
Joined: Mon Dec 02, 2019 11:19 pm

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!
Alex
Site Admin
Posts: 2305
Joined: Thu Jul 10, 2008 2:21 pm

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:

Code: Select all

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:

Code: Select all

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
IntegraMike
Posts: 56
Joined: Mon Dec 02, 2019 11:19 pm

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!
Post Reply