如何将Word转换为PDF

一、使用Spire组件

文件提取自Spire.Office for .NET v2.16

组件名称 组件版本

Spire.Doc.dll 5.9.23.14040

Spire.Presentation.dll 2.8.13.14040

Spire.XLS.dll 7.12.23.14040

Spire.Pdf.dll 3.9.130.14040

使用方法:请将需要用的组件连同Spire.License.dll一起添加至项目引用。PS:桌面客户端(WPF/WinForm等)调用应该没有问题,Web调用会报错,破解水平有限,请体谅。

 

免费下载Spire.Doc for .NET并将其转换为PDF以供个人使用或评估。 在C#中将Word转换为PDF的整个过程可以描述为2个简单的步骤。

1.通过使用ocument.LoadFromFile()方法声明您准备的文档和加载文档,传递给该方法的参数是文件名字符串。

2.使用document.SaveToFile()方法将Word文档另存为PDF,传递给此方法的参数是文件名字符串和文件格式,文件格式必须为PDF格式。

全部代码:

using Spire.Doc;
using Spire.Presentation;
using Spire.Xls;
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //word转PDF
            Document word = new Document();
            word.LoadFromFile(@"C:\1.docx");
            word.SaveToFile("ToPdf.pdf", Spire.Doc.FileFormat.PDF);
            System.Diagnostics.Process.Start("ToPdf.pdf");

            //Excel转PDF
            Workbook xls = new Workbook();
            xls.LoadFromFile(@"C:\1.xlsx");
            xls.SaveToFile("ToPdf.pdf", Spire.Xls.FileFormat.PDF);
            xls.ConverterSetting.SheetFitToPage = true;
            System.Diagnostics.Process.Start("ToPdf.pdf");

            //PPT转PDF
            Presentation ppt = new Presentation();
            ppt.LoadFromFile(@"C:\1.pptx");
            ppt.SaveToFile("ToPdf.pdf", Spire.Presentation.FileFormat.PDF);
            System.Diagnostics.Process.Start("ToPdf.pdf");
        }
    }
}

 

二、使用Aspose组件

ASPOSE及授权

Aspose.Cells.dll 文件版本8.3

Aspose.Pdf.dll 文件版本9.8

Aspose.Slides.dll文件版本14.10

Aspose.Words.dll 文件版本14.10

全部代码:

using Aspose.Cells;
using Aspose.Slides;
using Aspose.Words;
using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        /// <summary>
        /// PPT转PDF
        /// </summary>
        /// <param name="InPath">输入路径</param>
        /// <param name="OutPath">输出路径</param>
        public void PPT_PDF(string InPath, string OutPath)
        {
            Presentation ppt = new Presentation(InPath);
            ppt.Save(OutPath, Aspose.Slides.Export.SaveFormat.Pdf);
        }

        /// <summary>
        /// Word转PDF
        /// </summary>
        /// <param name="InPath"></param>
        /// <param name="OutPath"></param>
        public void Word_PDF(string InPath, string OutPath)
        {
            Document doc = new Document(InPath);
            doc.Save(OutPath, Aspose.Words.SaveFormat.Pdf);
        }

        /// <summary>
        /// Excel转PDF
        /// </summary>
        /// <param name="InPath"></param>
        /// <param name="OutPath"></param>
        public void Excel_PDF(string InPath, string OutPath)
        {
            Workbook excel = new Workbook(InPath);
            excel.Save(OutPath, Aspose.Cells.SaveFormat.Pdf);
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            AsposeLicenseHelper.SetCellsLicense();
            Excel_PDF(@"C:\Users\PC\Documents\WeChat Files\yangbobin\FileStorage\File\2021-09\校服费(1).xls", "ToPdf.pdf");
            System.Diagnostics.Process.Start("ToPdf.pdf");
        }
    }
}

public static class AsposeLicenseHelper
{
    public const string Key =
        "PExpY2Vuc2U+DQogIDxEYXRhPg0KICAgIDxMaWNlbnNlZFRvPkFzcG9zZSBTY290bGFuZCB" +
        "UZWFtPC9MaWNlbnNlZFRvPg0KICAgIDxFbWFpbFRvPmJpbGx5Lmx1bmRpZUBhc3Bvc2UuY2" +
        "9tPC9FbWFpbFRvPg0KICAgIDxMaWNlbnNlVHlwZT5EZXZlbG9wZXIgT0VNPC9MaWNlbnNlV" +
        "HlwZT4NCiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1p" +
        "dGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+DQogICAgPE9yZGVySUQ+MTQ" +
        "wNDA4MDUyMzI0PC9PcmRlcklEPg0KICAgIDxVc2VySUQ+OTQyMzY8L1VzZXJJRD4NCiAgIC" +
        "A8T0VNPlRoaXMgaXMgYSByZWRpc3RyaWJ1dGFibGUgbGljZW5zZTwvT0VNPg0KICAgIDxQc" +
        "m9kdWN0cz4NCiAgICAgIDxQcm9kdWN0PkFzcG9zZS5Ub3RhbCBmb3IgLk5FVDwvUHJvZHVj" +
        "dD4NCiAgICA8L1Byb2R1Y3RzPg0KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl" +
        "0aW9uVHlwZT4NCiAgICA8U2VyaWFsTnVtYmVyPjlhNTk1NDdjLTQxZjAtNDI4Yi1iYTcyLT" +
        "djNDM2OGYxNTFkNzwvU2VyaWFsTnVtYmVyPg0KICAgIDxTdWJzY3JpcHRpb25FeHBpcnk+M" +
        "jAxNTEyMzE8L1N1YnNjcmlwdGlvbkV4cGlyeT4NCiAgICA8TGljZW5zZVZlcnNpb24+My4w" +
        "PC9MaWNlbnNlVmVyc2lvbj4NCiAgICA8TGljZW5zZUluc3RydWN0aW9ucz5odHRwOi8vd3d" +
        "3LmFzcG9zZS5jb20vY29ycG9yYXRlL3B1cmNoYXNlL2xpY2Vuc2UtaW5zdHJ1Y3Rpb25zLm" +
        "FzcHg8L0xpY2Vuc2VJbnN0cnVjdGlvbnM+DQogIDwvRGF0YT4NCiAgPFNpZ25hdHVyZT5GT" +
        "zNQSHNibGdEdDhGNTlzTVQxbDFhbXlpOXFrMlY2RThkUWtJUDdMZFRKU3hEaWJORUZ1MXpP" +
        "aW5RYnFGZkt2L3J1dHR2Y3hvUk9rYzF0VWUwRHRPNmNQMVpmNkowVmVtZ1NZOGkvTFpFQ1R" +
        "Hc3pScUpWUVJaME1vVm5CaHVQQUprNWVsaTdmaFZjRjhoV2QzRTRYUTNMemZtSkN1YWoyTk" +
        "V0ZVJpNUhyZmc9PC9TaWduYXR1cmU+DQo8L0xpY2Vuc2U+";

    public static Stream LStream = (Stream)new MemoryStream(Convert.FromBase64String(Key));

    private static readonly string LicensePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\Aspose.Total.lic";

    public static void SetWordsLicense()
    {
        var l = new Aspose.Words.License();
        l.SetLicense(LicensePath);
    }

    public static void SetPdfLicense()
    {
        var l = new Aspose.Cells.License();
        l.SetLicense(LicensePath);
    }

    public static void SetSlidesLicense()
    {
        var l = new Aspose.Slides.License();
        l.SetLicense(LicensePath);
    }

    /// <summary>
    /// Excel
    /// </summary>
    public static void SetCellsLicense()
    {
        var l = new Aspose.Cells.License();
        l.SetLicense(LicensePath);
    }
}

posted on 2021-09-06 17:40  springsnow  阅读(978)  评论(0编辑  收藏  举报

导航