Read javascript from pdf using iTextSharp

半世苍凉 提交于 2019-12-12 05:45:22

问题


I have a script that runs after the pdf-file has been loaded and that populates some form fields in the pdf. I assume it is some kind of javascript running behind the scene. In the javascript code some values are stored that I need to retrieve. I use iTextSharp to work with the pdf-file. Is it possible to read the javascript code or the values so I can work with them in my c# code somehow?


回答1:


Modified from this SO answer:

var pdfReader = new PdfReader(infilename);
using (MemoryStream memoryStream = new MemoryStream())
{
    PdfStamper stamper = new PdfStamper(pdfReader, memoryStream);
    for (int i = 0; i <= pdfReader.XrefSize; i++)
    {
        PdfDictionary pd = pdfReader.GetPdfObject(i) as PdfDictionary;
        if (pd != null)
        {
            PdfObject poAA = pd.Get(PdfName.AA); //Gets automatic execution objects
            PdfObject poJS = pd.Get(PdfName.JS); // Gets javascript objects
            PdfObject poJavaScript = pd.Get(PdfName.JAVASCRIPT); // Gets other javascript objects
            //use poJS.GetBytes(), poJS.ToString() etc to inspect details...
        }
    }
    stamper.Close();
    pdfReader.Close();
    File.WriteAllBytes(rawfile, memoryStream.ToArray());
}

Here's a reference page for the PdfObject class.



来源:https://stackoverflow.com/questions/20243241/read-javascript-from-pdf-using-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!