private void textBoxPages_Validate(object sender, CancelEventArgs e) { CheckDocument(); if(!Enabled || suspendValidation) { e.Cancel = false; return; } TextBox textBox = sender as TextBox; string text = textBox.Text.Trim(); if (text.Trim().Length == 0) { e.Cancel = false; return; } if (!document.IsValid()) { ShowError("The document has been closed"); e.Cancel = true; } int pageCount = GetAvailablePages(); PageRange pageRange = new PageRange(); try { pageRange.Range = text; } catch (Exception ex) { ShowError(ex.Message); e.Cancel = true; return; } if (pageRange.End > pageCount) { ShowError(String.Format("There are only {0} page(s) in the doucment",pageCount)); e.Cancel = true; return; } e.Cancel = false; }
private void Save(SaveType saveType, bool concierge) { FolderBrowserDialog dialog = new FolderBrowserDialog(); string startingDirectory = null; DirectoryInfo directoryInfoMain; DirectoryInfo[] dirs; bool overwrite = false; try { directoryInfoMain = new DirectoryInfo(concierge ? Properties.Settings.Default.ConciergeLocation : Properties.Settings.Default.LastSavedDirectory); } catch (SecurityException /*securityException*/) { ShowError("You do not have permission to access " + (concierge ? "the Concierge directory" : Properties.Settings.Default.LastSavedDirectory)); return; } catch (ArgumentException /*argumentException*/) { ShowError("There is an invalid character in " + (concierge ? "either the name of the Concierge directory, or the name of a directory above it" : Properties.Settings.Default.LastSavedDirectory)); return; } catch (PathTooLongException /*pathTooLongException*/) { ShowError((concierge ? "The Concierge folder is too far deep. Try moving it to a higher directory" : Properties.Settings.Default.LastSavedDirectory + " is too far deep")); return; } if (directoryInfoMain.Exists) { startingDirectory = directoryInfoMain.FullName; if (concierge) { try { dirs = directoryInfoMain.GetDirectories(PatientsDirectoryName(saveType),SearchOption.TopDirectoryOnly); } catch (ArgumentException argumentException) { ShowError("The patient's name contains invalid characters.\n" + argumentException.Message); return; } catch (DirectoryNotFoundException directoryNotFoundException) { ShowError("It was not possible to read the Concierge directory\n" + directoryNotFoundException.Message); return; } catch (UnauthorizedAccessException unauthorizedAccessException) { ShowError("Permission denied reading the Concierge directory\n" + unauthorizedAccessException.Message); return; } if (dirs.Length == 1) { startingDirectory = dirs[0].FullName; } else if(dirs.Length > 1) { ShowError("There seems to be more than directory with the patient's name. Fix this and try again later"); return; } else { ShowError("The patient's directory does not exist. Fix this and try again later."); return; } } } else { if(concierge) ShowError("Cannot find the Concierge directory."); else ShowError("Cannot find " + Properties.Settings.Default.LastSavedDirectory); return; } dialog.SelectedPath = startingDirectory; var result = Show(dialog); if (result == DialogResult.OK) { string tempPath = Path.GetTempFileName() + ".PDF"; string tempPathPortable = GetPortablePath(tempPath); string fileName = (GetTextBoxDate(saveType).Text) + (saveType == SaveType.Procedure ? " " + textBoxProcedureName.Text + " " : " CONSULT ") + GetTextBoxLocation(saveType).Text + " " + GetTextBoxDoctor(saveType).Text + " " + PatientsDirectoryName(saveType) + ".PDF"; string destinationPath = dialog.SelectedPath + (dialog.SelectedPath.Last() != '\\' ? @"\" : String.Empty) + fileName; if (File.Exists(destinationPath)) { string prompt = String.Format( "The file \"{0}\" already exists in folder \"{1}\". "+ "Do you want to replace it? If you do, the original file will be lost forever.", fileName, destinationPath); result = AskYesNo(message:prompt,caption: "Warning"); if (result == DialogResult.No) return; overwrite = true; } PageRange pageRange = new PageRange(); pageRange.Range = GetTextBoxPages(saveType).Text; CAcroPDDoc pdoc = document.GetPDDoc(); object jsObject = pdoc.GetJSObject(); Type T = jsObject.GetType(); object[] parameters = {/* pdoc,*/ tempPathPortable, pageRange.Begin-1, pageRange.End-1 }; try { T.InvokeMember("ExtractPagesToFile", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, // no binder jsObject, parameters); } catch (Exception exception) { ShowError("Acrobat reported an error when extracting the pages.\n" + exception.Message); return; } if (overwrite) { try { File.Copy(tempPath, destinationPath, overwrite:true); } catch (Exception exceptionOnFileCopy) { ShowError("Error copying the temporary file over the original file.\n" + exceptionOnFileCopy.Message); return; } } else { try { File.Move(tempPath, destinationPath); } catch (Exception exceptionOnFileMove) { ShowError("Error moving the temporary file to the final destination.\n" + exceptionOnFileMove.Message); return; } } if(checkBoxView.Checked) Process.Start("explorer.exe", "/select,\"" + destinationPath + "\""); if (saveType == SaveType.Procedure) { AddPredefinedProcedure(textBoxProcedureName.Text); } if (concierge) { UpdateDirectoryLabels(destinationPath,saveType); } } }