Exemplo n.º 1
0
        public void Unlock(PdfOptions args)
        {
            OpenAsPdf(args.Source, args.PasswordArray);

            var tempTarget = Path.GetTempFileName();
            var reader     = new PdfReader(args.Source, new ReaderProperties().SetPassword(args.PasswordArray));
            var writer     = new PdfWriter(tempTarget);

            try
            {
                if (args.ForceDecryption)
                {
                    reader.SetUnethicalReading(true);
                }

                var doc = new PdfDocument(reader, writer);
                doc.SetCloseReader(true);
                doc.SetCloseWriter(true);

                doc.Close();

                File.Copy(tempTarget, args.Target, true);
            }
            finally
            {
                reader.Close();
                writer.Close();

                File.Delete(tempTarget);
            }
        }
Exemplo n.º 2
0
        public static bool TryParse(dynamic request, out PdfOptions result)
        {
            result = null;
            var values = request as IDictionary <string, object>;

            if (values == null)
            {
                result = null;
                return(false);
            }

            try
            {
                result = new PdfOptions
                {
                    Source          = values.ContainsKey("source") ? (string)values["source"] : null,
                    Target          = values.ContainsKey("target") ? (string)values["target"] : null,
                    Password        = values.ContainsKey("password") ? (string)values["password"] : null,
                    UserPassword    = values.ContainsKey("userPassword") ? (string)values["userPassword"] : null,
                    OwnerPassword   = values.ContainsKey("ownerPassword") ? (string)values["ownerPassword"] : null,
                    Permissions     = values.ContainsKey("permissions") ? (int)values["permissions"] : (int?)null,
                    EncryptionLevel = values.ContainsKey("encryptionLevel") ? (int)values["encryptionLevel"] : (int?)null,
                    ForceDecryption = values.ContainsKey("forceDecryption") ? (bool)values["forceDecryption"] : false
                };
                return(true);
            }
            catch (InvalidCastException)
            {
                return(false);
            }
        }
Exemplo n.º 3
0
        public void Protect(PdfOptions args)
        {
            var tempTarget = Path.GetTempFileName();

            var reader      = new PdfReader(args.Source, new ReaderProperties().SetPassword(args.PasswordArray));
            var permissions = args.Permissions ?? unchecked ((int)reader.GetPermissions());
            var encryption  = args.EncryptionLevel ?? reader.GetCryptoMode();

            // reader.GetCryptoMode() returns -1 when no encryption is set and this is not a valid encryption.
            // use AES 128 encryption by default.
            encryption = encryption != -1 ? encryption : EncryptionConstants.ENCRYPTION_AES_128;

            var writer = new PdfWriter(tempTarget, new WriterProperties().SetStandardEncryption(
                                           args.UserPasswordArray,
                                           args.OwnerPasswordArray ?? args.UserPasswordArray,
                                           permissions,
                                           encryption)
                                       );

            try
            {
                var doc = new PdfDocument(reader, writer);
                doc.SetCloseReader(true);
                doc.SetCloseWriter(true);

                doc.Close();

                File.Copy(tempTarget, args.Target, true);
            }
            finally
            {
                reader.Close();
                writer.Close();

                File.Delete(tempTarget);
            }
        }