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)

Functions

Link copied to clipboard
suspend fun isDocumentValid(context: Context, source: Any, password: String? = null): Boolean

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.

Link copied to clipboard
suspend fun isPasswordCorrect(context: Context, source: Any, password: String): Boolean

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

Link copied to clipboard
suspend fun isPasswordProtected(context: Context, source: Any): Boolean

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.

Link copied to clipboard
suspend fun validateDocument(context: Context, source: Any, password: String? = null, includeMetadata: Boolean = true): DocumentValidationResult

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