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

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Pdf
oleobject loo_Json
oleobject loo_JsonOut
oleobject loo_Reader
oleobject loo_Empty
oleobject loo_Info
oleobject loo_Opts
oleobject loo_ExtractOut
oleobject loo_Sb
oleobject loo_Owner
oleobject loo_OwnerInfo

li_Success = 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

loo_Pdf = create oleobject
li_rc = loo_Pdf.ConnectToNewObject("Chilkat.Pdf")
if li_rc < 0 then
    destroy loo_Pdf
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_Success = loo_Pdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    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.
loo_Pdf.UserPassword = ""
loo_Pdf.OwnerPassword = "owner-secret"

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat.JsonObject")

loo_Json.UpdateString("permissions.print","lowQuality")
loo_Json.UpdateBool("permissions.copy",0)
loo_Json.UpdateBool("permissions.modify",0)
loo_Json.UpdateBool("permissions.annotate",0)
//  The permissions not mentioned (fillForms, assemble, accessibility) stay granted.

loo_JsonOut = create oleobject
li_rc = loo_JsonOut.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Pdf.Protection("encrypt",loo_Json,loo_JsonOut)
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

li_Success = loo_Pdf.WriteFile("qa_output/memo_restricted.pdf")
if li_Success = 0 then
    Write-Debug loo_Pdf.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    return
end if

//  Anyone can open the result, with the rights of a user.
loo_Reader = create oleobject
li_rc = loo_Reader.ConnectToNewObject("Chilkat.Pdf")

li_Success = loo_Reader.LoadFile("qa_output/memo_restricted.pdf")
if li_Success = 0 then
    Write-Debug loo_Reader.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Reader
    return
end if

loo_Empty = create oleobject
li_rc = loo_Empty.ConnectToNewObject("Chilkat.JsonObject")

loo_Info = create oleobject
li_rc = loo_Info.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Reader.Protection("getInfo",loo_Empty,loo_Info)
if li_Success = 0 then
    Write-Debug loo_Reader.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Reader
    destroy loo_Empty
    destroy loo_Info
    return
end if

Write-Debug "opened with: " + loo_Info.StringOf("loaded.openedWith")
Write-Debug "print: " + loo_Info.StringOf("loaded.permissions.print")
Write-Debug "copy: " + string(loo_Info.BoolOf("loaded.permissions.copy"))
Write-Debug "modify: " + string(loo_Info.BoolOf("loaded.permissions.modify"))

//  Chilkat honors the restrictions.  Copying is not permitted, so the text cannot be extracted.
loo_Opts = create oleobject
li_rc = loo_Opts.ConnectToNewObject("Chilkat.JsonObject")

loo_ExtractOut = create oleobject
li_rc = loo_ExtractOut.ConnectToNewObject("Chilkat.JsonObject")

loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")

li_Success = loo_Reader.ExtractTextSb("text",loo_Opts,loo_Sb,loo_ExtractOut)
if li_Success = 0 then
    Write-Debug "Text extraction was refused: the PDF does not permit copying."
end if

//  The owner password lifts every restriction.
loo_Owner = create oleobject
li_rc = loo_Owner.ConnectToNewObject("Chilkat.Pdf")

loo_Owner.OwnerPassword = "owner-secret"
li_Success = loo_Owner.LoadFile("qa_output/memo_restricted.pdf")
if li_Success = 0 then
    Write-Debug loo_Owner.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Reader
    destroy loo_Empty
    destroy loo_Info
    destroy loo_Opts
    destroy loo_ExtractOut
    destroy loo_Sb
    destroy loo_Owner
    return
end if

loo_OwnerInfo = create oleobject
li_rc = loo_OwnerInfo.ConnectToNewObject("Chilkat.JsonObject")

li_Success = loo_Owner.Protection("getInfo",loo_Empty,loo_OwnerInfo)
if li_Success = 0 then
    Write-Debug loo_Owner.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Reader
    destroy loo_Empty
    destroy loo_Info
    destroy loo_Opts
    destroy loo_ExtractOut
    destroy loo_Sb
    destroy loo_Owner
    destroy loo_OwnerInfo
    return
end if

Write-Debug "opened with: " + loo_OwnerInfo.StringOf("loaded.openedWith")
li_Success = loo_Owner.ExtractTextSb("text",loo_Opts,loo_Sb,loo_ExtractOut)
if li_Success = 0 then
    Write-Debug loo_Owner.LastErrorText
    destroy loo_Pdf
    destroy loo_Json
    destroy loo_JsonOut
    destroy loo_Reader
    destroy loo_Empty
    destroy loo_Info
    destroy loo_Opts
    destroy loo_ExtractOut
    destroy loo_Sb
    destroy loo_Owner
    destroy loo_OwnerInfo
    return
end if

Write-Debug "characters extracted with the owner password: " + string(loo_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


destroy loo_Pdf
destroy loo_Json
destroy loo_JsonOut
destroy loo_Reader
destroy loo_Empty
destroy loo_Info
destroy loo_Opts
destroy loo_ExtractOut
destroy loo_Sb
destroy loo_Owner
destroy loo_OwnerInfo