private void OpenFile(object sender, RoutedEventArgs e)
        {
            OpenFileDialog <ArchiveDialog> ofd = new OpenFileDialog <ArchiveDialog>
            {
                FileDlgStartLocation = AddonWindowLocation.Bottom,
                InitialDirectory     = new System.Windows.Forms.OpenFileDialog().InitialDirectory,
                FileDlgOkCaption     = "&Открыть"
            };

            ofd.SetPlaces(new object[] { @"c:\", (int)Places.MyComputer, (int)Places.Favorites, (int)Places.All_Users_MyVideo, (int)Places.MyVideos });
            if (ofd.ShowDialog() == true)
            {
                var tab = new ArchiveTab(ofd.FileName, ofd.ChildWnd.Key);
                tab.LoadDataWin += LoadData;
                tab.CloseTab    += CloseTab;
                tab.TabIndex     = Archives.Items.Count;
                Archives.Items.Add(tab);
                Archives.SelectedIndex = tab.TabIndex;
                tab.Initialize();
            }
        }
        internal static Document AddDocument(string personnelNumber, string file, int?parentDocumentID, bool attachToDossier, Njit.Program.ComponentOne.Enums.SaveFormats imageFormat, Njit.Program.ComponentOne.Enums.CompressionTypes imageCompression, ArchiveTab archivetab)
        {
            System.IO.FileInfo fileInfo = new FileInfo(file);
            if (Setting.Archive.ThisProgram.LoadedArchiveSettings.MaxDocumentsSize > 0)
            {
                if (fileInfo.Length / 1024 > Setting.Archive.ThisProgram.LoadedArchiveSettings.MaxDocumentsSize)
                {
                    throw new Exception(string.Format("حجم فایل نباید از {0} کیلوبایت بیشتر باشد\r\nحجم این فایل {1} کیلوبایت است", Setting.Archive.ThisProgram.LoadedArchiveSettings.MaxDocumentsSize, fileInfo.Length / 1024));
                }
            }
            long   number        = Controller.Archive.DocumentController.GetNewDocumentNumber(personnelNumber);
            string fileExtension = Path.GetExtension(file).ToLower();

            Model.Archive.Dossier dossier = Controller.Archive.DossierController.Select(personnelNumber);
            if (imageExtensions.Contains(fileExtension))
            {
                switch (imageFormat)
                {
                case Njit.Program.ComponentOne.Enums.SaveFormats.None:
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.OnePdf:
                    if (fileExtension != ".pdf")
                    {
                        fileExtension = ".pdf";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Pdf.CreatePDF.FromImages(new Image[] { image }, newfile, 0, Njit.Pdf.CreatePDF.PageOrientation.عمودی, Njit.Pdf.CreatePDF.PageSize.OriginalSize);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.Pdf:
                    if (fileExtension != ".pdf")
                    {
                        fileExtension = ".pdf";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Pdf.CreatePDF.FromImages(new Image[] { image }, newfile, 0, Njit.Pdf.CreatePDF.PageOrientation.عمودی, Njit.Pdf.CreatePDF.PageSize.OriginalSize);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.OneMultiTiff:
                    if (fileExtension != ".tiff" && fileExtension != ".tif")
                    {
                        fileExtension = ".tiff";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Common.Helpers.ImageHelper.ConvertImage(image, System.Drawing.Imaging.ImageFormat.Tiff, GetImageCompression(imageCompression), newfile);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.Tiff:
                    if (fileExtension != ".tiff" && fileExtension != ".tif")
                    {
                        fileExtension = ".tiff";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Common.Helpers.ImageHelper.ConvertImage(image, System.Drawing.Imaging.ImageFormat.Tiff, GetImageCompression(imageCompression), newfile);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.JPEG:
                    if (fileExtension != ".jpg" && fileExtension != ".jpeg")
                    {
                        fileExtension = ".jpg";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Common.Helpers.ImageHelper.ConvertImage(image, System.Drawing.Imaging.ImageFormat.Jpeg, GetImageCompression(imageCompression), newfile);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.PNG:
                    if (fileExtension != ".png")
                    {
                        fileExtension = ".png";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Common.Helpers.ImageHelper.ConvertImage(image, System.Drawing.Imaging.ImageFormat.Png, GetImageCompression(imageCompression), newfile);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                case Njit.Program.ComponentOne.Enums.SaveFormats.BMP:
                    if (fileExtension != ".bmp")
                    {
                        fileExtension = ".bmp";
                        string newfile = GetUniqFileName(fileExtension);
                        Image  image   = Image.FromFile(file);
                        Njit.Common.Helpers.ImageHelper.ConvertImage(image, System.Drawing.Imaging.ImageFormat.Bmp, GetImageCompression(imageCompression), newfile);
                        image.Dispose();
                        file = newfile;
                    }
                    break;

                default:
                    throw new Exception();
                }
            }
            string fileNameToSaveInDatabase = Njit.Common.PublicMethods.ReplaceInvalidPathAndFileNameChars(personnelNumber, "-") + "-" + number.ToString() + fileExtension;
            string destinationFile          = null;

            if (Setting.Archive.ThisProgram.LoadedArchiveSettings.UseDatabase == false)
            {
                destinationFile = Path.Combine(dossier.FilesPathOrDatabaseName, fileNameToSaveInDatabase);
                Njit.Common.SystemUtility sysUtility = Njit.Program.Options.GetSystemUtility(DataAccess.ArchiveDataAccess.GetNewInstance().Connection, Setting.Program.ThisProgram.NetworkName, Setting.Program.ThisProgram.NetworkPort);
                int i = 1;
                while (sysUtility.FileExists(destinationFile))
                {
                    fileNameToSaveInDatabase = Njit.Common.PublicMethods.ReplaceInvalidPathAndFileNameChars(personnelNumber, "-") + "-" + number.ToString() + " (" + i.ToString() + ")" + fileExtension;
                    destinationFile          = Path.Combine(dossier.FilesPathOrDatabaseName, fileNameToSaveInDatabase);
                    i++;
                }
            }

            Model.Archive.Document document = Model.Archive.Document.GetNewInstance(personnelNumber, number.ToString(), fileNameToSaveInDatabase, archivetab.ID, attachToDossier, parentDocumentID, Controller.Archive.DocumentController.GetMaxDocumentIndex(personnelNumber) + 1);

            var archiveDc = Model.Archive.ArchiveDataClassesDataContext.GetNewInstance();

            Model.ArchiveDocument.DocumentDataClassesDataContext documentDc = null;
            if (Setting.Archive.ThisProgram.LoadedArchiveSettings.UseDatabase)
            {
                documentDc = Model.ArchiveDocument.DocumentDataClassesDataContext.GetNewInstance(Setting.Archive.ThisProgram.LoadedArchiveSettings.DocumentsPathOrDatabaseName);
            }

            archiveDc.Connection.Open();
            archiveDc.Transaction = archiveDc.Connection.BeginTransaction();
            if (documentDc != null)
            {
                documentDc.Connection.Open();
                documentDc.Transaction = documentDc.Connection.BeginTransaction();
            }
            try
            {
                archiveDc.Documents.InsertOnSubmit(document);
                archiveDc.SubmitChanges();
                archiveDc.Transaction.Commit();
                if (documentDc != null)
                {
                    Model.ArchiveDocument.Document doc = Model.ArchiveDocument.Document.GetNewInstance(document.ID, new System.Data.Linq.Binary(System.IO.File.ReadAllBytes(file)));
                    documentDc.Documents.InsertOnSubmit(doc);
                    documentDc.SubmitChanges();
                    documentDc.Transaction.Commit();
                }
            }
            catch (Exception ex)
            {
                archiveDc.Transaction.Rollback();
                if (documentDc != null)
                {
                    documentDc.Transaction.Rollback();
                }
                throw new Exception("خطا در ثبت اطلاعات" + "\r\n\r\n" + ex.Message);
            }
            finally
            {
                archiveDc.Connection.Close();
                if (documentDc != null)
                {
                    documentDc.Connection.Close();
                }
            }
            if (Setting.Archive.ThisProgram.LoadedArchiveSettings.UseDatabase == false)
            {
                FileStream serverFileStream = null;
                FileStream clientFileStream = null;
                try
                {
                    Njit.Common.SystemUtility sysUtility = Njit.Program.Options.GetSystemUtility(DataAccess.ArchiveDataAccess.GetNewInstance().Connection, Setting.Program.ThisProgram.NetworkName, Setting.Program.ThisProgram.NetworkPort);
                    if (!sysUtility.DirectoryExists(dossier.FilesPathOrDatabaseName))
                    {
                        sysUtility.CreateDirectory(dossier.FilesPathOrDatabaseName);
                    }
                    serverFileStream = sysUtility.CreateFile(destinationFile);
                    clientFileStream = File.OpenRead(file);
                    byte[] buffre    = new byte[1 * 1024 * 1024];
                    int    readCount = 0;
                    do
                    {
                        readCount = clientFileStream.Read(buffre, 0, buffre.Length);
                        serverFileStream.Write(buffre, 0, readCount);
                    }while (readCount > 0);
                    clientFileStream.Close();
                    serverFileStream.Close();
                    clientFileStream.Dispose();
                    serverFileStream.Dispose();
                }
                catch (Exception ex)
                {
                    if (clientFileStream != null)
                    {
                        clientFileStream.Dispose();
                    }
                    if (serverFileStream != null)
                    {
                        serverFileStream.Dispose();
                    }
                    throw new Exception("خطا در ذخیره فایل" + "\r\n" + file + "\r\n\r\n" + ex.Message);
                }
            }
            return(document);
        }