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請檢查是否是本程序沒有當前目錄寫入權限等。");
}
}