コード例 #1
0
        private void AddPrintExpireOnOpen(PdfDocument doc, DateTime ExpDate)
        {
            ExpDate = ExpDate.ToUniversalTime();
            var ExpireYear = ExpDate.Year;
            var ExpireMonth = ExpDate.Month;
            var ExpireDate = ExpDate.Day;
            var ExpireHour = ExpDate.Hour;
            var ExpireMin = ExpDate.Minute;

            //https://forums.adobe.com/thread/286036?start=0&tstart=0
            //http://forum.pdfsharp.net/viewtopic.php?f=2&t=368
            //This javascript string has been formatted for ease of update and reading
            string fullscript = @"function checkExpiration(LastYear, LastMonth, LastDate, LastHour, LastMin, LastSec, LastMS) {
    // document level function to see if passed date less than today's date 
    // check that numbers are passed as parameters 
    'use strict';
    if (isNaN(LastYear)) {
        LastYear = 1900;
    }
    if (isNaN(LastMonth)) {
        LastMonth = 1;
    }
    if (isNaN(LastDate)) {
        LastDate = 1;
    }
    if (isNaN(LastHour)) {
        LastHour = 0;
    }
    if (isNaN(LastMin)) {
        LastMin = 0;
    }
    if (isNaN(LastSec)) {
        LastSec = 0;
    }
    if (isNaN(LastMS)) {
        LastMS = 0;
    }

    LastMonth = LastMonth - 1; // adjust the passed month to the zero based month 
    // make the expiration date time object a numeric value 
    var myDate = new Date(Date.UTC(Number(LastYear), Number(LastMonth), Number(LastDate), Number(LastHour), Number(LastMin), Number(LastSec), Number(LastMS))).valueOf(), today = new Date().valueOf(); // convert passed expiration date time to a date time object value // get the current date time's object as a numeric value

    // return logical value of the comparison of the passed expiration date value to today - if true document has expired 
    return (myDate < today);
}
var ExpireYear = " + ExpireYear + @"; //2008
var ExpireMonth = " + ExpireMonth + @"; //March
var ExpireDate = " + ExpireDate + @"; //21st
var ExpireHour = " + ExpireHour + @"; //noon
var ExpireMin = " + ExpireMin + @"; //1

if (checkExpiration(ExpireYear, ExpireMonth, ExpireDate, ExpireHour, ExpireMin)) {
    try {
        this.closeDoc(1);
    } catch (e) {}
    app.alert('The file has expired. Contact creator to reprint the document.', 1, 0, 'Expired');
} else {
    //app.alert('Expires in ' + ExpireMonth + ' ' + ExpireDate + ' ' + ExpireYear + ' ' + ExpireHour + ' ' + ExpireMin, 1, 0, 'Expires in');
    this.syncAnnotScan();
    var annots = this.getAnnots();
    var i;
    for (i = 0; i < annots.length; i += 1) {
        if (annots[i].contents === 'ScreenProtector') {
            annots[i].hidden = true;
        }
    }

    this.print();
}";
            string DidPrintScript = @"
this.syncAnnotScan();
var annots = this.getAnnots();
var i;
for (i = 0; i < annots.length; i += 1) {
    if (annots[i].contents === 'ScreenProtector') {
        annots[i].hidden = false;
    }
}
try {
    this.closeDoc(1);
} catch (e) {}";
            /* Alternate method
            //If copies are needed
            //var pp = this.getPrintParams();
            //pp.NumCopies=eval(1);
            //this.print(pp);
            PdfDictionary PrintDictionary = new PdfDictionary();
            PrintDictionary.Elements["/Type"] = new PdfName("/Action");
            PrintDictionary.Elements["/S"] = new PdfName("/Named");
            PrintDictionary.Elements["/N"] = new PdfName("/Print");*/

            PdfDictionary DidPrintJavaScriptDictionary = new PdfDictionary();
            DidPrintJavaScriptDictionary.Elements["/Type"] = new PdfName("/Action");
            DidPrintJavaScriptDictionary.Elements["/S"] = new PdfName("/JavaScript");
            DidPrintJavaScriptDictionary.Elements.SetString("/JS", DidPrintScript);

            doc.SetAdditionalAction(new PdfName("/DP"), DidPrintJavaScriptDictionary);

            PdfDictionary UnhideDictionary = new PdfDictionary();
            UnhideDictionary.Elements["/Type"] = new PdfName("/Action");
            UnhideDictionary.Elements["/S"] = new PdfName("/JavaScript");
            UnhideDictionary.Elements.SetString("/JS", fullscript);
            
            doc.SetOpenAction(UnhideDictionary);
        }