2

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();
         } 
DevChamps
  • 33
  • 1
  • 11
  • *"It works fine when document is opened in Acrobat"* - anyone with a basic technical background will know that he merely needs to deactivate JavaScript in Acrobat to circumvent that mechanism. – mkl Jul 13 '18 at 13:40
  • @mkl can you please suggest then how can i secure it or make the pdf corrupt after the expiry date – DevChamps Jul 13 '18 at 14:22
  • One option is to use a professional DRM solution. If you don't want that, it costs money after all, you could try by sending the PDF in an unreadable state to the users (e.g. covering each page with some fixed image, or shifting the media box of each page outside the used area, or something like this) and only in case of a successful check (your `else {}` branch above) make the PDF readable (by iterating over the pages and removing the overlay image or correcting the media boxes). This of course means that users without a JavaScript enabled PDF viewer cannot read the PDF at all... – mkl Jul 13 '18 at 14:50
  • Such a mechanism obviously also can be circumvented but that requires a bit more work by the illicit user. – mkl Jul 13 '18 at 14:54
  • @mkl thats sounds good if you can provide me some example that would be great – DevChamps Jul 13 '18 at 17:21
  • Unfortunately I cannot, i have no specific Adobe javascript experience, merely a general idea what should be possible with it. – mkl Jul 13 '18 at 19:34

1 Answers1

0

Are you try "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{}";
Maidiries
  • 139
  • 1
  • 4