網站首頁 編程語言 正文
先要把word或ppt轉換為pdf; 以pdf的格式展示,防止文件拷貝。
轉換方法
1、安裝Word、Excel、PowerPoint組件
注意:需安裝Microsoft.Office.Interop.Word\Excel\PowerPoint組件。
程序集如下:
2、轉換代碼
(1)將Word轉換為pdf:?
using Microsoft.Office.Core;
using System;
using System.IO;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using Word = Microsoft.Office.Interop.Word;
namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bool isSuccess = DOCConvertToPDF(Directory.GetCurrentDirectory() + "\\aa.docx", Directory.GetCurrentDirectory() + "\\aa.pdf");
if (isSuccess)
{
pdfViewer1.LoadFromFile(Directory.GetCurrentDirectory() + "\\aa.pdf");
}
}
/// <summary>
/// Word轉換成pdf
/// </summary>
/// <param name="sourcePath">源文件路徑</param>
/// <param name="targetPath">目標文件路徑</param>
/// <returns>true=轉換成功</returns>
public static bool DOCConvertToPDF(string sourcePath, string targetPath)
{
bool result = false;
Word.Application app = new Word.Application();
Word.Document doc = null;
object missing = System.Reflection.Missing.Value;
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
try
{
app.Visible = false;
doc = app.Documents.Open(sourcePath);
doc.ExportAsFixedFormat(targetPath, Word.WdExportFormat.wdExportFormatPDF);
result = true;
}
catch (Exception ex)
{
result = false;
throw new ApplicationException(ex.Message);
}
finally
{
if (doc != null)
{
doc.Close(ref saveChanges, ref missing, ref missing);
doc = null;
}
if (app != null)
{
app.Quit(ref missing, ref missing, ref missing);
app = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
}
}
(2)把Excel文件轉換成PDF格式文件
/// <summary>
/// 把Excel文件轉換成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路徑</param>
/// <param name="targetPath">目標文件路徑</param>
/// <returns>true=轉換成功</returns>
public static bool XLSConvertToPDF(string sourcePath, string targetPath)
{
bool result = false;
Excel.XlFixedFormatType targetType = Excel.XlFixedFormatType.xlTypePDF;
object missing = Type.Missing;
Excel.Application app = null;
Excel.Workbook book = null;
try
{
app = new Excel.Application();
object target = targetPath;
object type = targetType;
book = app.Workbooks.Open(sourcePath, missing, missing, missing, missing, missing,
missing, missing, missing, missing, missing, missing, missing, missing, missing);
book.ExportAsFixedFormat(targetType, target, Excel.XlFixedFormatQuality.xlQualityStandard, true, false, missing, missing, missing, missing);
result = true;
}
catch (Exception ex)
{
result = false;
throw new ApplicationException(ex.Message);
}
finally
{
if (book != null)
{
book.Close(true, missing, missing);
book = null;
}
if (app != null)
{
app.Quit();
app = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
(3)把PowerPoint文件轉換成PDF格式文件
///<summary>
/// 把PowerPoint文件轉換成PDF格式文件
///</summary>
///<param name="sourcePath">源文件路徑</param>
///<param name="targetPath">目標文件路徑</param>
///<returns>true=轉換成功</returns>
public static bool PPTConvertToPDF(string sourcePath, string targetPath)
{
bool result = false;
PowerPoint.PpSaveAsFileType targetFileType = PowerPoint.PpSaveAsFileType.ppSaveAsPDF;
object missing = Type.Missing;
PowerPoint.Application app = null;
PowerPoint.Presentation pres = null;
try
{
app = new PowerPoint.Application();
pres = app.Presentations.Open(sourcePath, MsoTriState.msoTrue, MsoTriState.msoFalse, MsoTriState.msoFalse);
pres.SaveAs(targetPath, targetFileType, Microsoft.Office.Core.MsoTriState.msoTrue);
result = true;
}
catch (Exception ex)
{
result = false;
throw new ApplicationException(ex.Message);
}
finally
{
if (pres != null)
{
pres.Close();
pres = null;
}
if (app != null)
{
app.Quit();
app = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
原文鏈接:https://www.cnblogs.com/springsnow/p/13305794.html
相關推薦
- 2022-03-15 box-shadow上下左右四個邊框設置陰影樣式
- 2022-07-06 R語言可視化開發forestplot根據分組設置不同顏色_R語言
- 2023-12-19 CentOS和Ubuntu中防火墻相關命令
- 2022-07-09 利用go語言實現查找二叉樹中的最大寬度_Golang
- 2022-07-11 Docker 安裝redis
- 2022-12-30 python算法與數據結構朋友圈與水杯實驗題分析實例_python
- 2023-08-15 :prop父組件給子組件傳遞函數 子組件接收 并default子組件自己的方法 問題
- 2023-04-01 python加速器numba使用詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支