欧美成人精品手机在线观看_69视频国产_动漫精品第一页_日韩中文字幕网 - 日本欧美一区二区

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

【C#】Winform如何利用Aspose.Words/Cells/Slides將Office文檔Word/Excel/PowerPoint一鍵轉為PDF文件

admin
2025年2月11日 11:1 本文熱度 710

【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