Exemplo n.º 1
0
        public void PreviewChecks(AVCheckPrintingModel checkInfo, int formNum)
        {
            if (checkInfo == null)
            {
                throw new ArgumentNullException("checkInfo");
            }
            if (formNum < 1)
            {
                throw new ArgumentException("FormNum has to be greater than zero", "formNum");
            }

            var checkForms = this.GetAvailableCheckForms();
            //Debug.Assert(checkForms.ContainsKey(formNum));
            var filePath = checkForms[formNum];

            var wordDocumentPath = new FileInfo(filePath).FullName;

            //Debug.Assert(wordDocumentPath == filePath);

            if (!File.Exists(wordDocumentPath))
            {
                throw new ArgumentException("Word Template not found: " + wordDocumentPath);
            }

            using (var wordAutomation = new WordAutomation())
            {
                wordAutomation.FillOutCheckTemplate(wordDocumentPath, checkInfo);
            }
        }
Exemplo n.º 2
0
        private void CreateTemplates(object sender, DoWorkEventArgs e)
        {
            var worker = sender as BackgroundWorker;

            // Список для поиска
            var toFind = new List <string>
            {
                "<Description>",
                "<GIP>", "<Engineer>",
                "<Employer>", "<Year>",
                "<NumProj>", "<Controller>", "<Organization>",
                "<Resolution>", "<Customer>"
            };
            var assembly = Assembly.GetExecutingAssembly();

            worker?.ReportProgress(0, ModPlusAPI.Language.GetItem(LangItem, "h21"));
            var wordAutomation = new WordAutomation();

            // Запускаем Word
            wordAutomation.CreateWordApplication();

            // Проходим по объектам kap
            foreach (var kapDoc in _kapDocs.Where(x => x.Create))
            {
                if (worker != null && worker.CancellationPending)
                {
                    wordAutomation.CloseWordApp();
                    break;
                }

                worker?.ReportProgress(0, ModPlusAPI.Language.GetItem(LangItem, "h22") + ": " + ModPlusAPI.Language.GetItem(LangItem, "h15") + ": " + kapDoc.Name);

                // Временный файл
                var tmp = Path.GetTempFileName();
                var templateFullPath = Path.ChangeExtension(tmp, ".docx");

                // Имя внедренного ресурса
                var resourceName = "mpDocTemplates.Resources.Kap." + kapDoc.Name + ".docx";

                // Читаем ресурс в поток и сохраняем как временный файл
                using (Stream stream = assembly.GetManifestResourceStream(resourceName))
                {
                    SaveStreamToFile(templateFullPath, stream);
                }

                try
                {
                    using (FlatDocument flatDocument = new FlatDocument(templateFullPath))
                    {
                        // Помещаем в список на удаление
                        _fileToDelete?.Add(tmp);
                        _fileToDelete?.Add(templateFullPath);
                        for (var i = 0; i < toFind.Count; i++)
                        {
                            if (worker != null && worker.CancellationPending)
                            {
                                wordAutomation.CloseWordApp();
                                break;
                            }

                            worker?.ReportProgress(
                                Convert.ToInt32((decimal)i / toFind.Count * 100),
                                ModPlusAPI.Language.GetItem(LangItem, "h22") + ": " + ModPlusAPI.Language.GetItem(LangItem, "h15") + ": " + kapDoc.Name);
                            flatDocument.FindAndReplace(toFind[i], _toReplace[i]);
                            Thread.Sleep(50);
                        }
                    }

                    // Создаем документ, используя временный файл
                    worker?.ReportProgress(0,
                                           ModPlusAPI.Language.GetItem(LangItem, "h23") + ": " + ModPlusAPI.Language.GetItem(LangItem, "h15") + ": " + kapDoc.Name);
                    wordAutomation.CreateWordDoc(templateFullPath, true);
                }
                catch (System.Exception exception)
                {
                    // Если словили ошибку, то закрываем ворд
                    wordAutomation.CloseWordApp();
                    _error    = exception;
                    _hasError = true;
                }
            }

            // Проходим по объектам Лин
            foreach (var linDoc in _linDocs.Where(x => x.Create))
            {
                if (worker != null && worker.CancellationPending)
                {
                    wordAutomation.CloseWordApp();
                    break;
                }

                worker?.ReportProgress(0,
                                       ModPlusAPI.Language.GetItem(LangItem, "h22") + ": " + ModPlusAPI.Language.GetItem(LangItem, "h16") + ": " + linDoc.Name);

                // Временный файл
                var tmp = Path.GetTempFileName();
                var templateFullPath = Path.ChangeExtension(tmp, ".docx");

                // Имя внедренного ресурса
                var resourceName = "mpDocTemplates.Resources.Lin." + linDoc.Name + ".docx";

                // Читаем ресурс в поток и сохраняем как временный файл
                using (var stream = assembly.GetManifestResourceStream(resourceName))
                {
                    SaveStreamToFile(templateFullPath, stream);
                }

                try
                {
                    using (FlatDocument flatDocument = new FlatDocument(templateFullPath))
                    {
                        // Помещаем в список на удаление
                        _fileToDelete?.Add(tmp);
                        _fileToDelete?.Add(templateFullPath);
                        for (var i = 0; i < toFind.Count; i++)
                        {
                            if (worker != null && worker.CancellationPending)
                            {
                                wordAutomation.CloseWordApp();
                                break;
                            }

                            worker?.ReportProgress(
                                Convert.ToInt32((decimal)i / toFind.Count * 100),
                                ModPlusAPI.Language.GetItem(LangItem, "h22") + ": " + ModPlusAPI.Language.GetItem(LangItem, "h16") + ": " + linDoc.Name);
                            flatDocument.FindAndReplace(toFind[i], _toReplace[i]);
                            Thread.Sleep(50);
                        }
                    }

                    // Создаем документ, используя временный файл
                    worker?.ReportProgress(0,
                                           ModPlusAPI.Language.GetItem(LangItem, "h23") + ": " + ModPlusAPI.Language.GetItem(LangItem, "h16") + ": " + linDoc.Name);
                    wordAutomation.CreateWordDoc(templateFullPath, true);
                }
                catch (System.Exception exception)
                {
                    // Если словили ошибку, то закрываем ворд
                    wordAutomation.CloseWordApp();
                    _error    = exception;
                    _hasError = true;
                }
            }

            // Делаем word видимым
            wordAutomation.MakeWordAppVisible();
        }