I found this nice, working piece of code on this site but I wanted to make some alterations.
I want to ask a user for a document to upload, but I would like to convert their document to a PDF file if it is not in the PDF format already, such as converting all doc, docx, and excel files.
I got it to work with .doc files, and if I wanted to add more do, would I add them to "*.doc, *.docx, ..."?
Also, currently the program is converting the file if the file is in the same directory. I want it so that it accepts the new directory from the user and saves it to a different directory and not necessarily both in the same place - for example, the program would save from ...\Documents to ...\Uploads. How could I do this?
Here is the Word to PDF code:
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;
}
If you want to iterate over all your Word
documents with different extensions you can grab all files from a directory, and filter the list with Linq.
Here an example, you'll need to merge it back into your code in order to get it to work, but you'll get the idea.
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 */);
}