It works fine when document is opened in Acrobat, but does not work properly when the same PDF document is opened in web browser such as Google Chrome. this.closeDoc() is not getting executed in the Browser.
string path2 = @"D:\test\input.pdf"; string output = @"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();
}