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 2022-09-16 11:13  糯米白白  阅读(831)  评论(0编辑  收藏  举报

导航