如果PDF已过期,请关闭“浏览器”选项卡

时间:2018-07-13 12:30:33

标签: javascript c# itext adobe spire.doc

在Acrobat中打开文档时,它可以正常工作,但是在Web浏览器(如Google Chrome)中打开相同的PDF文档时,则不能正常工作。浏览器未执行this.closeDoc()。

字符串路径2 = @“ D:\ test \ input.pdf”;             字符串输出= @“ D:\ test \ output.pdf”;

        if (File.Exists(path2)) 
        { 
            iText.Kernel.Pdf.PdfWriter writer = new iText.Kernel.Pdf.PdfWriter(output);
            iText.Kernel.Pdf.PdfDocument pdfDocument = new iText.Kernel.Pdf.PdfDocument(new iText.Kernel.Pdf.PdfReader(path2) ,writer);
            pdfDocument.AddNewPage();

            String js = "var rightNow = new Date();"
                      + "var endDate = new Date('2017-07-13');"
                      + "if(rightNow.getTime() > endDate){"
                      + "app.alert('This Document has expired, please contact us for a new one.',1);" 
                      + "this.closeDoc();}"
                      + "else{}";

            pdfDocument.GetCatalog().SetOpenAction(iText.Kernel.Pdf.Action.PdfAction.CreateJavaScript(js));

            pdfDocument.Close();
         } 

1 个答案:

答案 0 :(得分:0)

您是否尝试“ window.close()”?

String js = "var rightNow = new Date();"
    + "var endDate = new Date('2017-07-13');"
    + "if(rightNow.getTime() > endDate){"
    + "  app.alert('This Document has expired, please contact us for a new one.',1);" 
    + "  this.closeDoc(); "
    + "  if(window){ window.close(); } "
    + "}"
    + "else{}";