Below is an example of how to use PDFDoc.RunJS, A premium license is required for this method.
private async System.Threading.Tasks.Task testJsAsync()
{
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile srcFile = await localFolder.GetFileAsync("Test.pdf");
IRandomAccessStream stream = await srcFile.OpenAsync(FileAccessMode.ReadWrite);
if(stream != null)
{
String js = "app.alert({cMsg:\"Hello World\", cTitle:\"Testing\", nIcon: 1, nType: 1});";
applyJs(stream, js);
//test set author and save PDF file.
js = "console.println(\"Author before set:\" + this.author);this.author='radaee';console.println(\"Author after set:\" + this.author);";
applyJs(stream, js);
//test add and set annotation.
js = "var sqannot = this.addAnnot({type: \"Square\", rect: [10, 10, 110, 110], page: 1, width: 10, strokeColor: [\"RGB\", 0, 0, 1]});console.println(\"fill color before set:\" + sqannot.fillColor);sqannot.fillColor=[\"RGB\", 1, 0, 0];console.println(\"fill color after set:\" + sqannot.fillColor);console.println(\"page no is:\" + sqannot.page);sqannot.page = 2;console.println(\"page set to:\" + sqannot.page);console.println(\"rect before set:\" + sqannot.rect);sqannot.rect=[50, 50, 150, 150];console.println(\"rect after set:\" + sqannot.rect);";
applyJs(stream, js);
//test bookmarks (assuming the pdf already contains bookmarks)
js = "console.println(\"bookmark label:\" + this.bookmarkRoot.children[0].name);this.bookmarkRoot.children[0].name='123';console.println(\"after set:\" + this.bookmarkRoot.children[0].name);";
applyJs(stream, js);
//test Exception catch
js = "try{app.alert();}catch(e){console.println(e);}";
applyJs(stream, js);
//test Uncaught Exception.
js = "console.print('test');";
applyJs(stream, js);
}
}
private void applyJs(IRandomAccessStream stream, String js)
{
PDFDoc doc = new PDFDoc();
if (doc.Open(stream, "") == PDF_ERROR.err_ok) //no password
{
try
{
doc.RunJS(js, new JSDelegate());
doc.Save();
}
catch (Exception e)
{
Debug.WriteLine("Exception: " + e.Message);
}
String val = doc.GetMeta("Author");
}
doc.Close();
}
class JSDelegate : PDFJSDelegate
{
public void OnConsole(int cmd, string para)
{
switch (cmd)
{
case 0://clear
Debug.WriteLine("Clear");
break;
case 1://hide
Debug.WriteLine("Hide");
break;
case 2://println
{
Debug.WriteLine(para + "\r\n");
}
break;
case 3://show
Debug.WriteLine("Show");
break;
}
}
public int OnAlert(int btn, string msg, string title)
{
Debug.WriteLine("Alert {title:\"" + title + "\",message:\"" + msg + "\",button:" + btn + ",return:1}\r\n");
return 1;
}
public bool OnDocClose()
{
return true;
}
public string OnTmpFile()
{
return "";
}
public void OnUncaughtException(int code, string msg)
{
Debug.WriteLine(msg + "\r\n");
}
}
RadaeePDF SDK for Windows 10
Created : 2017-12-19 09:32:10, Last Modified : 2017-12-19 09:41:27