我正嘗試使用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;
}
}
}
}
}
}
你能縮小一點嗎?你的意思是「不起作用」? – Matthew 2014-10-08 13:47:08
您將此標記爲'asp.net',但您正在使用Office Interop。這些不能很好地協同工作。微軟[特別推薦它](http://support2.microsoft.com/kb/257757)。如果您繼續在ASP.NET環境中使用Interop庫,則可能會遇到各種問題。 – mason 2014-10-08 13:50:45
不要將文檔轉換爲pdf。沒有顯示任何結果。 – 2014-10-08 13:52:05