PureBasic Requires Chilkat v11.7.0+
PureBasic
Remove the Restrictions from a PDF
See more PDF Protection Examples
Demonstrates the removeRestrictions operation, which grants every permission. A PDF that needs a password to open stays encrypted and keeps that password. A PDF with no user password, like the one here, has nothing left to protect and is saved unencrypted, the same as removeEncryption. The operation needs owner rights, so the PDF is loaded with its owner password.Chilkat PureBasic Downloads
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkJsonObject.pb"
IncludeFile "CkPdf.pb"
Procedure ChilkatExample()
success.i = 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 no_copy_permission.pdf
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; This PDF opens without a password, but copying text from it is not permitted.
; Removing the restrictions needs owner rights: load with the owner password.
CkPdf::setCkOwnerPassword(pdf, "owner-secret")
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/no_copy_permission.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
ProcedureReturn
EndIf
json.i = CkJsonObject::ckCreate()
If json.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
jsonOut.i = CkJsonObject::ckCreate()
If jsonOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckProtection(pdf,"getInfo",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "may copy, before: " + Str(CkJsonObject::ckBoolOf(jsonOut,"loaded.permissions.copy"))
Debug "password needed to open: " + Str(CkJsonObject::ckBoolOf(jsonOut,"loaded.userPasswordRequired"))
success = CkPdf::ckProtection(pdf,"removeRestrictions",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
; With no user password and nothing restricted there is nothing left to protect, so the PDF
; will be saved unencrypted. A PDF that needs a password to open would stay encrypted and
; keep its password.
Debug "will be saved encrypted: " + Str(CkJsonObject::ckBoolOf(jsonOut,"pending.encrypted"))
success = CkPdf::ckWriteFile(pdf,"qa_output/leaflet_unrestricted.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
; Anyone can now extract the text.
reader.i = CkPdf::ckCreate()
If reader.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckLoadFile(reader,"qa_output/leaflet_unrestricted.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(reader)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(reader)
ProcedureReturn
EndIf
opts.i = CkJsonObject::ckCreate()
If opts.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
extractOut.i = CkJsonObject::ckCreate()
If extractOut.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
sb.i = CkStringBuilder::ckCreate()
If sb.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkPdf::ckExtractTextSb(reader,"text",opts,sb,extractOut)
If success = 0
Debug CkPdf::ckLastErrorText(reader)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(reader)
CkJsonObject::ckDispose(opts)
CkJsonObject::ckDispose(extractOut)
CkStringBuilder::ckDispose(sb)
ProcedureReturn
EndIf
Debug "characters extracted: " + Str(CkStringBuilder::ckLength(sb))
; Sample output:
; may copy, before: False
; password needed to open: False
; will be saved encrypted: False
; characters extracted: 216
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
CkPdf::ckDispose(reader)
CkJsonObject::ckDispose(opts)
CkJsonObject::ckDispose(extractOut)
CkStringBuilder::ckDispose(sb)
ProcedureReturn
EndProcedure