Browse Source

Add RemoveRestrictions method to DocumentOperation

Marcin Ziąbek 7 months ago
parent
commit
690d37d4e9

+ 22 - 1
Source/QuestPDF.UnitTests/DocumentOperationTests.cs

@@ -159,6 +159,28 @@ public class DocumentOperationTests
             .Save("operation-decrypt.pdf");
     }
     
+    [Test]
+    public void RemoveRestrictionsTest()
+    {
+        GenerateSampleDocument("remove-restrictions-input-not-encrypted.pdf", Colors.Red.Medium, 10);
+        
+        DocumentOperation
+            .LoadFile("remove-restrictions-input-not-encrypted.pdf")
+            .Encrypt(new DocumentOperation.Encryption256Bit()
+            {
+                UserPassword = string.Empty,
+                OwnerPassword = "owner_password",
+                AllowPrinting = false,
+                AllowContentExtraction = false
+            })
+            .Save("remove-restrictions-input-encrypted.pdf");
+        
+        DocumentOperation
+            .LoadFile("remove-restrictions-input-encrypted.pdf", "owner_password")
+            .RemoveRestrictions()
+            .Save("operation-remove-restrictions.pdf");
+    }
+    
     [Test]
     public void LoadEncryptedWithIncorrectPasswordTest()
     {
@@ -192,7 +214,6 @@ public class DocumentOperationTests
             .ExtendMetadata("<rdf:Description xmlns:dc=\"http://purl.org/dc/elements/1.1/\" rdf:about=\"\"></rdf:Description>")
             .Save("operation-extend-metadata.pdf");
     }
-
     
     private void GenerateSampleDocument(string filePath, Color color, int length)
     {

+ 12 - 0
Source/QuestPDF/Fluent/DocumentOperation.cs

@@ -385,6 +385,18 @@ public sealed class DocumentOperation
         return this;
     }
     
+    /// <summary>
+    /// Remove security restrictions associated with digitally signed PDF files.
+    /// This may be combined with Decrypt() operation to allow free editing of previously signed/encrypted files.
+    /// This option invalidates and disables any digital signatures but leaves their visual appearances intact.
+    /// </summary>
+    public DocumentOperation RemoveRestrictions()
+    {
+        Configuration.Decrypt = string.Empty;
+        Configuration.RemoveRestrictions = string.Empty;
+        return this;
+    }
+    
     /// <summary>
     /// Encrypts the document using 40-bit encryption, applying specified owner and user passwords along with defined permissions.
     /// </summary>

+ 1 - 0
Source/QuestPDF/Qpdf/JobConfiguration.cs

@@ -21,6 +21,7 @@ sealed class JobConfiguration
     [Name("encrypt")] public EncryptionSettings? Encrypt { get; set; } 
     [Name("allowWeakCrypto")] public string AllowWeakCrypto { get; set; } = string.Empty;
     [Name("decrypt")] public string? Decrypt { get; set; } 
+    [Name("removeRestrictions")] public string? RemoveRestrictions { get; set; } 
     [Name("linearize")] public string? Linearize { get; set; }
     [Name("newlineBeforeEndstream")] public string? NewlineBeforeEndstream { get; set; } = string.Empty;