示例#1
0
        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);
                }
            }
        }
示例#2
0
        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;
                    }
                }
            }
        }
示例#3
0
        private void SplitFlatButton_Click(object sender, EventArgs e)
        {
            if (inputFileName == null)
            {
                MessageBoxForm msgbox = new MessageBoxForm()
                {
                    Caption = "Error: No PDF file!",
                    Message = "Error: Please add PDF file for spliting!"
                };

                msgbox.ShowDialog();
                msgbox.Dispose();
                return;
            }

            string spliter       = FileTextSpliterSingleLineTextField.Text;
            string outFolderName = Helpers.DocumentDirectory;

            if (SaveDirectoryCheckBox.CheckState == CheckState.Checked)
            {
                outFolderName = Path.GetDirectoryName(inputFileName) + @"\";
            }

            PdfDocument inputDocument = PdfReader.Open(inputFileName, PdfDocumentOpenMode.Import);

            for (int idx = 0; idx < inputDocument.PageCount; idx++)
            {
                // Create new document
                PdfDocument outputDocument = new PdfDocument
                {
                    Version = inputDocument.Version
                };

                if (PageNumbersCheckBox.CheckState == CheckState.Checked)
                {
                    outputDocument.Info.Title =
                        String.Format("Seit {0} von {1}", idx + 1, inputDocument.Info.Title);
                    outputDocument.Info.Creator = inputDocument.Info.Creator;

                    //Create Document FileName
                    string SaveFileName = String.Format("{3}{0}Teil{3}{1}{3}von{3}{2}.pdf", outputFileName, idx + 1, inputDocument.PageCount, spliter);

                    // Add the page and save it
                    outputDocument.AddPage(inputDocument.Pages[idx]);
                    outputDocument.Save(Path.Combine(outFolderName + SaveFileName));
                }
                else
                {
                    outputDocument.Info.Title =
                        String.Format("Seit {0} von {1}", idx + 1, inputDocument.Info.Title);
                    outputDocument.Info.Creator = inputDocument.Info.Creator;

                    //Create Document FileName
                    string SaveFileName = String.Format("{2}{0}{2}Seite{2}{1}.pdf", inputDocument, idx + 1, spliter);

                    // Add the page and save it
                    outputDocument.AddPage(inputDocument.Pages[idx]);
                    outputDocument.Save(Path.Combine(outFolderName + SaveFileName));
                }
            }
        }