62,074
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
namespace WordToPDFTest
{
public class WordToPDF
{
public WordToPDF()
{ }
public bool DoWordToPDF(string sourcePath, string targetPath, Word.WdExportFormat exportFormat)
{
bool result;
object paramMissing = Type.Missing;
Word.ApplicationClass wordApplication = new Word.ApplicationClass();
Word.Document wordDocument = null;
try
{
object paramSourceDocPath = sourcePath;
string paramExportFilePath = targetPath;
Word.WdExportFormat paramExportFormat = exportFormat;
bool paramOpenAfterExport = false;
Word.WdExportOptimizeFor paramExportOptimizeFor = Word.WdExportOptimizeFor.wdExportOptimizeForPrint;
Word.WdExportRange paramExportRange = Word.WdExportRange.wdExportAllDocument;
int paramStartPage = 0;
int paramEndPage = 0;
Word.WdExportItem paramExportItem = Word.WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true;
bool paramKeepIRM = true;
Word.WdExportCreateBookmarks paramCreateBookmarks = Word.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
bool paramDocStructureTags = true;
bool paramBitmapMissingFonts = true;
bool paramUseISO19005_1 = false;
wordDocument = wordApplication.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing, ref paramMissing, ref paramMissing,
ref paramMissing);
if (wordDocument != null)
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);
result = true;
}
finally
{
if (wordDocument != null)
{
wordDocument.Close(ref paramMissing, ref paramMissing, ref paramMissing);
wordDocument = null;
}
if (wordApplication != null)
{
wordApplication.Quit(ref paramMissing, ref paramMissing, ref paramMissing);
wordApplication = null;
}
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}
return result;
}
}
}
然后是本地文件上传后在服务器上转换成pdf文件
//首先文件上传至服务器
FileStream inStream = null;
FileStream outStream = null;
//保存到网络服务器的文件路径名
string file;
try
{
string fileName = localPathFile.Substring(localPathFile.LastIndexOf("\\") + 1);
//读取本地文件流
inStream = File.OpenRead(localPathFile);
//保存到网络服务器的文件路径名
file = Server.MapPath("/upload/") + fileName;
outStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
byte[] bytes = new byte[4096];
int start = 0;
int length;
while ((length = inStream.Read(bytes, 0, 4096)) > 0)
{
outStream.Write(bytes, 0, length);
start += length;
}
}
catch (Exception e)
{
throw new Exception("错误出现在:" + e.Message);
}
finally
{
inStream.Close();
outStream.Close();
}
//然后把word文件转化成pdf
Word.WdExportFormat wd = Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF;
WordToPDF wtp = new WordToPDF();
//在本地转换成pdf文件
string targetFilename = Server.MapPath("/download/") + Guid.NewGuid().ToString() + ".pdf";
wtp.DoWordToPDF(file, targetFilename, wd);
targetFilename 就是服务器上转换后的pdf文件完整路径名,下载方法略
localPathFile 本地word文件完成路径名