在C#中将文档转换为PDF

3

如何使用asp.net c#将.doc转换为.pdf?我不能使用任何第三方组件。

代码应该是:

  1. C#或vb.net
  2. 与VS 2005兼容。(如果不兼容,请也请回复,我会手动转换为VS 2005)

如果有任何问题,请告诉我。

谢谢!


请不要发布简单的请求,让别人为您编写代码。如果您有具体问题,请发布相关问题。 - Adam Robinson
1
@Adam:我会记住这个的。 - xorpower
1
可能是如何通过编程将Word文件转换为PDF?的重复问题。 - Mauricio Scheffer
@Scheffer:你的链接有效了!但是那里给出的代码片段只适用于 .doc 文件。有什么办法可以让它在 .docx 文件中工作吗? - xorpower
4个回答

8
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(从微软网站免费下载)

确保您引用了Word.12。它会自动添加Microsoft.Office.interop.word到您的引用中。按照以下步骤为其他office应用程序进行设置。 (注:您应该已经安装了VS 2005 Office工具第二版运行时(VSTO 2005 SE)(x86))


0
//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);

1
你的回答似乎没有提供任何与已接受答案不同的信息。通常来说,你应该描述你所发布的内容,而不仅仅是输入代码,尤其是对于一个已经有了被接受解决方案的旧问题。 - Seth Battin

0

您可以使用我的代码,它可以正常运行且不会在后台进程中保持打开的COM对象。

Application app = new Application();
                app.Visible = false;
                app.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                Documents documents = app.Documents;
                Document doc = documents.Open(fileLocation);
                newPath = Path.GetDirectoryName(fileLocation);
                newPath = newPath.Replace(newPath, outLocation);
                if (!File.Exists(newPath))
                {
                    doc.SaveAs2(newPath, WdSaveFormat.wdFormatPDF);
                }

                Marshal.ReleaseComObject(documents);
                doc.Close();
                Marshal.ReleaseComObject(doc);
                app.Quit();
                Marshal.ReleaseComObject(app);

0

您可以使用 Microsoft.Office.Interop.Word.dll 将 Word 文件转换为 PDF。

首先安装该软件包并添加对其的引用。

using Microsoft.Office.Interop.Word;

然后使用以下代码将Word文档转换为PDF

Application app = new Application();
Document doc = app.Documents.Open(@"D:/test.docx");
doc.SaveAs2(@"D:/test.pdf", WdSaveFormat.wdFormatPDF);
doc.Close();
app.Quit();
Console.WriteLine("Completed");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接