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

C# 将多个文件ext从用户输入转换为pdf而不是文件目录,c#,asp.net,word-automation,C#,Asp.net,Word Automation,我在这个网站上找到了这段不错的代码,但我想做一些修改 我想要求用户上传文档,但如果文档不是PDF格式,我想将其转换为PDF文件,例如转换所有文档、docx和excel文件 我用它来处理.doc文件,如果我想添加更多do,我会将它们添加到“*.doc,*.docx,…”中吗 此外,如果文件位于同一目录中,则当前程序正在转换该文件。我希望它能够接受来自用户的新目录,并将其保存到另一个目录中,而不一定同时保存在同一个位置-例如,程序将从…\Documents保存到…\Uploads。我怎么能这样做 以

我在这个网站上找到了这段不错的代码,但我想做一些修改

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

我用它来处理.doc文件,如果我想添加更多do,我会将它们添加到“*.doc,*.docx,…”中吗

此外,如果文件位于同一目录中,则当前程序正在转换该文件。我希望它能够接受来自用户的新目录,并将其保存到另一个目录中,而不一定同时保存在同一个位置-例如,程序将从…\Documents保存到…\Uploads。我怎么能这样做

以下是Word到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;
    }

如果您想迭代具有不同扩展名的所有
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 */);
    }

你试过为这个..实现SaveDialog()功能吗?@DJKRAZE我不确定我是否遵循了你的确切意思。你能澄清一点吗?谢谢。非常感谢。我会继续尝试这个。但是,如果我想转换用户输入的文件,而不一定是目录中的文件,该怎么办?我要求用户上传一个文件,然后他们浏览一个文件,然后单击上载-在这里,我将把用户输入的文件转换为pdf格式,并将其保存到“testStorefile”文件夹中