Sample code for 30+ languages & platforms
DataFlex Requires Chilkat v11.7.0+

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 DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoPdf
    Variant vJson
    Handle hoJson
    Variant vJsonOut
    Handle hoJsonOut
    String sTemp1

    Move False To iSuccess

    //  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

    Get Create (RefClass(cComChilkatPdf)) To hoPdf
    If (Not(IsComObjectCreated(hoPdf))) Begin
        Send CreateComObject of hoPdf
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End
    Get Create (RefClass(cComChilkatJsonObject)) To hoJsonOut
    If (Not(IsComObjectCreated(hoJsonOut))) Begin
        Send CreateComObject of hoJsonOut
    End

    //  The PDF opens without a password.
    Get ComLoadFile Of hoPdf "qa_data/pdf/no_copy_permission.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Extracting text is refused, because the PDF's permissions do not allow copying.
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln "Extraction refused: " sTemp1
    End

    //  "assess" reports only counts and ratings, never text, so it is always allowed.
    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComExtractText Of hoPdf "assess" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoJsonOut "quality.rating" To sTemp1
    Showln "rating: " sTemp1

    //  Load with the owner password to get owner rights, which include copying.
    Set ComOwnerPassword Of hoPdf To "owner-secret"
    Get ComLoadFile Of hoPdf "qa_data/pdf/no_copy_permission.pdf" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get pvComObject of hoJson to vJson
    Get pvComObject of hoJsonOut to vJsonOut
    Get ComExtractText Of hoPdf "text" vJson vJsonOut To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoPdf To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComStringOf Of hoJsonOut "text" To sTemp1
    Showln sTemp1

    //  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.


End_Procedure