在 C# 中将 Doc 转换为 PDF。
在 C# 中将 Doc 转换为 PDF。
如何使用ASP.NET C#将.doc文件转换为.pdf文件。我不能使用任何第三方组件。
代码应该是:
- C#或VB.NET
- 与VS 2005兼容。(如果不是,请仍然发布您的回复,我将手动转换为VS 2005)
如果有任何问题,请告诉我。
谢谢!
admin 更改状态以发布 2023年5月22日
//Add Office Library using Word = Microsoft.Office.Interop.Word; object str_letter_path = @"D:\DOCTEST.doc"; object outputFilePathPDF = @"D:\PDFTEST.PDF"; Word.Application wordApp = new Word.Application(); wordApp.Visible = false; wordApp.ScreenUpdating = false; object oMissing = System.Reflection.Missing.Value; object fileFormat = Word.WdSaveFormat.wdFormatPDF; Word.Document doc = wordApp.Documents.Open(ref str_letter_path, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); doc.Activate(); doc.SaveAs(ref outputFilePathPDF, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; if (doc != null) ((Word._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing); ((Microsoft.Office.Interop.Word._Application)wordApp).Quit(ref saveChanges, ref oMissing, ref oMissing);
:这是一段包含粗体文字“123”的HTML标记。
private Microsoft.Office.Interop.Word.ApplicationClass MSdoc; //Use for the parameter whose type are not known or say Missing object Unknown = Type.Missing; private void word2PDF(object Source, object Target) { //Creating the instance of Word Application if (MSdoc == null)MSdoc = new Microsoft.Office.Interop.Word.ApplicationClass(); try { MSdoc.Visible = false; MSdoc.Documents.Open(ref Source, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown); MSdoc.Application.Visible = false; MSdoc.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize; object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF; MSdoc.ActiveDocument.SaveAs(ref Target, ref format, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown); } catch (Exception e) { MessageBox.Show(e.Message); } finally { if (MSdoc != null) { MSdoc.Documents.Close(ref Unknown, ref Unknown, ref Unknown); //WordDoc.Application.Quit(ref Unknown, ref Unknown, ref Unknown); } // for closing the application WordDoc.Quit(ref Unknown, ref Unknown, ref Unknown); } }
前提条件:
- 带有(默认情况下将安装主要互操作性程序集的)MS word2007。
- 插件SaveAsPDFandXPS(来自MS Site的免费插件)
确保您引用了Word.12。
它将自动向您的引用中添加Microsoft.Office.interop.word。按照这些步骤使用其他Office应用程序。
(注意:您应该已经安装了VS 2005 Office工具第二版运行时(VSTO 2005 SE)(x86)