LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

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

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

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

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

 

然后在代碼中引入相關組件:

using Aspose.Words;

using Aspose.Cells;

using Aspose.Slides;

然后在組件上執(zhí)行相關轉(zhuǎn)換操作:

private void txt_file_path_MouseClick(object sender, MouseEventArgs e)

{

    // 創(chuàng)建 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 + "”已經(jīng)存在,無需轉(zhuǎn)換!";

                Console.WriteLine("文檔“" + targetfilepath + "”已經(jīng)存在,無需轉(zhuǎn)換!");

            }

            else

            {

                btn_office2pdf.Enabled = true;

            }

        }

    }

}


private void btn_office2pdf_Click(object sender, EventArgs e)

{

    string sourceFilePath = txt_file_path.Text;

    if (sourceFilePath == "")

    {

        MessageBox.Show("轉(zhuǎn)換失敗:沒有選擇要轉(zhuǎn)換的文件,請先選擇!","系統(tǒng)提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

        return;

    }


    //轉(zhuǎn)換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 + "”已經(jīng)存在,無需轉(zhuǎn)換!");

        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 + "”轉(zhuǎn)換為PDF文檔成功。");

    }

    catch

    {

        Console.WriteLine("文檔“" + sourceFilePath + "”轉(zhuǎn)換為PDF文檔失敗!\r\n請檢查是否是本程序沒有當前目錄寫入權限等。");

    }

}


該文章在 2025/2/11 17:23:47 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務費用、相關報表等業(yè)務管理,結(jié)合碼頭的業(yè)務特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved

黄频国产免费高清视频,久久不卡精品中文字幕一区,激情五月天AV电影在线观看,欧美国产韩国日本一区二区
亚洲国产中文在线二区三区 | 日本午夜精品一区二区三区电影 | 特级撒尿AV在线播放 | 日本有码视频中文字幕 | 网友在线亚洲专区 | 香港三级日本三级a视频 |