為 PDF 文件添加有效日期
Spire.PDF for .NET 是一款專門對 Word 文檔進行操作的 .NET 類庫。致力于在于幫助開發(fā)人員輕松快捷高效地創(chuàng)建、編輯、轉換和打印 Microsoft Word 文檔,而無需安裝 Microsoft Word。
行號用于在每行文本旁邊顯示 Word 自動計算的行數(shù)。當我們需要參考合同或法律文件等文檔中的特定行時,它非常有用。word中的行號功能允許我們設置起始值、編號間隔、與文本的距離以及行號的編號方式。使用 Spire.Doc,我們可以實現(xiàn)上述所有功能。本文將介紹如何將 HTML 轉換為 PDF。
歡迎加入spire技術交流群:767755948
PDF 規(guī)范格式中沒有定義過期日期的概念,但有一個變通方法,我們可以使用 JavaScript 應用過期日期。Spire.PDF 也支持向 PDF 文件添加 java 腳本操作。本文將介紹如何在 C# 和 VB.NET 中使用 Spire.PDF 為 PDF 文檔添加 JavaScript 過期日期。
步驟 1:創(chuàng)建一個 PdfDocument 類對象,并為其添加一個空白頁。
PdfDocument doc = new PdfDocument(); doc.Pages.Add();
第 2 步:定義 JavaScript 代碼。
string javaScript = "var rightNow = new Date();"
                    + "var endDate = new Date('October 20, 2016 23:59:59');"
                    + "if(rightNow.getTime() > endDate)"
                    + "app.alert('This Document has expired, please contact us for a new one.',1);"
                    + "this.closeDoc();";
第 3 步:創(chuàng)建一個 PdfJavaScriptAction 對象,在 PDF 文檔中執(zhí)行 java 腳本操作。
PdfJavaScriptAction js = new PdfJavaScriptAction(javaScript);
第 4 步:將 JavaScript 設置為 PDF 打開操作。
doc.AfterOpenAction = js;
第 5 步:保存文件。
doc.SaveToFile("ExpiryDate.pdf", FileFormat.PDF);
輸出:
 
 
完整代碼:
[C#]
using Spire.Pdf;
using Spire.Pdf.Actions;
namespace AddExpiryDate
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            doc.Pages.Add();
            string javaScript = "var rightNow = new Date();"
                     + "var endDate = new Date('October 20, 2016 23:59:59');"
                     + "if(rightNow.getTime() > endDate)"
                     + "app.alert('This Document has expired, please contact us for a new one.',1);"
                     + "this.closeDoc();";
            PdfJavaScriptAction js = new PdfJavaScriptAction(javaScript);
            doc.AfterOpenAction = js;
            doc.SaveToFile("ExpiryDate.pdf", FileFormat.PDF);
        }
    }
}
[VB.NET] 
Imports Spire.Pdf
Imports Spire.Pdf.Actions
Namespace AddExpiryDate
	Class Program
		Private Shared Sub Main(args As String())
			Dim doc As PdfDocument = New PdfDocument()
doc.Pages.Add()
 
String javaScript = "var rightNow = new Date();"
                    + "var endDate = new Date('October 20, 2016 23:59:59');"
                    + "if(rightNow.getTime() > endDate)"
                    + "app.alert('This Document has expired, please contact us for a new one.',1);"
                    Dim "this.closeDoc();" As +
Dim js As PdfJavaScriptAction = New PdfJavaScriptAction(javaScript)
doc.AfterOpenAction = js
doc.SaveToFile("ExpiryDate.pdf", FileFormat.PDF)
		End Sub
	End Class
End Namespace
                
            
 QQ交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號
渝公網(wǎng)安備
            50010702500608號
             
            
 客服熱線
客服熱線