PDF Testing

This commit is contained in:
202310715297 RAIHAN ARIQ MUZAKKI 2025-11-14 20:59:46 +07:00
parent a7f770adf4
commit 30298ac46e
5 changed files with 120 additions and 4 deletions

View File

@ -68,7 +68,7 @@ dependencies {
debugImplementation(libs.ui.tooling)
debugImplementation(libs.ui.test.manifest)
// Room Database - TAMBAHKAN INI SEMUA
// Room Database
val roomVersion = "2.6.1"
implementation("androidx.room:room-runtime:$roomVersion")
implementation("androidx.room:room-ktx:$roomVersion")
@ -91,4 +91,7 @@ dependencies {
// Gson
implementation("com.google.code.gson:gson:2.10.1")
// PDF Support
implementation("com.tom-roush:pdfbox-android:2.0.27.0")
}

View File

@ -40,6 +40,10 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Initialize PDFBox
com.example.notebook.utils.PdfHelper.initialize(this)
setContent {
NotebookTheme {
Surface(

View File

@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit
*/
interface GeminiApiService {
@POST("v1beta/models/gemini-1.5-flash:generateContent")
@POST("v1beta/models/gemini-2.0-flash:generateContent")
suspend fun generateContent(
@Query("key") apiKey: String,
@Body request: GeminiRequest

View File

@ -76,11 +76,27 @@ object FileHelper {
}
/**
* Baca text dari file
* Baca text dari file (support Text, Markdown, dan PDF)
*/
fun readTextFromFile(filePath: String): String? {
return try {
File(filePath).readText()
val file = File(filePath)
val extension = file.extension.lowercase()
when (extension) {
"pdf" -> {
// Extract text dari PDF
PdfHelper.extractTextFromPdf(filePath)
}
"txt", "md", "markdown" -> {
// Baca text biasa
file.readText()
}
else -> {
println("⚠️ Format file tidak didukung untuk ekstraksi teks: $extension")
null
}
}
} catch (e: Exception) {
e.printStackTrace()
null

View File

@ -0,0 +1,93 @@
package com.example.notebook.utils
import android.content.Context
import android.net.Uri
import com.tom_roush.pdfbox.android.PDFBoxResourceLoader
import com.tom_roush.pdfbox.pdmodel.PDDocument
import com.tom_roush.pdfbox.text.PDFTextStripper
import java.io.File
/**
* Helper untuk extract text dari PDF
*/
object PdfHelper {
/**
* Initialize PDFBox (panggil sekali saat app start)
*/
fun initialize(context: Context) {
PDFBoxResourceLoader.init(context)
}
/**
* Extract text dari PDF file
* Return: Text content atau null jika error
*/
fun extractTextFromPdf(filePath: String): String? {
return try {
val file = File(filePath)
val document = PDDocument.load(file)
val stripper = PDFTextStripper()
val text = stripper.getText(document)
document.close()
// Cleanup whitespace
text.trim()
} catch (e: Exception) {
println("❌ Error extract PDF: ${e.message}")
e.printStackTrace()
null
}
}
/**
* Extract text dari PDF URI (sebelum disimpan)
*/
fun extractTextFromPdfUri(context: Context, uri: Uri): String? {
return try {
val inputStream = context.contentResolver.openInputStream(uri)
val document = PDDocument.load(inputStream)
val stripper = PDFTextStripper()
val text = stripper.getText(document)
document.close()
inputStream?.close()
text.trim()
} catch (e: Exception) {
println("❌ Error extract PDF from URI: ${e.message}")
e.printStackTrace()
null
}
}
/**
* Get info PDF (jumlah halaman, dll)
*/
fun getPdfInfo(filePath: String): PdfInfo? {
return try {
val file = File(filePath)
val document = PDDocument.load(file)
val info = PdfInfo(
pageCount = document.numberOfPages,
title = document.documentInformation.title ?: "Unknown",
author = document.documentInformation.author ?: "Unknown"
)
document.close()
info
} catch (e: Exception) {
null
}
}
}
data class PdfInfo(
val pageCount: Int,
val title: String,
val author: String
)