Exemplo n.º 1
0
        protected override void DoProcessPdf(Job job)
        {
            var pdfFile       = job.TempOutputFiles.First();
            var processedFile = Path.ChangeExtension(pdfFile, "_processed.pdf");
            var version       = DeterminePdfVersionForPdfStamper(job.Profile);

            var stamperBundle = StamperCreator.CreateStamperAccordingToEncryptionAndSignature(pdfFile, processedFile, job.Profile, version);
            var stamper       = stamperBundle.Item1;

            var resources = new List <IDisposable>();

            resources.Add(stamperBundle.Item2);
            resources.Add(stamperBundle.Item3);

            try
            {
                //Encryption before adding Background and Signing!
                var encrypter = new Encrypter();
                encrypter.SetEncryption(stamper, job.Profile, job.Passwords);

                var metadataUpdater = new XmpMetadataUpdater();
                metadataUpdater.UpdateXmpMetadata(stamper, job.Profile);

                var backgroundAdder = new BackgroundAdder();
                backgroundAdder.AddBackground(stamper, job.Profile, resources);

                //Signing after adding background and update metadata
                var signer = new ITextSigner();
                signer.SignPdfFile(stamper, job.Profile, job.Passwords, job.Accounts);
            }
            finally
            {
                try
                {
                    stamper?.Dispose();
                }
                catch (Exception ex)
                {
                    Logger.Warn($"Could not close iText pdf stamper.\r\n{ex}");
                }

                foreach (var resource in resources)
                {
                    resource.Dispose();
                }
            }

            //Set the final output pdf
            job.TempOutputFiles.Clear();
            job.TempOutputFiles.Add(processedFile);
        }
Exemplo n.º 2
0
        protected override void DoProcessPdf(Job job)
        {
            string preProcessFile = null;

            try
            {
                var pdfFile = job.TempOutputFiles.First();
                preProcessFile = MoveFileToPreProcessFile(pdfFile, "PreProcess");
                var version = DeterminePdfVersionForPdfStamper(job.Profile);
                using (var stamper = StamperCreator.CreateStamperAccordingToEncryptionAndSignature(preProcessFile, pdfFile, job.Profile, version))
                {
                    var encrypter = new Encrypter();
                    encrypter.SetEncryption(stamper, job.Profile, job.Passwords);
                    //Encryption before adding Background and Signing!

                    var metadataUpdater = new XmpMetadataUpdater();
                    metadataUpdater.UpdateXmpMetadata(stamper, job.Profile);

                    var backgroundAdder = new BackgroundAdder();
                    backgroundAdder.AddBackground(stamper, job.Profile);

                    var signer = new ITextSigner();
                    signer.SignPdfFile(stamper, job.Profile, job.Passwords);
                    //Signing after adding background and update metadata

                    stamper.Close();
                }
            }
            finally
            {
                //delete copy of original file
                if (!string.IsNullOrEmpty(preProcessFile))
                {
                    if (File.Exists(preProcessFile))
                    {
                        File.Delete(preProcessFile);
                    }
                }
            }
        }