網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
先要把word或ppt轉(zhuǎn)換為pdf; 以pdf的格式展示,防止文件拷貝。
轉(zhuǎn)換方法
1、安裝Word、Excel、PowerPoint組件
注意:需安裝Microsoft.Office.Interop.Word\Excel\PowerPoint組件。
程序集如下:
2、轉(zhuǎn)換代碼
(1)將Word轉(zhuǎn)換為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轉(zhuǎn)換成pdf
/// </summary>
/// <param name="sourcePath">源文件路徑</param>
/// <param name="targetPath">目標(biāo)文件路徑</param>
/// <returns>true=轉(zhuǎn)換成功</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文件轉(zhuǎn)換成PDF格式文件
/// <summary>
/// 把Excel文件轉(zhuǎn)換成PDF格式文件
/// </summary>
/// <param name="sourcePath">源文件路徑</param>
/// <param name="targetPath">目標(biāo)文件路徑</param>
/// <returns>true=轉(zhuǎn)換成功</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文件轉(zhuǎn)換成PDF格式文件
///<summary>
/// 把PowerPoint文件轉(zhuǎn)換成PDF格式文件
///</summary>
///<param name="sourcePath">源文件路徑</param>
///<param name="targetPath">目標(biāo)文件路徑</param>
///<returns>true=轉(zhuǎn)換成功</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
相關(guān)推薦
- 2021-12-12 Redis實(shí)現(xiàn)分布式鎖的實(shí)例講解_Redis
- 2022-11-19 ubuntu desktop 開(kāi)啟root賬戶
- 2022-04-14 Python中五種列表拷貝的方法_python
- 2022-02-21 React事件綁定詳解_React
- 2022-01-31 torch.save實(shí)現(xiàn)對(duì)網(wǎng)絡(luò)結(jié)構(gòu)和模型參數(shù)的保存 & pytorch模型文件.pt .pt
- 2023-04-21 深入理解Python中__init__.py文件_python
- 2022-08-17 Python?獲取今天任意時(shí)刻的時(shí)間戳的方法_python
- 2022-05-10 setAttribute() 與 getAttribute() 用法剖析及選項(xiàng)卡操作的實(shí)例展示,這一
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支