예제 #1
0
        internal SettingsLogs Apply(PrintJob job)
        {
            SettingsLogs logs = new SettingsLogs();

            PropertyInfo[] properties = this.GetType().GetProperties();
            for (int t = properties.Length - 1; t >= 0; t--)
            {
                PropertyInfo prop     = properties[t];
                Type         propType = Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType;
                if (NativelySupportedTypes.Contains(propType) || typeof(Enum).IsAssignableFrom(propType))
                {
                    XmlIgnoreAttribute ignore = prop.GetCustomAttribute <XmlIgnoreAttribute>(true);
                    if (!ReferenceEquals(ignore, null))
                    {
                        continue;
                    }

                    object value = prop.GetValue(this);
                    if (!ReferenceEquals(value, null))
                    {
                        string fullName = Path + prop.Name;
                        string val      = value.ToString();
                        try
                        {
                            job.SetProfileSetting(fullName, val);
                            logs.AppliedSettings.Add(string.Concat(fullName, " = \"", val, "\""));
                        }catch (Exception ex)
                        {
                            logs.Errors.Add(string.Concat("Error while setting \"", fullName, "\" with value \"", val, "\": ", ex.Message));
                        }
                    }
                }
                else if (typeof(PdfCreatorSettingsBase).IsAssignableFrom(propType))
                {
                    PdfCreatorSettingsBase sub = prop.GetValue(this) as PdfCreatorSettingsBase;
                    if (!ReferenceEquals(sub, null))
                    {
                        SettingsLogs l = sub.Apply(job);
                        logs.Errors.AddRange(l.Errors);
                        logs.AppliedSettings.AddRange(l.AppliedSettings);
                    }
                }
                else
                {
                    System.Diagnostics.Debugger.Break();
                }
            }
            return(logs);
        }
        private bool CreatePdf(string destPdfFilePath, bool blocking, Action <PrintQueue> doPrint)
        {
            Queue  pdfQueue = new Queue();
            string backup   = null;

            try
            {
                pdfQueue.Initialize();
                string dir = System.IO.Path.GetDirectoryName(destPdfFilePath);
                if (!System.IO.Directory.Exists(dir))
                {
                    System.IO.Directory.CreateDirectory(dir);
                }

                if (System.IO.File.Exists(destPdfFilePath))
                {
                    if (Settings.BackupSettings.BackupExisting)
                    {
                        backup = RenameToBackup(destPdfFilePath, dir);
                    }
                    else
                    {
                        System.IO.File.Delete(destPdfFilePath);
                    }
                }

                if (string.IsNullOrWhiteSpace(UniquePrintJobName))
                {
                    UniquePrintJobName = Guid.NewGuid().ToString("D"); // Corolate the job going into the queue, with the job in PdfCreator's queue
                }
                using (PrintQueue pq = new PrintQueue(new LocalPrintServer(), Settings.PrinterQueueName))
                {
                    pq.CurrentJobSettings.Description = UniquePrintJobName;

                    doPrint(pq);
                }

                PrintJob job;

                do
                {
                    if (!pdfQueue.WaitForJob(Settings.WaitForJobTimeoutInSec))
                    {
                        throw new Exception(string.Concat("Timeout, printjob not found in queue within ", Settings.WaitForJobTimeoutInSec, " seconds."));
                    }

                    job = pdfQueue.NextJob;
                } while (job.PrintJobInfo.PrintJobName != UniquePrintJobName);

                logs = ApplyPdfForgeSettings(job);

                job.ConvertTo(destPdfFilePath);

                if (blocking)
                {
                    while (!job.IsFinished)
                    {
                        Thread.Sleep(100); // Nagging will only slow down the process
                    }
                }
                else
                {
                    return(true);
                }

                bool ok = job.IsFinished && job.IsSuccessful;

                if (ok && !string.IsNullOrEmpty(backup) && !Settings.BackupSettings.KeepBackupIfSuccessful)
                {
                    System.IO.File.Delete(backup);
                }

                return(ok);
            }
            catch
            {
                if (Settings.BackupSettings.RestoreBackupOnError && !string.IsNullOrEmpty(backup))
                {
                    try
                    {
                        if (System.IO.File.Exists(backup))
                        {
                            if (System.IO.File.Exists(destPdfFilePath))
                            {
                                System.IO.File.Delete(destPdfFilePath);
                            }
                            Thread.Sleep(100); // let filesystem catch up or else the rename will sometimes fail on some systems
                            System.IO.File.Move(backup, destPdfFilePath);
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Trace.WriteLine(string.Concat("Failed to restore backup: ", ex.ToString()));
                    }
                }
                throw;
            }
            finally
            {
                pdfQueue.ReleaseCom();
            }
        }