PureBasic Requires Chilkat v11.7.0+
PureBasic
Extract Text from a PDF that Forbids Copying
See more PDF Text Extraction Examples
Demonstrates a PDF that opens without a password but whose permissions do not allow copying text. The "text", "markdown", "search" and "tables" operations fail with that reason; "assess" always works. Loading the PDF with the OwnerPassword property set grants owner rights, and extraction works.Chilkat PureBasic Downloads
IncludeFile "CkPdf.pb"
IncludeFile "CkJsonObject.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 (owner password: owner-secret; copying not permitted) is available at no_copy_permission.pdf
pdf.i = CkPdf::ckCreate()
If pdf.i = 0
Debug "Failed to create object."
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
; The PDF opens without a password.
success = CkPdf::ckLoadFile(pdf,"qa_data/pdf/no_copy_permission.pdf")
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
; Extracting text is refused, because the PDF's permissions do not allow copying.
success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
If success = 0
Debug "Extraction refused: " + CkPdf::ckLastErrorText(pdf)
EndIf
; "assess" reports only counts and ratings, never text, so it is always allowed.
success = CkPdf::ckExtractText(pdf,"assess",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug "rating: " + CkJsonObject::ckStringOf(jsonOut,"quality.rating")
; Load with the owner password to get owner rights, which include copying.
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)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
success = CkPdf::ckExtractText(pdf,"text",json,jsonOut)
If success = 0
Debug CkPdf::ckLastErrorText(pdf)
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndIf
Debug CkJsonObject::ckStringOf(jsonOut,"text")
; Sample output:
; Extraction refused: ChilkatLog:
; ExtractText:
; ...
; The PDF's permissions do not allow copying or extracting text (load it with the owner password to extract).
; ...
; rating: good
; Visitor Leaflet
;
; Welcome to the harbor lighthouse. The tower is open on weekends from July to September.
;
; Please keep to the marked paths and do not climb on the breakwater.
;
; The lantern room is closed in high winds.
CkPdf::ckDispose(pdf)
CkJsonObject::ckDispose(json)
CkJsonObject::ckDispose(jsonOut)
ProcedureReturn
EndProcedure