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

Ruby
require 'chilkat'

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

pdf = Chilkat::CkPdf.new()
success = pdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
if (success == false)
    print pdf.lastErrorText() + "\n";
    exit
end

#  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.put_UserPassword("")
pdf.put_OwnerPassword("owner-secret")

json = Chilkat::CkJsonObject.new()
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.

jsonOut = Chilkat::CkJsonObject.new()
success = pdf.Protection("encrypt",json,jsonOut)
if (success == false)
    print pdf.lastErrorText() + "\n";
    exit
end

success = pdf.WriteFile("qa_output/memo_restricted.pdf")
if (success == false)
    print pdf.lastErrorText() + "\n";
    exit
end

#  Anyone can open the result, with the rights of a user.
reader = Chilkat::CkPdf.new()
success = reader.LoadFile("qa_output/memo_restricted.pdf")
if (success == false)
    print reader.lastErrorText() + "\n";
    exit
end

empty = Chilkat::CkJsonObject.new()
info = Chilkat::CkJsonObject.new()
success = reader.Protection("getInfo",empty,info)
if (success == false)
    print reader.lastErrorText() + "\n";
    exit
end

print "opened with: " + info.stringOf("loaded.openedWith") + "\n";
print "print: " + info.stringOf("loaded.permissions.print") + "\n";
print "copy: " + info.BoolOf("loaded.permissions.copy").to_s() + "\n";
print "modify: " + info.BoolOf("loaded.permissions.modify").to_s() + "\n";

#  Chilkat honors the restrictions.  Copying is not permitted, so the text cannot be extracted.
opts = Chilkat::CkJsonObject.new()
extractOut = Chilkat::CkJsonObject.new()
sb = Chilkat::CkStringBuilder.new()
success = reader.ExtractTextSb("text",opts,sb,extractOut)
if (success == false)
    print "Text extraction was refused: the PDF does not permit copying." + "\n";
end

#  The owner password lifts every restriction.
owner = Chilkat::CkPdf.new()
owner.put_OwnerPassword("owner-secret")
success = owner.LoadFile("qa_output/memo_restricted.pdf")
if (success == false)
    print owner.lastErrorText() + "\n";
    exit
end

ownerInfo = Chilkat::CkJsonObject.new()
success = owner.Protection("getInfo",empty,ownerInfo)
if (success == false)
    print owner.lastErrorText() + "\n";
    exit
end

print "opened with: " + ownerInfo.stringOf("loaded.openedWith") + "\n";
success = owner.ExtractTextSb("text",opts,sb,extractOut)
if (success == false)
    print owner.lastErrorText() + "\n";
    exit
end

print "characters extracted with the owner password: " + sb.get_Length().to_s() + "\n";

#  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