AutoIt Requires Chilkat v11.7.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = 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
$oPdf = ObjCreate("Chilkat.Pdf")
$bSuccess = $oPdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
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 = ObjCreate("Chilkat.JsonObject")
$oJson.UpdateString("permissions.print","lowQuality")
$oJson.UpdateBool("permissions.copy",False)
$oJson.UpdateBool("permissions.modify",False)
$oJson.UpdateBool("permissions.annotate",False)
; The permissions not mentioned (fillForms, assemble, accessibility) stay granted.
$oJsonOut = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oPdf.Protection("encrypt",$oJson,$oJsonOut)
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
$bSuccess = $oPdf.WriteFile("qa_output/memo_restricted.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oPdf.LastErrorText & @CRLF)
Exit
EndIf
; Anyone can open the result, with the rights of a user.
$oReader = ObjCreate("Chilkat.Pdf")
$bSuccess = $oReader.LoadFile("qa_output/memo_restricted.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oReader.LastErrorText & @CRLF)
Exit
EndIf
$oEmpty = ObjCreate("Chilkat.JsonObject")
$oInfo = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oReader.Protection("getInfo",$oEmpty,$oInfo)
If ($bSuccess = False) Then
ConsoleWrite($oReader.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("opened with: " & $oInfo.StringOf("loaded.openedWith") & @CRLF)
ConsoleWrite("print: " & $oInfo.StringOf("loaded.permissions.print") & @CRLF)
ConsoleWrite("copy: " & $oInfo.BoolOf("loaded.permissions.copy") & @CRLF)
ConsoleWrite("modify: " & $oInfo.BoolOf("loaded.permissions.modify") & @CRLF)
; Chilkat honors the restrictions. Copying is not permitted, so the text cannot be extracted.
$oOpts = ObjCreate("Chilkat.JsonObject")
$oExtractOut = ObjCreate("Chilkat.JsonObject")
$oSb = ObjCreate("Chilkat.StringBuilder")
$bSuccess = $oReader.ExtractTextSb("text",$oOpts,$oSb,$oExtractOut)
If ($bSuccess = False) Then
ConsoleWrite("Text extraction was refused: the PDF does not permit copying." & @CRLF)
EndIf
; The owner password lifts every restriction.
$oOwner = ObjCreate("Chilkat.Pdf")
$oOwner.OwnerPassword = "owner-secret"
$bSuccess = $oOwner.LoadFile("qa_output/memo_restricted.pdf")
If ($bSuccess = False) Then
ConsoleWrite($oOwner.LastErrorText & @CRLF)
Exit
EndIf
$oOwnerInfo = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oOwner.Protection("getInfo",$oEmpty,$oOwnerInfo)
If ($bSuccess = False) Then
ConsoleWrite($oOwner.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("opened with: " & $oOwnerInfo.StringOf("loaded.openedWith") & @CRLF)
$bSuccess = $oOwner.ExtractTextSb("text",$oOpts,$oSb,$oExtractOut)
If ($bSuccess = False) Then
ConsoleWrite($oOwner.LastErrorText & @CRLF)
Exit
EndIf
ConsoleWrite("characters extracted with the owner password: " & $oSb.Length & @CRLF)
; 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