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

posted on   糯米白白  阅读(922)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示