2014-10-08 122 views
-2

我正尝试使用Visual Studio 2013将doc或docx文件转换为PDF。C#语言。 我试图调试,我想我有下面的代码部分的问题:C#。 Doc to PDF转换

public static void Main(string[] args) 
    { 
     if (args.Count() > 1) 
     { 
      translate.ConvertAllWordFilesToPdf(args[0], args[1]); 
     } 

    } 

我没有得到任何错误。我得到这个输出消息: 程序'[9240] Conversion.vshost.exe'已退出,代码为0(0x0)。

谢谢你的帮助。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using System.IO; 
using System.Text; 
using Microsoft.Office.Interop.Word; 

namespace Conversion 
{ 
public static class Program 
{ 
    public static void Main(string[] args) 
    { 
     if (args.Count() > 1) 
     { 
      translate.ConvertAllWordFilesToPdf(args[0], args[1]); 
     } 

    } 

    public class translate 
    { 

     public static void ConvertAllWordFilesToPdf(string WordFilesLocation, string PdfFilesLocation) 
     { 
      Document doc = null; 


      object oMissing = System.Reflection.Missing.Value; 
      Microsoft.Office.Interop.Word.Application word = null; 

      try 
      { 

       word = new Microsoft.Office.Interop.Word.Application(); 


       DirectoryInfo dirInfo = new DirectoryInfo(WordFilesLocation); 

       FileInfo[] wordFiles = dirInfo.GetFiles("*.doc"); 

       if (wordFiles.Length > 0) 
       { 
        word.Visible = false; 
        word.ScreenUpdating = false; 
        string sourceFile = ""; 
        string destinationFile = ""; 
        try 
        { 
         foreach (FileInfo wordFile in wordFiles) 
         { 

          Object filename = (Object)wordFile.FullName; 

          sourceFile = wordFile.Name; 
          destinationFile = ""; 


          doc = word.Documents.Open(ref filename, 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(); 
          object outputFileName = null; 

          if (wordFile.FullName.ToUpper().Contains(".DOCX")) 
          { 
           outputFileName = wordFile.FullName.Replace(".docx", ".pdf"); 
           destinationFile = sourceFile.Replace(".docx", ".pdf"); 

          } 
          else 
          { 
           outputFileName = wordFile.FullName.Replace(".doc", ".pdf"); 
           destinationFile = sourceFile.Replace(".doc", ".pdf"); 
          } 

          sourceFile = WordFilesLocation + @"C:\Source" + destinationFile; 
          destinationFile = PdfFilesLocation + @"C:\Destination" + destinationFile; 

          object fileFormat = WdSaveFormat.wdFormatPDF; 


          doc.SaveAs(ref outputFileName, 
           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 = WdSaveOptions.wdDoNotSaveChanges; 
          ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing); 
          doc = null; 


          if (System.IO.File.Exists(destinationFile)) 
          { 
           System.IO.File.Replace(sourceFile, destinationFile, null); 
          } 
          else 
          { 
           System.IO.File.Move(sourceFile, destinationFile); 
          } 

          Console.WriteLine("Success:" + "SourceFile-" + outputFileName.ToString() + " DestinationFile-" + destinationFile); 

         } 


         ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing); 
         word = null; 
        } 
        catch (Exception ex) 
        { 

         Console.WriteLine("Fail:" + "SourceFile-" + sourceFile + " DestinationFile-" + destinationFile + "#Error-" + ex.Message); 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("Error occured while processing"); 
       Console.WriteLine(ex.Message); 
      } 
      finally 
      { 
       if (doc != null) 
       { 
        ((_Document)doc).Close(ref oMissing, ref oMissing, ref oMissing); 
        doc = null; 

       } 
       if (word != null) 
       { 
        ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing); 
        word = null; 
       } 
      } 
     } 
    } 
    } 
} 
+0

你能缩小一点吗?你的意思是“不起作用”? – Matthew 2014-10-08 13:47:08

+3

您将此标记为'asp.net',但您正在使用Office Interop。这些不能很好地协同工作。微软[特别推荐它](http://support2.microsoft.com/kb/257757)。如果您继续在ASP.NET环境中使用Interop库,则可能会遇到各种问题。 – mason 2014-10-08 13:50:45

+0

不要将文档转换为pdf。没有显示任何结果。 – 2014-10-08 13:52:05

回答

0

我注意到一个很有意思的一段代码:

sourceFile = WordFilesLocation + @"C:\Source" + destinationFile; 
destinationFile = PdfFilesLocation + @"C:\Destination" + destinationFile; 

尽量去除@"C:\Source"。 我删除它,代码在我的电脑上工作。

此外,我不确定是否应该为某些位置字符串(不以“\”结尾)追加@"\"。小心。

+0

“@”符号告诉编译器转义给定字符串中的所有字符。根本不需要,但是如果不使用它,则可能需要相应地修改字符串的内容。 – 2014-10-08 14:05:19

+0

而不是@“C:\ Source”我把“\\”但它仍然不起作用。 – 2014-10-08 14:11:18

+0

我得到这个输出消息:程序'[9240] Conversion.vshost.exe'已退出代码0(0x0)。 – 2014-10-08 14:12:23