Xbase++ Requires Chilkat v11.7.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oPdf
LOCAL oJson
LOCAL oJsonOut
LOCAL oReader
LOCAL oEmpty
LOCAL oInfo
LOCAL oOpts
LOCAL oExtractOut
LOCAL oSb
LOCAL oOwner
LOCAL oOwnerInfo
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 two_page_memo.pdf
oPdf := CreateObject("Chilkat.Pdf")
nSuccess := oPdf:LoadFile("qa_data/pdf/two_page_memo.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
RETURN
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.
oPdf:UserPassword := ""
oPdf:OwnerPassword := "owner-secret"
oJson := CreateObject("Chilkat.JsonObject")
oJson:UpdateString("permissions.print", "lowQuality")
oJson:UpdateBool("permissions.copy", 0)
oJson:UpdateBool("permissions.modify", 0)
oJson:UpdateBool("permissions.annotate", 0)
// The permissions not mentioned (fillForms, assemble, accessibility) stay granted.
oJsonOut := CreateObject("Chilkat.JsonObject")
nSuccess := oPdf:Protection("encrypt", oJson, oJsonOut)
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
nSuccess := oPdf:WriteFile("qa_output/memo_restricted.pdf")
IF (nSuccess == 0)
? oPdf:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
RETURN
ENDIF
// Anyone can open the result, with the rights of a user.
oReader := CreateObject("Chilkat.Pdf")
nSuccess := oReader:LoadFile("qa_output/memo_restricted.pdf")
IF (nSuccess == 0)
? oReader:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
RETURN
ENDIF
oEmpty := CreateObject("Chilkat.JsonObject")
oInfo := CreateObject("Chilkat.JsonObject")
nSuccess := oReader:Protection("getInfo", oEmpty, oInfo)
IF (nSuccess == 0)
? oReader:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oEmpty:destroy()
oInfo:destroy()
RETURN
ENDIF
? "opened with: " + oInfo:StringOf("loaded.openedWith")
? "print: " + oInfo:StringOf("loaded.permissions.print")
? "copy: " + Str(oInfo:BoolOf("loaded.permissions.copy"))
? "modify: " + Str(oInfo:BoolOf("loaded.permissions.modify"))
// Chilkat honors the restrictions. Copying is not permitted, so the text cannot be extracted.
oOpts := CreateObject("Chilkat.JsonObject")
oExtractOut := CreateObject("Chilkat.JsonObject")
oSb := CreateObject("Chilkat.StringBuilder")
nSuccess := oReader:ExtractTextSb("text", oOpts, oSb, oExtractOut)
IF (nSuccess == 0)
? "Text extraction was refused: the PDF does not permit copying."
ENDIF
// The owner password lifts every restriction.
oOwner := CreateObject("Chilkat.Pdf")
oOwner:OwnerPassword := "owner-secret"
nSuccess := oOwner:LoadFile("qa_output/memo_restricted.pdf")
IF (nSuccess == 0)
? oOwner:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oEmpty:destroy()
oInfo:destroy()
oOpts:destroy()
oExtractOut:destroy()
oSb:destroy()
oOwner:destroy()
RETURN
ENDIF
oOwnerInfo := CreateObject("Chilkat.JsonObject")
nSuccess := oOwner:Protection("getInfo", oEmpty, oOwnerInfo)
IF (nSuccess == 0)
? oOwner:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oEmpty:destroy()
oInfo:destroy()
oOpts:destroy()
oExtractOut:destroy()
oSb:destroy()
oOwner:destroy()
oOwnerInfo:destroy()
RETURN
ENDIF
? "opened with: " + oOwnerInfo:StringOf("loaded.openedWith")
nSuccess := oOwner:ExtractTextSb("text", oOpts, oSb, oExtractOut)
IF (nSuccess == 0)
? oOwner:LastErrorText
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oEmpty:destroy()
oInfo:destroy()
oOpts:destroy()
oExtractOut:destroy()
oSb:destroy()
oOwner:destroy()
oOwnerInfo:destroy()
RETURN
ENDIF
? "characters extracted with the owner password: " + Str(oSb: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
oPdf:destroy()
oJson:destroy()
oJsonOut:destroy()
oReader:destroy()
oEmpty:destroy()
oInfo:destroy()
oOpts:destroy()
oExtractOut:destroy()
oSb:destroy()
oOwner:destroy()
oOwnerInfo:destroy()