本快速教程指导如何使用来自 PFX 证书文件的 C# 在 PowerPoint 中插入签名。它包含有关环境配置、分步程序逻辑和可运行示例代码的信息,用于使用 C#向 PowerPoint 添加数字签名。它还提供了有关在将数字签名注释添加到演示文件并将其保存为 PPTX 文件之前设置数字签名注释的信息。
使用 C# 在 PowerPoint 中插入签名的步骤
- 配置环境以添加 Aspose.Slides for .NET 以签署演示文稿
- 在需要从 PFX 证书签名的 Presentation 对象中创建或加载文件
- 通过提供 PFX 证书文件部分和密码来实例化 DigitalSignature 类对象
- 设置要插入的签名的所需属性
- 将新创建的签名添加到演示文稿的签名集合中
- 保存添加了新签名的更新演示文稿
这些步骤描述了如何使用 C# 在 PowerPoint 中添加签名的过程,方法是共享指向必要资源、程序序列和示例代码的链接。介绍了完成任务所需的所有必要类、方法和属性,例如 Presentation 类用于创建或加载演示文稿,DigitalSignature 类用于加载 PFX 证书。需要注意的是,此功能目前仅适用于 PPTX 文件。
使用 C# 在 PowerPoint 中插入数字签名的代码
using Aspose.Cells; | |
using Aspose.Slides; | |
using System; | |
namespace AsposeProjects | |
{ | |
class Program | |
{ | |
static void Main(string[] args) // Main function to sign a presentation in C# | |
{ | |
// If you are using .NET Framework 4.7.0 or higher, uncomment the following lines of codes as this framework does not take sha1 | |
//AppContext.SetSwitch("Switch.System.Security.Cryptography.Xml.UseInsecureHashAlgorithms", true); | |
//AppContext.SetSwitch("Switch.System.Security.Cryptography.Pkcs.UseInsecureHashAlgorithms", true); | |
// Initialize license | |
Aspose.Slides.License lic = new Aspose.Slides.License(); | |
lic.SetLicense("Aspose.Total.lic"); | |
// Create or load the presentation | |
Presentation presentation = new Presentation(); | |
// Instantiate the DigitalSignature by providing the PFX file and the password | |
DigitalSignature digitalSignature = new DigitalSignature("certificate.pfx", "mypass"); | |
// Set comments for the signature for user assistance | |
digitalSignature.Comments = "Test comments for the digital signature"; | |
// Insert the new signature to the signatures collection | |
presentation.DigitalSignatures.Add(digitalSignature); | |
// Save presentation | |
presentation.Save("SomePresentationSigned.pptx", Aspose.Slides.Export.SaveFormat.Pptx); | |
Console.WriteLine("Done"); | |
} | |
} | |
} |
此代码演示如何使用 C# 在 PowerPoint 中添加签名,在第一步中,您使用 Presentation 类加载/创建演示文稿文件,该类提供许多其他构造函数也可以从流而不是磁盘加载文件,并使用LoadOptions 类对象来自定义加载过程。同样,您可以使用 Comments 属性在将数字签名添加到演示文稿之前为其设置注释。需要注意的是,对于.NET Framework等于或高于4.7.0的,需要取消程序开头的两行代码的注释。
这个尖锐的教程解释了如何使用 C#向 PowerPoint 添加签名。如果您想了解加密演示文稿的过程,请参阅 如何在 C# .NET 中保护 PowerPoint 演示文稿 上的文章。