Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.7.0+

Remove the Password and Encryption from a PDF

See more PDF Protection Examples

Demonstrates the removeEncryption operation, which saves the PDF with no encryption at all: no password, no restrictions. It needs owner rights, so the PDF is loaded with its owner password. As with every Protection operation, the change is applied when the file is written.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL oReader
LOCAL oInfo

nSuccess := 0

//  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 encrypted_user_password.pdf

oPdf := CreateObject("Chilkat.Pdf")

//  Removing the encryption needs owner rights: load with the owner password.
oPdf:OwnerPassword := "owner-secret"
nSuccess := oPdf:LoadFile("qa_data/pdf/encrypted_user_password.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    RETURN
ENDIF

oJson := CreateObject("Chilkat.JsonObject")
oJsonOut := CreateObject("Chilkat.JsonObject")
nSuccess := oPdf:Protection("getInfo", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

? "encrypted, as loaded: " + Str(oJsonOut:BoolOf("loaded.encrypted"))
? "algorithm: " + oJsonOut:StringOf("loaded.algorithm")

nSuccess := oPdf:Protection("removeEncryption", oJson, oJsonOut)
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

? "will be saved encrypted: " + Str(oJsonOut:BoolOf("pending.encrypted"))

nSuccess := oPdf:WriteFile("qa_output/minutes_unprotected.pdf")
IF (nSuccess == 0)
    ? oPdf:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    RETURN
ENDIF

//  The result is an ordinary PDF that opens without a password.
oReader := CreateObject("Chilkat.Pdf")
oInfo := CreateObject("Chilkat.JsonObject")
nSuccess := oReader:PreLoadCheck("qa_output/minutes_unprotected.pdf", oInfo)
IF (nSuccess == 0)
    ? oReader:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oReader:destroy()
    oInfo:destroy()
    RETURN
ENDIF

? "encrypted: " + Str(oInfo:BoolOf("encrypted"))
nSuccess := oReader:LoadFile("qa_output/minutes_unprotected.pdf")
IF (nSuccess == 0)
    ? oReader:LastErrorText
    oPdf:destroy()
    oJson:destroy()
    oJsonOut:destroy()
    oReader:destroy()
    oInfo:destroy()
    RETURN
ENDIF

? "pages: " + Str(oReader:NumPages)

//  Sample output:

//  encrypted, as loaded: True
//  algorithm: rc4-128
//  will be saved encrypted: False
//  encrypted: False
//  pages: 1

oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oInfo:destroy()