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

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL loReader
LOCAL loOpts
LOCAL loExtractOut
LOCAL loSb

lnSuccess = 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 no_copy_permission.pdf

loPdf = CreateObject('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.
loPdf.OwnerPassword = "owner-secret"
lnSuccess = loPdf.LoadFile("qa_data/pdf/no_copy_permission.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    CANCEL
ENDIF

loJson = CreateObject('Chilkat.JsonObject')
loJsonOut = CreateObject('Chilkat.JsonObject')
lnSuccess = loPdf.Protection("getInfo",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

? "may copy, before: " + STR(loJsonOut.BoolOf("loaded.permissions.copy"))
? "password needed to open: " + STR(loJsonOut.BoolOf("loaded.userPasswordRequired"))

lnSuccess = loPdf.Protection("removeRestrictions",loJson,loJsonOut)
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

*  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.
? "will be saved encrypted: " + STR(loJsonOut.BoolOf("pending.encrypted"))

lnSuccess = loPdf.WriteFile("qa_output/leaflet_unrestricted.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    CANCEL
ENDIF

*  Anyone can now extract the text.
loReader = CreateObject('Chilkat.Pdf')
lnSuccess = loReader.LoadFile("qa_output/leaflet_unrestricted.pdf")
IF (lnSuccess = 0) THEN
    ? loReader.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loReader
    CANCEL
ENDIF

loOpts = CreateObject('Chilkat.JsonObject')
loExtractOut = CreateObject('Chilkat.JsonObject')
loSb = CreateObject('Chilkat.StringBuilder')
lnSuccess = loReader.ExtractTextSb("text",loOpts,loSb,loExtractOut)
IF (lnSuccess = 0) THEN
    ? loReader.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loReader
    RELEASE loOpts
    RELEASE loExtractOut
    RELEASE loSb
    CANCEL
ENDIF

? "characters extracted: " + STR(loSb.Length)

*  Sample output:

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

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loReader
RELEASE loOpts
RELEASE loExtractOut
RELEASE loSb