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

Restrict Printing, Copying and Editing of a PDF

See more PDF Protection Examples

Demonstrates a PDF that opens without a password but limits what a reader may do with it. The user password is left empty, the owner password protects the restrictions, and the permissions member of the JSON says what is denied. Chilkat honors these permissions itself: with copying denied, text extraction is refused unless the PDF is loaded with its owner password. An empty owner password is refused when anything is restricted, because anyone opening the file would then have owner rights.

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loPdf
LOCAL loJson
LOCAL loJsonOut
LOCAL loReader
LOCAL loEmpty
LOCAL loInfo
LOCAL loOpts
LOCAL loExtractOut
LOCAL loSb
LOCAL loOwner
LOCAL loOwnerInfo

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 two_page_memo.pdf

loPdf = CreateObject('Chilkat.Pdf')
lnSuccess = loPdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (lnSuccess = 0) THEN
    ? loPdf.LastErrorText
    RELEASE loPdf
    CANCEL
ENDIF

*  No user password: the PDF opens without a prompt.  The owner password is what protects the
*  restrictions, so it must not be empty when anything is restricted.
loPdf.UserPassword = ""
loPdf.OwnerPassword = "owner-secret"

loJson = CreateObject('Chilkat.JsonObject')
loJson.UpdateString("permissions.print","lowQuality")
loJson.UpdateBool("permissions.copy",0)
loJson.UpdateBool("permissions.modify",0)
loJson.UpdateBool("permissions.annotate",0)
*  The permissions not mentioned (fillForms, assemble, accessibility) stay granted.

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

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

*  Anyone can open the result, with the rights of a user.
loReader = CreateObject('Chilkat.Pdf')
lnSuccess = loReader.LoadFile("qa_output/memo_restricted.pdf")
IF (lnSuccess = 0) THEN
    ? loReader.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loReader
    CANCEL
ENDIF

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

? "opened with: " + loInfo.StringOf("loaded.openedWith")
? "print: " + loInfo.StringOf("loaded.permissions.print")
? "copy: " + STR(loInfo.BoolOf("loaded.permissions.copy"))
? "modify: " + STR(loInfo.BoolOf("loaded.permissions.modify"))

*  Chilkat honors the restrictions.  Copying is not permitted, so the text cannot be extracted.
loOpts = CreateObject('Chilkat.JsonObject')
loExtractOut = CreateObject('Chilkat.JsonObject')
loSb = CreateObject('Chilkat.StringBuilder')
lnSuccess = loReader.ExtractTextSb("text",loOpts,loSb,loExtractOut)
IF (lnSuccess = 0) THEN
    ? "Text extraction was refused: the PDF does not permit copying."
ENDIF

*  The owner password lifts every restriction.
loOwner = CreateObject('Chilkat.Pdf')
loOwner.OwnerPassword = "owner-secret"
lnSuccess = loOwner.LoadFile("qa_output/memo_restricted.pdf")
IF (lnSuccess = 0) THEN
    ? loOwner.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loReader
    RELEASE loEmpty
    RELEASE loInfo
    RELEASE loOpts
    RELEASE loExtractOut
    RELEASE loSb
    RELEASE loOwner
    CANCEL
ENDIF

loOwnerInfo = CreateObject('Chilkat.JsonObject')
lnSuccess = loOwner.Protection("getInfo",loEmpty,loOwnerInfo)
IF (lnSuccess = 0) THEN
    ? loOwner.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loReader
    RELEASE loEmpty
    RELEASE loInfo
    RELEASE loOpts
    RELEASE loExtractOut
    RELEASE loSb
    RELEASE loOwner
    RELEASE loOwnerInfo
    CANCEL
ENDIF

? "opened with: " + loOwnerInfo.StringOf("loaded.openedWith")
lnSuccess = loOwner.ExtractTextSb("text",loOpts,loSb,loExtractOut)
IF (lnSuccess = 0) THEN
    ? loOwner.LastErrorText
    RELEASE loPdf
    RELEASE loJson
    RELEASE loJsonOut
    RELEASE loReader
    RELEASE loEmpty
    RELEASE loInfo
    RELEASE loOpts
    RELEASE loExtractOut
    RELEASE loSb
    RELEASE loOwner
    RELEASE loOwnerInfo
    CANCEL
ENDIF

? "characters extracted with the owner password: " + STR(loSb.Length)

*  Sample output:

*  opened with: emptyPassword
*  print: lowQuality
*  copy: False
*  modify: False
*  Text extraction was refused: the PDF does not permit copying.
*  opened with: owner
*  characters extracted with the owner password: 1048

RELEASE loPdf
RELEASE loJson
RELEASE loJsonOut
RELEASE loReader
RELEASE loEmpty
RELEASE loInfo
RELEASE loOpts
RELEASE loExtractOut
RELEASE loSb
RELEASE loOwner
RELEASE loOwnerInfo