Sample code for 30+ languages & platforms
Zig 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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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

    const pdf = try chilkat.Pdf.init();
    defer pdf.deinit();

    // 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.setOwnerPassword("owner-secret");
    pdf.loadFile("qa_data/pdf/no_copy_permission.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    const json_out = try chilkat.JsonObject.init();
    defer json_out.deinit();
    pdf.protection("getInfo", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("may copy, before: {}\n", .{json_out.boolOf("loaded.permissions.copy")});
    std.debug.print("password needed to open: {}\n", .{json_out.boolOf("loaded.userPasswordRequired")});

    pdf.protection("removeRestrictions", json, json_out) catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        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.
    std.debug.print("will be saved encrypted: {}\n", .{json_out.boolOf("pending.encrypted")});

    pdf.writeFile("qa_output/leaflet_unrestricted.pdf") catch {
        std.debug.print("{s}\n", .{try pdf.getLastErrorText(alloc)});
        return;
    };

    // Anyone can now extract the text.
    const reader = try chilkat.Pdf.init();
    defer reader.deinit();
    reader.loadFile("qa_output/leaflet_unrestricted.pdf") catch {
        std.debug.print("{s}\n", .{try reader.getLastErrorText(alloc)});
        return;
    };

    const opts = try chilkat.JsonObject.init();
    defer opts.deinit();
    const extract_out = try chilkat.JsonObject.init();
    defer extract_out.deinit();
    const sb = try chilkat.StringBuilder.init();
    defer sb.deinit();
    reader.extractTextSb("text", opts, sb, extract_out) catch {
        std.debug.print("{s}\n", .{try reader.getLastErrorText(alloc)});
        return;
    };

    std.debug.print("characters extracted: {d}\n", .{sb.getLength()});

    // Sample output:

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