Sample code for 30+ languages & platforms
.NET Core C# Requires Chilkat v11.7.0+

Remove the Restrictions from a PDF

See more PDF Protection Examples

Demonstrates the removeRestrictions operation, which grants every permission. A PDF that needs a password to open stays encrypted and keeps that password. A PDF with no user password, like the one here, has nothing left to protect and is saved unencrypted, the same as removeEncryption. The operation needs owner rights, so the PDF is loaded with its owner password.

Chilkat .NET Core C# Downloads

.NET Core C#
bool success = false;

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  The PDF used in this example is available at no_copy_permission.pdf

Chilkat.Pdf pdf = new Chilkat.Pdf();

//  This PDF opens without a password, but copying text from it is not permitted.
//  Removing the restrictions needs owner rights: load with the owner password.
pdf.OwnerPassword = "owner-secret";
success = pdf.LoadFile("qa_data/pdf/no_copy_permission.pdf");
if (success == false) {
    Debug.WriteLine(pdf.LastErrorText);
    return;
}

Chilkat.JsonObject json = new Chilkat.JsonObject();
Chilkat.JsonObject jsonOut = new Chilkat.JsonObject();
success = pdf.Protection("getInfo",json,jsonOut);
if (success == false) {
    Debug.WriteLine(pdf.LastErrorText);
    return;
}

Debug.WriteLine("may copy, before: " + Convert.ToString(jsonOut.BoolOf("loaded.permissions.copy")));
Debug.WriteLine("password needed to open: " + Convert.ToString(jsonOut.BoolOf("loaded.userPasswordRequired")));

success = pdf.Protection("removeRestrictions",json,jsonOut);
if (success == false) {
    Debug.WriteLine(pdf.LastErrorText);
    return;
}

//  With no user password and nothing restricted there is nothing left to protect, so the PDF
//  will be saved unencrypted.  A PDF that needs a password to open would stay encrypted and
//  keep its password.
Debug.WriteLine("will be saved encrypted: " + Convert.ToString(jsonOut.BoolOf("pending.encrypted")));

success = pdf.WriteFile("qa_output/leaflet_unrestricted.pdf");
if (success == false) {
    Debug.WriteLine(pdf.LastErrorText);
    return;
}

//  Anyone can now extract the text.
Chilkat.Pdf reader = new Chilkat.Pdf();
success = reader.LoadFile("qa_output/leaflet_unrestricted.pdf");
if (success == false) {
    Debug.WriteLine(reader.LastErrorText);
    return;
}

Chilkat.JsonObject opts = new Chilkat.JsonObject();
Chilkat.JsonObject extractOut = new Chilkat.JsonObject();
Chilkat.StringBuilder sb = new Chilkat.StringBuilder();
success = reader.ExtractTextSb("text",opts,sb,extractOut);
if (success == false) {
    Debug.WriteLine(reader.LastErrorText);
    return;
}

Debug.WriteLine("characters extracted: " + Convert.ToString(sb.Length));

//  Sample output:

//  may copy, before: False
//  password needed to open: False
//  will be saved encrypted: False
//  characters extracted: 216