正如大家所知道的,Adobe Acrobat Automation和Aspose.Pdf for .NET 都允许你通过 编程的方式将文本印章/水印添加到PDF文件中。然而,Aspose.Pdf for .NET比Adobe Acrobat Automation 更易于使用。在这篇文章中,我将为大家展示如何使用Adobe Acrobat Automation和Aspose.Pdf for .NET 添加文本印章和水印,以及这两种产品之间的区别。
下面的代码片段向你展示了如何使用Adobe Acrobat Automation将文本印章/水印添加到PDF文件。
[C#]
// Create an AVDoc object
AcroAVDoc gAVDoc = new AcroAVDoc();
gAVDoc.Open("D:/Ap Data/Source_Files/Test.pdf","");
CAcroPDDoc pdDoc = (CAcroPDDoc)gAVDoc.GetPDDoc();
//Acquire the Acrobat JavaScript Object interface from the PDDoc object
Object jsObj = pdDoc.GetJSObject();
Type T = jsObj.GetType();
//get current time and make a string from it
System.DateTime currentTime = System.DateTime.Now;
// make a color object
Object colorObj = T.InvokeMember(
"color",
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.Instance,
null, jsObj, null);
Type colorType = colorObj.GetType();
Object blueColorObj = colorType.InvokeMember(
"blue",
BindingFlags.GetProperty |
BindingFlags.Public |
BindingFlags.Instance,
null, colorObj, null);
// Add a text watermark.
object[] addTextWatermarkParam = { currentTime.ToShortTimeString(), 1, "Helvetica", 100, blueColorObj, 0, 0, true, true, true, 0, 3, 20, -45, false, 1.0, false, 0, 0.7 };
T.InvokeMember(
"addWatermarkFromText",
BindingFlags.InvokeMethod |
BindingFlags.Public |
BindingFlags.Instance,
null, jsObj, addTextWatermarkParam);
[VB.NET]
' Create an AVDoc object
Dim gAVDoc As New AcroAVDoc()
gAVDoc.Open("D:/Ap Data/Source_Files/Test.pdf", "")
Dim pdDoc As CAcroPDDoc = CType(gAVDoc.GetPDDoc(), CAcroPDDoc)
'Acquire the Acrobat JavaScript Object interface from the PDDoc object
Dim jsObj As Object = pdDoc.GetJSObject()
Dim T As Type = jsObj.GetType()
'get current time and make a string from it
Dim currentTime As System.DateTime = System.DateTime.Now
' make a color object
Dim colorObj As Object = T.InvokeMember("color", BindingFlags.GetProperty Or BindingFlags.Public Or BindingFlags.Instance, Nothing, jsObj, Nothing)
Dim colorType As Type = colorObj.GetType()
Dim blueColorObj As Object = colorType.InvokeMember("blue", BindingFlags.GetProperty Or BindingFlags.Public Or BindingFlags.Instance, Nothing, colorObj, Nothing)
' Add a text watermark.
Dim addTextWatermarkParam() As Object = {currentTime.ToShortTimeString(), 1, "Helvetica", 100, blueColorObj, 0, 0, True, True, True, 0, 3, 20, -45, False, 1.0, False, 0, 0.7}
T.InvokeMember("addWatermarkFromText", BindingFlags.InvokeMethod Or BindingFlags.Public Or BindingFlags.Instance, Nothing, jsObj, addTextWatermarkParam)
下面的代码片断展示了如何使用Aspose.Pdf for .NET控件中的PdfFileStamp类添加文本印章/水印到PDF文档中。
[C#]
//open document
PdfFileStamp fileStamp = new PdfFileStamp(@"D:\Source_Files\test.pdf", @"D:\Source_Files\output.pdf");
//create stamp
Aspose.Pdf.Facades.Stamp stamp = new Aspose.Pdf.Facades.Stamp();
//get current time and make a string from it
System.DateTime currentTime = System.DateTime.Now;
stamp.BindLogo(new FormattedText(currentTime.ToShortTimeString(), Color.Blue, Color.White, Aspose.Pdf.Facades.FontStyle.Helvetica, EncodingType.Winansi, true, 100));
//Set Origion
stamp.SetOrigin(100, 600);
//Set as Background
stamp.IsBackground = true;
//Set Opacity
stamp.Opacity = .7F;
//add stamp to PDF file
fileStamp.AddStamp(stamp);
//save updated PDF file
fileStamp.Close();
[VB.NET]
'open document
Dim fileStamp As New PdfFileStamp("D:\Source_Files\test.pdf", "D:\Source_Files\output.pdf")
'create stamp
Dim stamp As New Aspose.Pdf.Facades.Stamp()
'get current time and make a string from it
Dim currentTime As System.DateTime = System.DateTime.Now
stamp.BindLogo(New FormattedText(currentTime.ToShortTimeString(), Color.Blue, Color.White, Aspose.Pdf.Facades.FontStyle.Helvetica, EncodingType.Winansi, True, 100))
'Set Origion
stamp.SetOrigin(100, 600)
'Set as Background
stamp.IsBackground = True
'Set Opacity
stamp.Opacity = 0.7F
'add stamp to PDF file
fileStamp.AddStamp(stamp)
'save updated PDF file
fileStamp.Close()