PDF Testing
This commit is contained in:
parent
a7f770adf4
commit
30298ac46e
@ -68,7 +68,7 @@ dependencies {
|
|||||||
debugImplementation(libs.ui.tooling)
|
debugImplementation(libs.ui.tooling)
|
||||||
debugImplementation(libs.ui.test.manifest)
|
debugImplementation(libs.ui.test.manifest)
|
||||||
|
|
||||||
// Room Database - TAMBAHKAN INI SEMUA
|
// Room Database
|
||||||
val roomVersion = "2.6.1"
|
val roomVersion = "2.6.1"
|
||||||
implementation("androidx.room:room-runtime:$roomVersion")
|
implementation("androidx.room:room-runtime:$roomVersion")
|
||||||
implementation("androidx.room:room-ktx:$roomVersion")
|
implementation("androidx.room:room-ktx:$roomVersion")
|
||||||
@ -91,4 +91,7 @@ dependencies {
|
|||||||
|
|
||||||
// Gson
|
// Gson
|
||||||
implementation("com.google.code.gson:gson:2.10.1")
|
implementation("com.google.code.gson:gson:2.10.1")
|
||||||
|
|
||||||
|
// PDF Support
|
||||||
|
implementation("com.tom-roush:pdfbox-android:2.0.27.0")
|
||||||
}
|
}
|
||||||
@ -40,6 +40,10 @@ class MainActivity : ComponentActivity() {
|
|||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
|
// Initialize PDFBox
|
||||||
|
com.example.notebook.utils.PdfHelper.initialize(this)
|
||||||
|
|
||||||
setContent {
|
setContent {
|
||||||
NotebookTheme {
|
NotebookTheme {
|
||||||
Surface(
|
Surface(
|
||||||
|
|||||||
@ -15,7 +15,7 @@ import java.util.concurrent.TimeUnit
|
|||||||
*/
|
*/
|
||||||
interface GeminiApiService {
|
interface GeminiApiService {
|
||||||
|
|
||||||
@POST("v1beta/models/gemini-1.5-flash:generateContent")
|
@POST("v1beta/models/gemini-2.0-flash:generateContent")
|
||||||
suspend fun generateContent(
|
suspend fun generateContent(
|
||||||
@Query("key") apiKey: String,
|
@Query("key") apiKey: String,
|
||||||
@Body request: GeminiRequest
|
@Body request: GeminiRequest
|
||||||
|
|||||||
@ -76,11 +76,27 @@ object FileHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Baca text dari file
|
* Baca text dari file (support Text, Markdown, dan PDF)
|
||||||
*/
|
*/
|
||||||
fun readTextFromFile(filePath: String): String? {
|
fun readTextFromFile(filePath: String): String? {
|
||||||
return try {
|
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) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
null
|
null
|
||||||
|
|||||||
93
app/src/main/java/com/example/notebook/utils/PdfHelper.kt
Normal file
93
app/src/main/java/com/example/notebook/utils/PdfHelper.kt
Normal 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
|
||||||
|
)
|
||||||
Loading…
x
Reference in New Issue
Block a user