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

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
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

set pdf = Server.CreateObject("Chilkat.Pdf")
success = pdf.LoadFile("qa_data/pdf/two_page_memo.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
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"

set json = Server.CreateObject("Chilkat.JsonObject")
success = json.UpdateString("permissions.print","lowQuality")
success = json.UpdateBool("permissions.copy",0)
success = json.UpdateBool("permissions.modify",0)
success = json.UpdateBool("permissions.annotate",0)
'  The permissions not mentioned (fillForms, assemble, accessibility) stay granted.

set jsonOut = Server.CreateObject("Chilkat.JsonObject")
success = pdf.Protection("encrypt",json,jsonOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

success = pdf.WriteFile("qa_output/memo_restricted.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( pdf.LastErrorText) & "</pre>"
    Response.End
End If

'  Anyone can open the result, with the rights of a user.
set reader = Server.CreateObject("Chilkat.Pdf")
success = reader.LoadFile("qa_output/memo_restricted.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( reader.LastErrorText) & "</pre>"
    Response.End
End If

set empty = Server.CreateObject("Chilkat.JsonObject")
set info = Server.CreateObject("Chilkat.JsonObject")
success = reader.Protection("getInfo",empty,info)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( reader.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "opened with: " & info.StringOf("loaded.openedWith")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "print: " & info.StringOf("loaded.permissions.print")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "copy: " & info.BoolOf("loaded.permissions.copy")) & "</pre>"
Response.Write "<pre>" & Server.HTMLEncode( "modify: " & info.BoolOf("loaded.permissions.modify")) & "</pre>"

'  Chilkat honors the restrictions.  Copying is not permitted, so the text cannot be extracted.
set opts = Server.CreateObject("Chilkat.JsonObject")
set extractOut = Server.CreateObject("Chilkat.JsonObject")
set sb = Server.CreateObject("Chilkat.StringBuilder")
success = reader.ExtractTextSb("text",opts,sb,extractOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( "Text extraction was refused: the PDF does not permit copying.") & "</pre>"
End If

'  The owner password lifts every restriction.
set owner = Server.CreateObject("Chilkat.Pdf")
owner.OwnerPassword = "owner-secret"
success = owner.LoadFile("qa_output/memo_restricted.pdf")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( owner.LastErrorText) & "</pre>"
    Response.End
End If

set ownerInfo = Server.CreateObject("Chilkat.JsonObject")
success = owner.Protection("getInfo",empty,ownerInfo)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( owner.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "opened with: " & ownerInfo.StringOf("loaded.openedWith")) & "</pre>"
success = owner.ExtractTextSb("text",opts,sb,extractOut)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( owner.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "characters extracted with the owner password: " & sb.Length) & "</pre>"

'  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

%>
</body>
</html>