LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

【C#】Winform如何利用Aspose.Words/Cells/Slides将Office文档Word/Excel/PowerPoint一键转为PDF文件

admin
2025年2月11日 11:1 本文热度 126

【C#】Winform如何利用Aspose.Words/Cells/Slides将Office文档Word/Excel/PowerPoint一键转为PDF文件 

先在项目中添加:Aspose.Cells.dll、Aspose.Words.dll、Aspose.Slides.dll 三个dll的引用。

 

然后在代码中引入相关组件:

using Aspose.Words;

using Aspose.Cells;

using Aspose.Slides;

然后在组件上执行相关转换操作:

private void txt_file_path_MouseClick(object sender, MouseEventArgs e)

{

    // 创建 OpenFileDialog 对象

    OpenFileDialog openFileDialog = new OpenFileDialog();


    // 设置对话框标题

    openFileDialog.Title = "选择一个Office文件";


    // 设置文件过滤器,允许选择特定类型的文件

    openFileDialog.Filter = "Office文件 (*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx)|*.doc;*.docx;*.xls;*.xlsx;*.ppt;*.pptx";


    // 允许用户选择多个文件

    openFileDialog.Multiselect = false;


    // 显示对话框并检查用户是否点击了“确定”

    if (openFileDialog.ShowDialog() == DialogResult.OK)

    {

        // 获取用户选择的文件路径

        string[] selectedFiles = openFileDialog.FileNames;


        // 输出选择的文件路径

        foreach (string file in selectedFiles)

        {

            txt_file_path.Text = file;

        }

        // 设置输出PDF文件的路径

        if (txt_file_path.Text != "")

        {

            string sourceFilePath = txt_file_path.Text;

            string targetfilepath = (Public.Left(sourceFilePath, sourceFilePath.Length - 4) + ".pdf").Replace("..", ".");

            if (File.Exists(targetfilepath))

            {

                btn_office2pdf.Enabled = false;

                txt_result.Text = "文档“" + targetfilepath + "”已经存在,无需转换!";

                Console.WriteLine("文档“" + targetfilepath + "”已经存在,无需转换!");

            }

            else

            {

                btn_office2pdf.Enabled = true;

            }

        }

    }

}


private void btn_office2pdf_Click(object sender, EventArgs e)

{

    string sourceFilePath = txt_file_path.Text;

    if (sourceFilePath == "")

    {

        MessageBox.Show("转换失败:没有选择要转换的文件,请先选择!","系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

        return;

    }


    //转换Office文档为PDF文档

    office2pdf(sourceFilePath, 0);

}


public void office2pdf(string sourceFilePath, int alertFlag)

{

    // 检查源文件是否存在

    if (!File.Exists(sourceFilePath))

    {

        Console.WriteLine("文档“" + sourceFilePath + "”不存在,请先选择!");

        return;

    }


    // 检查源文件是否是指定格式

    string sourceFileExt = Public.Right(sourceFilePath, 4).ToLower();

    if (!(sourceFileExt == ".doc" || sourceFileExt == ".xls" || sourceFileExt == ".ppt" || sourceFileExt == "docx" || sourceFileExt == "xlsx" || sourceFileExt == "pptx"))

    {

        Console.WriteLine("文档格式“" + sourceFileExt + "”错误,必须为doc/xls/ppt文档!");

        return;

    }


    // 设置输出PDF文件的路径

    string targetfilepath = (Public.Left(sourceFilePath, sourceFilePath.Length - 4) + ".pdf").Replace("..", ".");

    if (File.Exists(targetfilepath))

    {

        Console.WriteLine("文档“" + targetfilepath + "”已经存在,无需转换!");

        return;

    }


    try

    {

        if (sourceFileExt == ".doc" || sourceFileExt == "docx")

        {

            // 读取doc文档

            Aspose.Words.Document doc = new Aspose.Words.Document(sourceFilePath);

            //保存为PDF文件,此处的SaveFormat支持很多种格式,如图片,epub,rtf 等等

            doc.Save(targetfilepath, Aspose.Words.SaveFormat.Pdf);

        }

        if (sourceFileExt == ".xls" || sourceFileExt == "xlsx")

        {

            // 初始化Workbook对象

            Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook(sourceFilePath);

            // 将Excel保存为PDF格式

            workbook.Save(targetfilepath, Aspose.Cells.SaveFormat.Pdf);

        }

        if (sourceFileExt == ".ppt" || sourceFileExt == "pptx")

        {

            // 加载PPT或PPTX文件

            var pres = new Presentation(sourceFilePath);

            // 保存为PDF格式

            pres.Save(targetfilepath, Aspose.Slides.Export.SaveFormat.Pdf);

        }

        Console.WriteLine("文档“" + sourceFilePath + "”转换为PDF文档成功。");

    }

    catch

    {

        Console.WriteLine("文档“" + sourceFilePath + "”转换为PDF文档失败!\r\n请检查是否是本程序没有当前目录写入权限等。");

    }

}


该文章在 2025/2/11 17:23:47 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2025 ClickSun All Rights Reserved