private void btnShowPDFDetails_Click(object sender, RoutedEventArgs e) { try { // reference: https://stackoverflow.com/questions/1168976/wpf-datagrid-button-in-a-column-getting-the-row-from-which-it-came-on-the-cli/4926268 CustomPDFDocumentDetails pdfDetail = ((FrameworkElement)sender).DataContext as CustomPDFDocumentDetails; SinglePDFDetailsWindow sw = new SinglePDFDetailsWindow(EnableErrorLog); sw.Owner = Application.Current.MainWindow; // We must also set the owner for this to work. sw.WindowStartupLocation = WindowStartupLocation.CenterOwner; sw.originalTotalPages = pdfDetail.pages; sw.ShowDialog(); if (sw.DialogResult.HasValue && sw.DialogResult.Value) { pdfDetail.userSelectedPages = sw.SelectedPages; // after made change, refresh the grid dgPDFs.Items.Refresh(); } else { // do nothing. User cancel the action. } } catch (Exception ex) { Utils.WriteErrorMessageToFile(ex.ToString(), EnableErrorLog); } }
private List <CustomPDFDocumentDetails> ImportSelectedPDFFiles() { try { List <CustomPDFDocumentDetails> lstPDFs = new List <CustomPDFDocumentDetails>(); OpenFileDialog ofd = new OpenFileDialog(); ofd.Multiselect = true; // Allow user to select multiple PDF files ofd.Filter = "PDF Files|*.pdf"; // strictly limit selection to PDF files bool?result = ofd.ShowDialog(); if (result.HasValue && result.Value) { // This will handle single select as well as multiple select for (int i = 0; i < ofd.FileNames.Length; i++) { CustomPDFDocumentDetails pdf = new CustomPDFDocumentDetails(); // IMPORTANT - this only gets basic file info. To get PDF file detailed info: go to this method - GetSelectedPDFFileDetails pdf.fullPath = ofd.FileNames[i]; // this is FULL file path pdf.fileName = System.IO.Path.GetFileNameWithoutExtension(ofd.SafeFileNames[i]); // this is only file name WITHOUT extension pdf.extension = System.IO.Path.GetExtension(pdf.fullPath); // this will be: .pdf pdf.fileSizeInByte = new System.IO.FileInfo(pdf.fullPath).Length; // this will get actual file size in Byte if (pdf.extension.ToLower() == ".pdf") { lstPDFs.Add(pdf); } } return(lstPDFs); } return(lstPDFs); } catch (Exception ex) { Utils.WriteErrorMessageToFile(ex.ToString(), EnableErrorLog); return(null); } }
private void btnStart_Click(object sender, RoutedEventArgs e) { try { // NOT USED NOW // SaveWindow sw = new SaveWindow(); // sw.Owner = Application.Current.MainWindow; // We must also set the owner for this to work. // sw.WindowStartupLocation = WindowStartupLocation.CenterOwner; // sw.ShowDialog(); FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.RootFolder = Environment.SpecialFolder.Desktop; fbd.Description = "Please select a folder to save PDF file."; System.Windows.Forms.DialogResult fbdResult = fbd.ShowDialog(); if (fbdResult == System.Windows.Forms.DialogResult.OK) { // Now we have path FOR final PDF string outputFolder = fbd.SelectedPath; // Step 02. Process PDFs string tempFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // System.IO.Path.GetTempPath(); List <string> lstTempFilesFullPathList = new List <string>(); for (int i = 0; i < dgPDFs.Items.Count; i++) { CustomPDFDocumentDetails pdfDetail = (CustomPDFDocumentDetails)dgPDFs.Items[i]; string selectPages = string.Format("1-{0}", pdfDetail.pages); if (!Utils.IsNullOrBlank(pdfDetail.userSelectedPages)) { selectPages = pdfDetail.userSelectedPages; } string filePath = pdfDetail.fullPath; String tempFilename = string.Format("pdf_temp_{0}.pdf", i); string tempFilePath = System.IO.Path.Combine(tempFolder, tempFilename); // action 1. split KeepSelectedPages(filePath, selectPages, tempFilePath); // add temp file paths to list so we know where they are lstTempFilesFullPathList.Add(tempFilePath); } // define final PDF path & filename string finalFile = System.IO.Path.Combine(outputFolder, "final.pdf"); // user should be able to name their own file (with default filename given) // action 2. merge IEnumerable <string> filenames = lstTempFilesFullPathList.ToArray(); MergePDFs(filenames, finalFile); // action 3. purge temp files } } catch (Exception ex) { Utils.WriteErrorMessageToFile(ex.ToString(), EnableErrorLog); } }