ewefr

C#語法 Word轉Pdf、合併Word、下載MS檔案

張貼者: Tony 2011年10月19日 星期三

這個要先加入Microsoft.Office.Interop.Word參考
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;//這個記得參考


Word轉Pdf
public static bool ConvertWordtoPdf(object WoldFilePath, object PdfFilePath)
{
// 在此处放置用户代码以初始化页面
object Missing = Type.Missing;
Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
//必须设置为不可见
WordApp.Visible = false;
try
{
//Document doc = new Document(WoldFilePath.ToString());
//doc.Save(PdfFilePath.ToString());
//创建以strTemp为模板的文档
//object oTemplate = Server.MapPath(strTemp);

Word.Document WordDoc = WordApp.Documents.Add(ref WoldFilePath, ref Missing, ref Missing, ref Missing);
WordDoc.Activate();

//对标签"Title"进行填充
//string strBM = "Title";
//object objBM = strBM;
//if (WordApp.ActiveDocument.Bookmarks.Exists(strBM) == true)
//{
// WordApp.ActiveDocument.Bookmarks.get_Item(ref objBM).Select();
// WordApp.Selection.TypeText("公文標題");
//}

object FileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;
//保存为新文件
//object oNewFileName = Server.MapPath(newFileName);
WordDoc.SaveAs(ref PdfFilePath, FileFormat, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing,
ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing, ref Missing);
WordDoc.Close(ref Missing, ref Missing, ref Missing);
WordApp.Quit(ref Missing, ref Missing, ref Missing);
return true;
}

catch (Exception e)
{
MessageBox.Show(e.Message.ToString());
return false;

}
}

合併Word
public static bool MergeWord(string[] InFilePath,string OutFilePath)
{
try
{
object missing = Missing.Value;
object iLastWord = InFilePath.Last();//最後合併的word
object oOutputDoc = OutFilePath;//合併檔
object oPageBreak = Word.WdBreakType.wdSectionBreakContinuous;
//object oPageBreak = Word.WdBreakType.wdLineBreak;//接下行合併(LineBreak)
//object oPageBreak = Word.WdBreakType.wdPageBreak;//接下頁合併(PageBreak)

Word.Application wordApp = new Word.Application();

Word.Document origDoc = wordApp.Documents.Open(ref iLastWord, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
origDoc.Activate();

for (int i = 0; i < InFilePath.Count();i++)
{
if (i == (InFilePath.Count() - 1) || InFilePath.Count() == 0)
break;
wordApp.Selection.InsertFile(InFilePath[i], ref missing, ref missing, ref missing, ref missing);
wordApp.Selection.InsertBreak(ref oPageBreak);
}

wordApp.ActiveDocument.SaveAs(ref oOutputDoc, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
wordApp.ActiveDocument.Close(ref missing, ref missing, ref missing);
wordApp.Quit(ref missing, ref missing, ref missing); //加這行可以 Kill WINWORD.EXE process

return true;
}
catch
{
return false;
}
}

下載MS檔案
public static bool DownloadFile(string FilePath,string NewFileName, HttpResponse response)
{
try
{
FileInfo DownloadFile = new FileInfo(FilePath);
response.Clear();
response.ClearHeaders();
response.Buffer = false;
//Response.ContentType指定檔案類型
//可以為application/ms-excel || application/ms-word || application/ms-txt
//application/ms-html || 或其他瀏覽器可以支援的文件
response.ContentType = "application/octet-stream";//所有類型
//下面這行很重要, attachment 参数表示作為附件下載,可以改成 online線上開啟
//filename=FileFlow.xls 指定输出檔案名稱,注意其附檔名和指定檔案類型相符,
//可以為:.doc || .xls || .txt ||.htm
response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(NewFileName, System.Text.Encoding.UTF8));
response.AppendHeader("Content-Length", DownloadFile.Length.ToString());//取得檔案大小

response.WriteFile(DownloadFile.FullName);
response.Flush();
response.End();

return true;
}
catch
{
return false;
}
}

1 Comment
  1. jw noze Said,

    C#中WORD轉PDF,试试iDiTect.Converter工具

    Posted on 2018年11月5日 晚上7:15

     

張貼留言

The Will Will Web

技術分享
MVP版主

重灌狂人

資訊分享

wowboxBlog

提供網頁設計相關資訊

KingKong Bruce記事

技術分享

C# Examples

csharp相關技術與範例

黑暗執行緒

技術分享
MVP版主

杨延成

技術分享
作者文章對於EF有詳細介紹

月光下的嘆息

工具分享
介紹許多好用的工具程式

Dot blogs

DotNet

格子樑/艾倫郭

技術分享
MVP版主