Package 

Object PDFDocumentValidator


  • 
    public class PDFDocumentValidator
    
                        

    Utility class for validating PDF documents from various sources.

    This class provides static methods to validate PDF documents and determine their state:

    • Valid and readable

    • Password protected

    • Corrupted

    • Invalid format

    • File not found

    All operations are performed on background threads for optimal performance and can be used to pre-validate documents before attempting to open them in viewers or generate thumbnails.

    Example usage:

    // Basic validation
    val result = PDFDocumentValidator.validateDocument(context, file)
    when (result) {
        is DocumentValidationResult.Valid -> {
            println("Document is valid with ${result.pageCount} pages")
        }
        is DocumentValidationResult.PasswordProtected -> {
            println("Document requires password")
        }
        is DocumentValidationResult.Corrupted -> {
            println("Document is corrupted: ${result.reason}")
        }
        is DocumentValidationResult.Invalid -> {
            println("Document is invalid: ${result.reason}")
        }
        is DocumentValidationResult.Error -> {
            println("Error validating document: ${result.errorMessage}")
        }
    }
    
    // Validation with password attempt
    val resultWithPassword = PDFDocumentValidator.validateDocument(
        context, file, password = "user_password"
    )
    
    // Quick validation (faster, less detailed)
    val isValid = PDFDocumentValidator.isDocumentValid(context, file)
    • Method Detail

      • validateDocument

         final DocumentValidationResult validateDocument(Context context, Object source, String password, Boolean includeMetadata)

        Validates a PDF document from any supported source and returns detailed validation results.

        Parameters:
        context - The application context.
        source - The PDF document source (File, Uri, ByteArray, InputStream, String asset path, or DocumentSource).
        password - Optional password to test if the document is password protected.
        includeMetadata - Whether to include metadata analysis in the validation (default: true).
      • isDocumentValid

         final Boolean isDocumentValid(Context context, Object source, String password)

        Performs a quick validation check to determine if a document is valid and readable. This is a faster operation that returns only a boolean result.

        Parameters:
        context - The application context.
        source - The PDF document source.
        password - Optional password for encrypted documents.
      • isPasswordCorrect

         final Boolean isPasswordCorrect(Context context, Object source, String password)

        Validates if a document requires a password and tests if the provided password is correct.

        Parameters:
        context - The application context.
        source - The PDF document source.
        password - The password to test.
      • isPasswordProtected

         final Boolean isPasswordProtected(Context context, Object source)

        Determines if a document is password protected without attempting to open it. This method will try to open the document without a password and check if it fails due to password protection.

        Parameters:
        context - The application context.
        source - The PDF document source.