Esempio n. 1
0
        public DirectoryWatcher(DirectoryWatcherConfiguration configuration)
        {
            if (!String.IsNullOrWhiteSpace(configuration.WatcherDirectory) && Directory.Exists(configuration.WatcherDirectory))
            {
                this._fileSystemWatcher = new FileSystemWatcher(configuration.WatcherDirectory)
                {
                    NotifyFilter          = NotifyFilters.DirectoryName,
                    IncludeSubdirectories = false,
                    InternalBufferSize    = configuration.InternalBufferSize
                };
                this.DoSaveBackups = configuration.DoSaveBackups;
                this.DoSaveErrors  = configuration.DoSaveErrors;
                this._fileSystemWatcher.Created += DirectoryWatcher_OnCreated;

                #region Create subdirecories used for processing files

                this._errorsPath = System.IO.Path.Combine(configuration.WatcherDirectory, "errors");

                if (!Directory.Exists(this._errorsPath))
                {
                    Directory.CreateDirectory(this._errorsPath, Directory.GetAccessControl(configuration.WatcherDirectory));
                    EventLogManager.WriteInformation(String.Format("Errors directory created at {0}", this._errorsPath));
                }

                this._backupsPath = System.IO.Path.Combine(configuration.WatcherDirectory, "backups");

                if (!Directory.Exists(this._backupsPath))
                {
                    Directory.CreateDirectory(this._backupsPath, Directory.GetAccessControl(configuration.WatcherDirectory));
                    EventLogManager.WriteInformation(String.Format("Backups directory created at {0}", this._backupsPath));
                }

                this._workPath = System.IO.Path.Combine(configuration.WatcherDirectory, "work");

                if (!Directory.Exists(this._workPath))
                {
                    Directory.CreateDirectory(this._workPath, Directory.GetAccessControl(configuration.WatcherDirectory));
                    EventLogManager.WriteInformation(String.Format("Work directory created at {0}", this._workPath));
                }

                #endregion
            }
            else
            {
                EventLogManager.WriteError(new Exception(String.Format("Path {0} does not exist.", configuration.WatcherDirectory)));
            }
        }
Esempio n. 2
0
        private void FillFormFields(string directoryPath, PdfMerge pdfMerge)
        {
            if (pdfMerge != null && pdfMerge.Pdfs != null)
            {
                string       formPath;
                string       newFilePath;
                PdfDocument  document = null;
                PdfAcroForm  form;
                PdfFormField pdfFormField;

                foreach (var pdf in pdfMerge.Pdfs)
                {
                    try
                    {
                        formPath    = System.IO.Path.Combine(directoryPath, pdf.Filename);
                        newFilePath = System.IO.Path.Combine(
                            directoryPath,
                            String.Format("{0}.{1}", String.Format("{0}{1}", System.IO.Path.GetFileNameWithoutExtension(pdf.Filename), "_Revised"), System.IO.Path.GetExtension(pdf.Filename)));
                        document = new PdfDocument(Helpers.GetPdfReader(formPath), new PdfWriter(newFilePath));

                        form = PdfAcroForm.GetAcroForm(document, true);
                        if (pdf.Fields != null && pdf.Fields.Count > 0)
                        {
                            foreach (var field in pdf.Fields)
                            {
                                if (field.Value != null)
                                {
                                    pdfFormField = form.GetField(field.Name);

                                    if (pdfFormField != null)
                                    {
                                        form.GetField(field.Name).SetValue(field.Value);

                                        //form.GetField(field.Name).SetFontSize(0);
                                    }
                                    else
                                    {
                                        EventLogManager.WriteWarning(String.Format("Field '{0}' does not exist in '{1}'", field.Name, pdf.Filename));
                                        //document.Close();
                                        //throw new Exception(String.Format("Field '{0}' does not exist in '{1}'", field.Name, pdf.Filename));
                                    }
                                }
                            }
                        }

                        //The below will make sure the fields are not editable in
                        //the output PDF.
                        form.FlattenFields();  // Maybe make this an option in the json so we can partialy fill out forms at some point?
                        //document.Close();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (document != null && !document.IsClosed())
                        {
                            document.Close();
                        }
                    }

                    // Now rename the new one back to the old name
                    File.Delete(formPath);
                    File.Move(newFilePath, formPath);
                }
            }
        }
Esempio n. 3
0
        private void ProcessDirectory(string directoryPath)
        {
            // DON'T TOUCH THE BACKUPS, ERRORS AND WORK DIRECTORIES.  Just in case they were made or renamed after the fact for some reason
            if (directoryPath != this._errorsPath && directoryPath != this._backupsPath && directoryPath != this._workPath)
            {
                string pdfJsonPath = System.IO.Path.Combine(directoryPath, "pdf.json");

                if (File.Exists(pdfJsonPath))
                {
                    string workPath = System.IO.Path.Combine(this._workPath, System.IO.Path.GetFileName(directoryPath));

                    try
                    {
                        CopyToDirectory(directoryPath, workPath);

                        PdfMerge pdfMerge = null;

                        string jsonPath = System.IO.Path.Combine(workPath, "pdf.json");
                        using (StreamReader r = Helpers.GetStreamReader(jsonPath))
                        {
                            string json = r.ReadToEnd();
                            pdfMerge = JsonConvert.DeserializeObject <PdfMerge>(json);
                        }

                        FillFormFields(workPath, pdfMerge);
                        MergePdfs(workPath, pdfMerge);
                        //NumberPages(workPath, pdfMerge);
                        FinishPdf(workPath, pdfMerge);

                        // Move original to backups directory
                        if (DoSaveBackups)
                        {
                            string backupsPath = System.IO.Path.Combine(this._backupsPath, String.Format("{0}_{1}", System.IO.Path.GetFileName(directoryPath), DateTime.Now.ToString("yyyyMMddHHmmss")));
                            Directory.Move(directoryPath, backupsPath);
                        }
                        else
                        {
                            Directory.Delete(directoryPath, true);
                        }
                    }
                    catch (Exception ex)
                    {
                        EventLogManager.WriteError(ex);

                        if (DoSaveErrors)
                        {
                            // Move original to errors directory
                            string errorsPath = System.IO.Path.Combine(this._errorsPath, String.Format("{0}_{1}", System.IO.Path.GetFileName(directoryPath), DateTime.Now.ToString("yyyyMMddHHmmss")));
                            Directory.Move(directoryPath, errorsPath);
                        }
                        else
                        {
                            Directory.Delete(directoryPath, true);
                        }
                    }

                    // Delete work directory
                    Directory.Delete(workPath, true);
                }
                else
                {
                    EventLogManager.WriteInformation(String.Format("No pdf.json file.  {0} skipped.", directoryPath));
                }
            }
        }