从用户输入而不是文件目录将多个文件ext转换为pdf

时间:2013-02-16 21:05:01

标签: c# asp.net word-automation

我在这个网站上找到了这个很好的,有效的代码,但我想做一些改动。

我想要求用户上传文档,但如果文档不是PDF格式,我想将其文档转换为PDF文件,例如转换所有doc,docx和excel文件。 / p>

我让它使用.doc文件,如果我想添加更多,我会将它们添加到“* .doc,* .docx,...”吗?

此外,如果文件位于同一目录中,目前程序正在转换文件。我想要它,以便它接受来自用户的新目录并将其保存到不同的目录,而不一定在同一个地方 - 例如,程序将从... \ Documents保存到... \上传。我怎么能这样做?

这是Word to PDF代码:

private void Word2PDF() {
        //Create a new Microsoft Word application object
     Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

    //Adding dummy value because c# doesn't have optional arguments
     object oMissing = System.Reflection.Missing.Value;

    //Getting list of word files in specified directory
     DirectoryInfo dirInfo = new DirectoryInfo("C:\\TestFilestore\\");
    FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

    word.Visible = false;
    word.ScreenUpdating = false;

        foreach (FileInfo wordFile in wordFiles) {
            //Cast as object for word open method
            Object filename = (Object) wordFile.FullName;

           Microsoft.Office.Interop.Word.Document 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 = wordFile.FullName.Replace(".doc", ".pdf");
            object fileFormat = WdSaveFormat.wdFormatPDF;

            //Save document into pdf format
            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);

            //close the word document, but leave the word application open.
            //doc has to be cast to type_document so that it will find the correct close method.
            object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
            ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
            doc = null;
        }

        //word has to be case to type_application so that it will find the correct quit method.
        ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
        word = null;
    }

1 个答案:

答案 0 :(得分:0)

如果要迭代所有具有不同扩展名的Word文档,可以从目录中获取所有文件,并使用Linq过滤列表。

这是一个例子,你需要将它合并回你的代码才能让它运行起来,但你会明白这一点。

    Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
    var oMissing = System.Reflection.Missing.Value;

    // grab all the filenames from your directory (*.*) and filter them with linq
    var wordDocumentFilenames = Directory.GetFiles(@"C:\TestFilestore\", "*.*").
                                Where(file => 
                                    file.ToLower().EndsWith("doc") ||
                                    file.ToLower().EndsWith("docx")).  // extend the list to your needs
                                    ToList(); 

    foreach (var wordDocumentFilename in wordDocumentFilenames)
    {
        Microsoft.Office.Interop.Word.Document wordDocument = word.Documents.Open(
            wordDocumentFilename, 
            ref oMissing,
            /* supply the rest of the parameters */);
    }