C# PDF转Word实现 使用Aspose.Words
之前发过一篇使用 Microsoft.Office.Interop.Word 的PDF转Word
但是没装Office就容易出现 检索 COM 类工厂中 CLSID 为 {xxx} 的组件失败
所以又找了找发现Aspose.Words可以实现
引用 Aspose.Words.dll 可用nuget包(有需要的私信我)
#region Aspose.Words
using Aspose.Words;
using Aspose.Words.Saving;
public static bool WordToPDFByAspose(string from, string to,string password)
{
bool result = true;
try
{
Aspose.Words.Document doc = new Aspose.Words.Document(from);
//保存为PDF文件,此处的SaveFormat支持很多种格式,如图片,epub,rtf 等等
//权限这块的设置成不可复制
Aspose.Words.Saving.PdfSaveOptions saveOptions = new Aspose.Words.Saving.PdfSaveOptions();
// Create encryption details and set owner password.
PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails(password, "password", Aspose.Words.Saving.PdfEncryptionAlgorithm.RC4_128);
// Start by disallowing all permissions.
encryptionDetails.Permissions = PdfPermissions.DisallowAll;
// Extend permissions to allow editing or modifying annotations.
encryptionDetails.Permissions = PdfPermissions.ModifyAnnotations | PdfPermissions.DocumentAssembly;
saveOptions.EncryptionDetails = encryptionDetails;
// Render the document to PDF format with the specified permissions.
doc.Save(to, saveOptions);
//doc.Save(to, SaveFormat.Pdf);
}
catch (Exception e)
{
result = false;
LogHelper.WriteLog(e);
}
return result;
}
#endregion
分类:
C#
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂