PDF转换成Word,ppt转换成word
pdf與word我沒找到直接轉換的方式,不過可以用間接方式嘛!
pdf ==》picture ==》word!ppt轉word的原理也是先把ppt轉成圖片,再把圖片插入word!
先準備好幾個程序集:fontbox-0.1.0-dev.dll,IKVM.GNU.Classpath.dll,IKVM.Runtime.dll,Interop.Microsoft.Office.Core.dll,PDFBox-0.7.3.dll,Spire.Doc.dll,Spire.License.dll,Spire.Pdf.dll
一個小工具:gswin32.exe
pdf轉word主要代碼:
/// <summary>/// 將PDF文檔轉換成圖片/// </summary>/// <param name="pdfFile">PDF文檔物理路徑</param>/// <param name="imgPath">轉換成的圖片文件的存放物理路徑</param>/// <param name="isDeletePDF">轉換成圖片文件以后是否刪除原PDF文檔</param>/// <returns>返回轉換成的圖片文件物理路徑的集合</returns>public List<string> PdfToImages(string pdfFile, string imgPath, bool isDeletePDF){List<string> imgList = new List<string>();PDDocument doc = PDDocument.load(pdfFile);int pageCount = doc.getDocumentCatalog().getAllPages().size();//計算pdf文檔的總頁數string pdfFileName = Path.GetFileName("");int index = pdfFileName.LastIndexOf('.');if (index != -1)pdfFileName = pdfFileName.Substring(0, index);string imgFile = Path.Combine(imgPath, pdfFileName);//轉換成的圖片文件if (pageCount == 0) return null;if (pageCount == 1){imgFile += ".jpg";imgList.Add(imgFile);if (File.Exists(imgFile)) File.Delete(imgFile);}else{for (int i = 0; i < pageCount; i++){string _imgFile = imgFile + (i + 1).ToString() + ".jpg";imgList.Add(_imgFile);if (File.Exists(_imgFile)) File.Delete(_imgFile);}imgFile += "%d.jpg";}//此調用方法有命令框出現// System.Diagnostics.Process.Start(System.Configuration.ConfigurationManager.AppSettings["GhostScriptView"] +"\\gswin32.exe", System.Configuration.ConfigurationManager.AppSettings["GhostScriptArguments"] + @" -sOutputFile=" + imgFile + " " + pdfFile); ProcessStartInfo startinfo = new ProcessStartInfo();startinfo.CreateNoWindow = true;startinfo.FileName = System.Configuration.ConfigurationManager.AppSettings["GhostScriptView"] + "\\gswin32.exe";startinfo.Arguments = System.Configuration.ConfigurationManager.AppSettings["GhostScriptArguments"] + @" -sOutputFile=" + imgFile + " " + pdfFile;startinfo.WindowStyle = ProcessWindowStyle.Hidden;startinfo.CreateNoWindow = true;startinfo.UseShellExecute = true;Process p = new Process();p.StartInfo = startinfo;p.Start();if (p.WaitForExit(60*1000*10)){Thread.Sleep(1000);p.Close();}if (isDeletePDF){File.Delete(pdfFile);}return imgList;}/// <summary> /// 圖片插入word /// </summary> /// <param name="ImagesURL">圖片路徑集合</param> public void PicToWord(List<string> ImagesURL) {//Thread.Sleep(1000);//Process[] thepro = Process.GetProcessesByName("gswin32");//if (thepro.Length > 0)////如果進程曾在或者不止一個 //{// //逐個結束 // for (int i = 0; i < thepro.Length; i++)// {// //如果還沒有結束就關閉他 // if (!thepro[i].CloseMainWindow()) thepro[i].Kill();// }//}for (int i = 0; i < ImagesURL.Count; i++){if (File.Exists(ImagesURL[i].ToString() + ".jpg")){File.Delete(ImagesURL[i].ToString() + ".jpg");}if (File.Exists(ImagesURL[i].ToString())){File.Copy(ImagesURL[i].ToString(), ImagesURL[i].ToString() + ".jpg");}} //ThreadPool.QueueUserWorkItem( //使用線程池 //(P_temp) => //使用lambda表達式 //{word.Application newapp = new word.Application(); //創建Word應用程序對象object nothing = System.Reflection.Missing.Value;word.Document newdoc = newapp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);//生成一個word文檔 if (newdoc != null){try{for (int i = (ImagesURL.Count - 1); i >= 0; i--){string a = ImagesURL[i].ToString() + ".jpg";newdoc.InlineShapes.AddPicture(a);File.Delete(ImagesURL[i].ToString());File.Delete(ImagesURL[i].ToString() + ".jpg");}}catch (Exception ex){this.TextBox1.Text = ex.Message;}// newdoc.Close(); newdoc.SaveAs("F:\\Test.docx");newapp.Quit();}
// });
}
?
ppt轉word主要代碼:
/// <summary>/// PPT轉Word/// </summary>/// <param name="pptPath">ppt文件路徑</param>/// <param name="imgPath">圖片路徑</param>/// <param name="WordPath">存放word文檔路徑</param>/// <returns></returns>public bool PPTToWord(string pptPath,string imgPath,string WordPath){bool bo=true;List<string> image = new List<string>();try{var app = new PowerPoint.Application();var ppt = app.Presentations.Open(pptPath, Core.MsoTriState.msoFalse, Core.MsoTriState.msoFalse, Core.MsoTriState.msoFalse);var index = 0;var fileName = System.IO.Path.GetFileNameWithoutExtension(pptPath);foreach (PowerPoint.Slide slid in ppt.Slides){++index;//設置圖片大小slid.Export(imgPath + string.Format("page{0}.png", index.ToString()), "png", 1024, 768);image.Add(imgPath + string.Format("page{0}.png", index.ToString()));}//釋放資源 ppt.Close();app.Quit();GC.Collect();}catch (Exception ex){bo = false ;}Word.Application newapp = new Word.Application(); //創建Word應用程序對象object nothing = System.Reflection.Missing.Value;Word.Document newdoc = newapp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);//生成一個word文檔 if (newdoc != null){try{for (int i = (image.Count - 1); i >= 0; i--){string a = image[i].ToString();newdoc.InlineShapes.AddPicture(a);File.Delete(image[i].ToString());}}catch (Exception ex){bo = false;}newdoc.SaveAs(WordPath);newapp.Quit();}return bo;}這就ok了!
但是!當小女子放到服務器(win2008)上發現,天吶!bug!bug!bug!pdf能轉成圖片,但是在插入圖片的時候報錯,未將對象引用到實例!再看看ppt轉word。Oh my god!ppt不能open!
奇怪,在我的電腦上沒問題呀,這是怎么回事呢!仔細看看規律。pdf與office不是同一回事,出問題的都是office!抱著試試的心態,給office賦予權限去!
comexp.msc -32,打開32位的組件服務。"組件服務"->"計算機"->"我的電腦"->"DCOM配置"。找到office,給每一個office配置都賦予權限。
1、右鍵==》屬性==》安全==》啟動和激活權限==》自定義==》編輯==》添加Everyone==》本地啟動、本地激活。
2、右鍵==》屬性==》安全==》訪問權限==》自定義==》編輯==》添加Everyone==》本地訪問。
3、右鍵==》屬性==》安全==》配置權限==》自定義==》編輯==》添加Everyone==》完全控制。
4、右鍵==》屬性==》標識==》交互使用戶。
看看我們的程序,這個時候已經完全成功了!!!!
?程序集與gswin32下載
?
轉載于:https://www.cnblogs.com/xielianghui/p/7660225.html
總結
以上是生活随笔為你收集整理的PDF转换成Word,ppt转换成word的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zune linux_解决:安装Zune
- 下一篇: 10大电子商务创新案例集合