public void ImageConvert(string input, string output) { try { //Gets and saves ImageName and directory List <string> file = input.Split('\\').ToList(); List <string> path = new List <string>(); for (int i = 0; i < file.Count - 1; i++) { path.Add(file[i]); } DirectoryInfo dirInfo = new DirectoryInfo(string.Join("\\", path)); FileInfo[] ImageFiles = dirInfo.GetFiles(file.Last()); if (ImageFiles.Count() == 0) { MessageBox.Show("No File Found. Please check you have selected correct existing file!"); } //Using foreach for future upgrade where you would convert multiple images at once foreach (var image in ImageFiles) { //Saves the image string outputpath; string[] separatedPath; ConvertImage(input, output, file, image, out outputpath, out separatedPath); string newDirect = outputpath; string newFileName = separatedPath.Last(); ImageToPNG imageToPNG = new ImageToPNG(); imageToPNG.FileName = newFileName; imageToPNG.DirectoryPath = output; imageToPNG.OutputDirectory = newDirect; imageToPNG.Type = "Image"; string message = "Converting Done!"; string title = "Done!"; MessageBox.Show(message, title); ConvertorDbService.AddImageConvert(imageToPNG.FileName, imageToPNG.OutputDirectory, true); } } catch (ArgumentNullException) { MessageBox.Show("Path not setted! Please set both Directories!", "Converting error"); ConvertorDbService.AddImageConvert(input, output, false); } catch (ExternalException) { MessageBox.Show("Path not setted! Please set both Directories!", "Converting error"); ConvertorDbService.AddImageConvert(input, output, false); } }
public void WordToPDFConv(string input, string outpath) { isDone = false; //Opens Microsoft Word in Background Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); //Gets DocumentName and DocumentPath List <string> file = input.Split('\\').ToList(); List <string> path = new List <string>(); for (int i = 0; i < file.Count - 1; i++) { path.Add(file[i]); } var worddoc = file.Last(); //Saves the name and the path DirectoryInfo dirInfo = new DirectoryInfo(string.Join("\\", path)); FileInfo[] wordFiles = dirInfo.GetFiles(worddoc); if (wordFiles.Count() == 0) { MessageBox.Show("No Documents In This path"); } //Hides the word document in Background word.Visible = false; word.ScreenUpdating = false; //Using foreach for future upgrade where you would convert multiple documents at once foreach (FileInfo wordFile in wordFiles) { try { WordToPDF wordToPDF = new WordToPDF(); object outputFileName = null; Object filename = (Object)wordFile.FullName; //Opens the file in word Document doc = word.Documents.Open(ref filename); doc.Activate(); //Renaming the original's file extension to PDF outputFileName = wordFile.FullName.Replace(".doc", ".pdf"); if (wordFile.Extension == ".docx") { outputFileName = wordFile.FullName.Replace(".docx", ".pdf"); } //Saves the renamed file in PDF format object fileFormat = WdSaveFormat.wdFormatPDF; doc.SaveAs(ref outputFileName, ref fileFormat); object saveChanges = WdSaveOptions.wdDoNotSaveChanges; //Closes the document (doc).Close(ref saveChanges); doc = null; string sourceFile = (string)(input); string destinationFile; if (wordFile.Extension == ".docx") { destinationFile = outpath + "\\" + wordFile.Name.Replace(".docx", ".pdf"); } else { destinationFile = outpath + "\\" + wordFile.Name.Replace(".doc", ".pdf"); } if (outpath == string.Join("\\", path)) { isDone = true; ConvertorDbService.AddDocConvert(wordFile.Name.Replace(".doc", ".pdf"), outpath, true); } else { if (File.Exists(destinationFile)) { var messagebox = MessageBox.Show("File with same name already exists! Do you want to overwrite it?", "Error", MessageBoxButtons.YesNoCancel); if (messagebox == DialogResult.Yes) { File.Delete(destinationFile); File.Move((string)(outputFileName), destinationFile); isDone = true; ConvertorDbService.AddDocConvert(wordFile.Name.Replace(".doc", ".pdf"), outpath, true); } else if (messagebox == DialogResult.No) { MessageBox.Show("Converting Canceled"); ConvertorDbService.AddDocConvert(wordFile.Name, outpath, false); } else { ConvertorDbService.AddDocConvert(wordFile.Name, outpath, false); } } else { ConvertorDbService.AddDocConvert((string)(outputFileName), destinationFile, false); File.Move((string)(outputFileName), destinationFile); isDone = true; } } string newDirect = destinationFile; string newFileName = sourceFile; wordToPDF.FileName = newFileName; wordToPDF.DirectoryPath = dirInfo.FullName; wordToPDF.OutputDirectory = newDirect; wordToPDF.Type = "Document"; } catch (ArgumentException) { ConvertorDbService.AddDocConvert(wordFile.Name, wordFile.DirectoryName, false); MessageBox.Show("Error during converting. Please check if you selected correct convert file and settings!"); } //Quitting Microsoft Word from the background word.Quit(ref oMissing, ref oMissing, ref oMissing); word = null; string message = "Converting Done!"; string title = "Done!"; if (isDone) { MessageBox.Show(message, title); } } }