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

B4X
Dim success As Boolean = False

'  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

Dim pdf As ChilkatPdf
pdf.Initialize("pdf")
success = pdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
If success = False Then
    Log(pdf.LastErrorText)
    Return
End If


'  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.
pdf.UserPassword = ""
pdf.OwnerPassword = "owner-secret"

Dim json As ChilkatJsonObject
json.Initialize
json.UpdateString("permissions.print", "lowQuality")
json.UpdateBool("permissions.copy", False)
json.UpdateBool("permissions.modify", False)
json.UpdateBool("permissions.annotate", False)
'  The permissions not mentioned (fillForms, assemble, accessibility) stay granted.

Dim jsonOut As ChilkatJsonObject
jsonOut.Initialize
success = pdf.Protection("encrypt", json, jsonOut)
If success = False Then
    Log(pdf.LastErrorText)
    Return
End If

success = pdf.WriteFile("qa_output/memo_restricted.pdf")
If success = False Then
    Log(pdf.LastErrorText)
    Return
End If


'  Anyone can open the result, with the rights of a user.
Dim reader As ChilkatPdf
reader.Initialize("reader")
success = reader.LoadFile("qa_output/memo_restricted.pdf")
If success = False Then
    Log(reader.LastErrorText)
    Return
End If

Dim empty As ChilkatJsonObject
empty.Initialize
Dim info As ChilkatJsonObject
info.Initialize
success = reader.Protection("getInfo", empty, info)
If success = False Then
    Log(reader.LastErrorText)
    Return
End If

Log("opened with: " & info.StringOf("loaded.openedWith"))
Log("print: " & info.StringOf("loaded.permissions.print"))
Log("copy: " & info.BoolOf("loaded.permissions.copy"))
Log("modify: " & info.BoolOf("loaded.permissions.modify"))

'  Chilkat honors the restrictions.  Copying is not permitted, so the text cannot be extracted.
Dim opts As ChilkatJsonObject
opts.Initialize
Dim extractOut As ChilkatJsonObject
extractOut.Initialize
Dim sb As ChilkatStringBuilder
sb.Initialize
success = reader.ExtractTextSb("text", opts, sb, extractOut)
If success = False Then
    Log("Text extraction was refused: the PDF does not permit copying.")
End If


'  The owner password lifts every restriction.
Dim owner As ChilkatPdf
owner.Initialize("owner")
owner.OwnerPassword = "owner-secret"
success = owner.LoadFile("qa_output/memo_restricted.pdf")
If success = False Then
    Log(owner.LastErrorText)
    Return
End If

Dim ownerInfo As ChilkatJsonObject
ownerInfo.Initialize
success = owner.Protection("getInfo", empty, ownerInfo)
If success = False Then
    Log(owner.LastErrorText)
    Return
End If

Log("opened with: " & ownerInfo.StringOf("loaded.openedWith"))
success = owner.ExtractTextSb("text", opts, sb, extractOut)
If success = False Then
    Log(owner.LastErrorText)
    Return
End If

Log("characters extracted with the owner password: " & sb.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