facing issue in going to specific page in search in pdf in kotline andriod studio
fida2580 opened this issue · 0 comments
import android.app.AlertDialog
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.github.barteksc.pdfviewer.PDFView
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle
import com.example.pdf_reader.databinding.PdfviewerBinding
import java.io.File
class PdfViewerFragment : Fragment(), OnPageChangeListener, OnLoadCompleteListener {
private lateinit var binding: PdfviewerBinding
private var pageNumber: Int = 0
private var filePath: String? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
binding = PdfviewerBinding.inflate(inflater, container, false)
filePath = arguments?.getString("filePath")
filePath?.let {
val file = File(it)
if (file.exists()) {
binding.pdfview.apply {
fromFile(file)
.defaultPage(pageNumber)
.enableSwipe(true)
.swipeHorizontal(false)
.onPageChange(this@PdfViewerFragment)
.onLoad(this@PdfViewerFragment)
.enableAnnotationRendering(true)
.scrollHandle(DefaultScrollHandle(requireContext()))
.load()
}
} else {
Log.e(TAG, "File does not exist: $it")
Toast.makeText(requireContext(), "File does not exist: $it", Toast.LENGTH_LONG).show()
}
}
binding.srch2.setOnClickListener {
showPageNumberDialog()
}
return binding.root
}
private fun showPageNumberDialog() {
val editText = EditText(requireContext()).apply {
inputType = android.text.InputType.TYPE_CLASS_NUMBER
hint = "Enter page number"
}
AlertDialog.Builder(requireContext())
.setTitle("Go to Page")
.setView(editText)
.setPositiveButton("Go") { _, _ ->
val pageNumberText = editText.text.toString()
if (pageNumberText.isNotEmpty()) {
val pageNumber = pageNumberText.toIntOrNull()
if (pageNumber != null && pageNumber in 1..binding.pdfview.pageCount) {
gotoPage(pageNumber)
} else {
Toast.makeText(requireContext(), "Invalid page number. Enter a number between 1 and ${binding.pdfview.pageCount}", Toast.LENGTH_LONG).show()
}
} else {
Toast.makeText(requireContext(), "Page number input is empty", Toast.LENGTH_SHORT).show()
}
}
.setNegativeButton("Cancel", null)
.create()
.show()
}
private fun gotoPage(pageNumber: Int) {
binding.pdfview.jumpTo(pageNumber - 1, true)
Toast.makeText(requireContext(), "Jumping to page $pageNumber", Toast.LENGTH_SHORT).show()
Log.d(TAG, "Jumping to page $pageNumber (zero-based index: ${pageNumber - 1})")
}
override fun onPageChanged(page: Int, pageCount: Int) {
pageNumber = page
Toast.makeText(requireContext(), "Current page: ${page + 1} of $pageCount", Toast.LENGTH_SHORT).show()
Log.d(TAG, "Current page: ${page + 1} of $pageCount")
}
override fun loadComplete(nbPages: Int) {
Toast.makeText(requireContext(), "PDF loaded with $nbPages pages", Toast.LENGTH_SHORT).show()
Log.d(TAG, "PDF loaded with $nbPages pages")
}
companion object {
private const val TAG = "PdfViewerFragment"
fun newInstance(filePath: String): PdfViewerFragment {
val fragment = PdfViewerFragment()
val args = Bundle()
args.putString("filePath", filePath)
fragment.arguments = args
return fragment
}
}
}
this is my code where the user will enter the any page number in the seach button then it will go to that specific page which the user enter now the issue is that when i enter the page number then it doesnot go to that page but i the toast message is showe which i added to test let suppose i m on 5th page and i enter 20 page then it doesnot jump to the 20th page but the toast message is showing that jump to 20 page