【问题标题】:C#. Doc to PDF conversionC#。文档到 PDF 的转换
【发布时间】:2014-12-03 05:01:36
【问题描述】:

我正在尝试使用 Visual Studio 2013 将 doc 或 docx 文件转换为 PDF。C# 语言。 我尝试调试,但我认为以下部分代码有问题:

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;
                }
            }
        }
    }
  }
}

【问题讨论】:

  • 你能把范围缩小一点吗? “不起作用”是什么意思?
  • 您将此标记为 asp.net,但您使用的是 Office Interop。这些不能很好地协同工作。微软specifically recommends against it。如果您继续在 ASP.NET 环境中使用 Interop 库,您可能会遇到各种问题。
  • 不要将 doc 转换为 pdf。不显示任何结果。
  • 你是否真的将所需的参数传递给 main 方法。
  • Thangadurai,如何传递参数?

标签: c# asp.net pdf-generation


【解决方案1】:

我注意到一段非常有趣的代码:

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

尝试删除@"C:\Source"。 我删除了它,代码在我的电脑上运行了。

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

【讨论】:

  • “@”符号告诉编译器转义给定字符串中的所有字符。完全没有必要,但如果不使用,可能需要相应地修改字符串的内容。
  • 我把 @"C:\Source" 换成了 "\\" 但还是不行。
  • 我收到此输出消息:程序“[9240] Conversion.vshost.exe”已退出,代码为 0 (0x0)。
  • 正常退出。我猜没有文件被处理。尝试在foreach循环中输出args、wordFiles的长度、wordFile.Name等来定位问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多