private void SplitPdf_DragDrop(object sender, DragEventArgs e) { this.BackColor = _defaultBackColor; //Tuple to store all files List <Tuple <string> > FileListT = new List <Tuple <string> >(); //Temp string list for Drag And Drop from explorer string[] FileList = null; //Detect drag and drop from Explorer if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true) { // Extract the data from the DataObject-Container into a string list FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false); try { //Add extracted files to Tuple foreach (string item in FileList) { FileListT.Add(new Tuple <string>(item)); } } catch (Exception ex) { log.Error(ex); } } //Detect drag and drop from Outlook else if (e.Data.GetDataPresent("FileGroupDescriptor")) { OutlookDataObject dataObject = new OutlookDataObject(e.Data); string[] filenames = (string[])dataObject.GetData("FileGroupDescriptor"); MemoryStream[] filestreams = (MemoryStream[])dataObject.GetData("FileContents"); for (int fileIndex = 0; fileIndex < filenames.Length; fileIndex++) { //use the fileindex to get the name and data stream string filename = Path.Combine(Helpers.TempDirectory + filenames[fileIndex]); MemoryStream filestream = filestreams[fileIndex]; //save the file stream using its name to the application path FileStream outputStream = System.IO.File.Create(filename); filestream.WriteTo(outputStream); outputStream.Close(); //Here u get multiple file names writen in tuple list FileListT.Add(new Tuple <string>(filename)); } } else { return; } // Do something with the data... if (FileListT.Count() <= 0 || FileListT.Count() >= 2) { MessageBoxForm msgbox = new MessageBoxForm() { Caption = "Error: One file allowed", Message = "Error: Only one file is allowed for Drag and Drop operation!" }; msgbox.ShowDialog(); msgbox.Dispose(); return; } else { foreach (Tuple <string> FileFromList in FileListT) { string Ext = Path.GetExtension(FileFromList.Item1).ToLower(); string Name = Path.GetFileName(FileFromList.Item1); string FileName = Path.Combine(Helpers.TempDirectory + Name); string FileNameWithoutExt = Path.GetFileNameWithoutExtension(FileFromList.Item1); switch (Ext) { case ".pdf": //System.IO.File.Move(FileFromList.Item1, FileName); inputFileName = FileFromList.Item1; break; default: MessageBoxForm msgbox = new MessageBoxForm() { Caption = "Error: PDF file allowed", Message = "Error: Only PDF file is allowed for Drag and Drop operation!" }; msgbox.ShowDialog(); msgbox.Dispose(); break; } } } }
private void MergePDF_DragDrop(object sender, DragEventArgs e) { //Making shure backcolor is normal color this.BackColor = _defaultBackColor; //Tuple to store all files List <Tuple <string> > FileListT = new List <Tuple <string> >(); //Temp string list for Drag And Drop from explorer string[] FileList = null; //Detect drag and drop from Explorer if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true) { // Extract the data from the DataObject-Container into a string list FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false); try { //Add extracted pdf files to Tuple foreach (string item in FileList) { if (Path.GetExtension(item).ToLower() == ".pdf") { FileListT.Add(new Tuple <string>(item)); } } } catch (Exception ex) { log.Error(ex); } } //Detect drag and drop from Outlook else if (e.Data.GetDataPresent("FileGroupDescriptor")) { OutlookDataObject dataObject = new OutlookDataObject(e.Data); string[] filenames = (string[])dataObject.GetData("FileGroupDescriptor"); MemoryStream[] filestreams = (MemoryStream[])dataObject.GetData("FileContents"); for (int fileIndex = 0; fileIndex < filenames.Length; fileIndex++) { //use the fileindex to get the name and data stream string filename = Path.Combine(Helpers.TempDirectory + filenames[fileIndex]); MemoryStream filestream = filestreams[fileIndex]; //save the file stream using its name to the application path FileStream outputStream = System.IO.File.Create(filename); filestream.WriteTo(outputStream); outputStream.Close(); //Here u get multiple file names writen in tuple list if they are with .pdf extension if (Path.GetExtension(filename).ToLower() == ".pdf") { FileListT.Add(new Tuple <string>(filename)); } } } else { return; } // here we go thru tuple list with all files and add them to the listview, but first we check if we have any pdf file in the list if (FileListT.Count() <= 0) { MessageBoxForm msgbox = new MessageBoxForm() { Caption = "Error: No PDF files", Message = "Error: For merging we accept only PDF files!" }; msgbox.ShowDialog(); msgbox.Dispose(); return; } else { foreach (Tuple <string> FileFromList in FileListT) { FilesListView.Items.Add(Path.GetFileName(FileFromList.Item1), FileFromList.Item1); } } }