【问题标题】:convert doc to pdf in c#在c#中将doc转换为pdf
【发布时间】:2011-06-21 23:36:25
【问题描述】:

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

代码应该在

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

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

谢谢!

【问题讨论】:

  • 请不要发布简单的请求,要求某人为您编写代码。如果您有具体问题,请发布相关问题。
  • @Adam:我会记住这一点
  • @Scheffer:您的链接有效!但是那里给出的代码 sn-p 仅对 .doc 有用。有什么想法让它适用于 .docx?

标签: c# asp.net pdf visual-studio-2005 doc


【解决方案1】:

您可以使用我的代码,它可以正常工作,并且不会在后台进程中保留打开的 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);

【讨论】:

    【解决方案2】:

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

    先安装包并添加对它的引用。

    using Microsoft.Office.Interop.Word;
    

    然后使用以下代码Convert word document to 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");
    

    【讨论】:

      【解决方案3】:
      //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);
      

      【讨论】:

      • 目前尚不清楚您的答案添加了什么与接受的答案不同。一般来说,您应该描述您发布的内容,而不是仅仅输入代码,但对于已经有公认解决方案的老问题更是如此。
      【解决方案4】:
      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 添加到您的参考中。按照这些进行其他办公应用程序。 (注意:您应该已安装 VS 2005 Tools for Office 2nd Ed. Runtime (VSTO 2005 SE) (x86)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-05
        • 2014-12-09
        • 2012-04-14
        相关资源
        最近更新 更多