使用itext7c将Javascript添加到PDF文件#

使用itext7c将Javascript添加到PDF文件#,javascript,c#,asp.net-mvc,pdf,itext7,Javascript,C#,Asp.net Mvc,Pdf,Itext7,我正在尝试使用iText7库和C将javascript添加到我的PDF文件中# 目前,这是我的代码…到目前为止还没有完成 public FileResult Download(string id) { var fileSelect = _context.FileStores.SingleOrDefault(c => c.File_Id == id); string base64string = Convert.ToBase64Str

我正在尝试使用iText7库和C将javascript添加到我的PDF文件中#

目前,这是我的代码…到目前为止还没有完成

public FileResult Download(string id)
    {
        var fileSelect = _context.FileStores.SingleOrDefault(c => c.File_Id == id);
        
        string base64string = Convert.ToBase64String(fileSelect.File_Content, 0, fileSelect.File_Content.Length);


        using (MemoryStream stream = new System.IO.MemoryStream())
        {

            MemoryStream memory = new MemoryStream(fileSelect.File_Content);
            BinaryReader BRreader = new BinaryReader(memory);
            StringBuilder text = new StringBuilder();


            PdfReader reader = new PdfReader(memory);
            //FileStream output = new FileStream(@"Manual.pdf", FileMode.Create);

            PdfDocument Pdfdoc = new PdfDocument(reader);
            Document doc = new Document(Pdfdoc);
            PdfAction action = PdfAction.CreateJavaScript("var rightNow = new Date(); " +
                                                          "var endDate = new Date('May 03, 2021 10:00:00');" +
                                                          "if(rightNow.getTime() > endDate){" +
                                                          "app.alert('This Document has expired, please contact us for a new one');" +
                                                          "this.closeDoc();}");
            reader.Close();

            return File(memory, "application/pdf", "ExportData.pdf");
        }

我想将此javascript添加到我的PDF中,并在添加完javascript后下载该文件。有人知道如何将Javascript添加到pdf中吗?谢谢

您可以将Javascript片段添加为文档级OpenAction,在打开文档时执行:

PdfReader=newpdfReader(“input.pdf”);
PdfWriter writer=新的PdfWriter(“output.pdf”);
PdfDocument Pdfdoc=新PdfDocument(读写器);
PdAction=PdAction.CreateJavaScript(
“var rightNow=新日期();”+
var endDate=新日期('2021年5月3日10:00:00')+
“如果(rightNow.getTime()>endDate){”+
“app.alert('此文档已过期,请与我们联系以获取新文档')+
“this.closeDoc();}”
);
Pdfdoc.getCatalog().SetOpenAction(操作);
Pdfdoc.Close();

顺便说一句:你知道这样一个脚本是很容易绕过的吗?是的@mkl,我正在尝试看看是否有可能像acrobatHey man@rhens那样实现它,非常感谢!这就行了!。。。您是否知道如何实现下载功能,用户可以选择保存文件的路径?我不确定是否可以通过Javascript从PDF查看器中下载和保存文件。您可以打开URL并通过浏览器处理文件保存:
app.launchURL('https://URL/to/your/PDF');无论如何,要做好遇到安全问题的准备,这可能取决于PDF查看器的配置。