2

I need a clear solution of how to get the number of pages from a PDF file in android. Assuming the person has selected the file then on the onActivityResult(), I want to know how many pages the PDF file is. I would love a solution that will work for minSdkVersion 19 and would not need me paying for any PDF manipulation software, because I have checked round the net and there is no clear solution. Please it will be well appreciated because I really need the solution. I am using it with Firebase and want to upload the number of pages of the PDF as I also upload the file.

Here is a sample code, I used PDF Box: The error I keep getting is 08-10 21:07:57.533 20514-20514/com.example.android.firebaseupload E/file: Error: End-of-File, expected line I had to put in the file path manually before now and it has been the same error even after typing the file path manually. How do I load a pdf file from a path into the PDDocument.load()??

 try {

         PDDocument doc = PDDocument.load("file:///storage/emulated/0/Boom%20Player/cache/c2#.pdf".getBytes());
         Toast.makeText(MainActivity.this,"Number of pages: "+doc.getNumberOfPages(),Toast.LENGTH_LONG).show();
     }
     catch (Exception e)
     {

         Log.e("file",e.getMessage().toString());
     }
Lemuel Ogbunude
  • 184
  • 3
  • 16
  • https://stackoverflow.com/q/35629451/115145 https://stackoverflow.com/q/6026971/115145 https://stackoverflow.com/q/4134949/115145 and many other pages show up for a Google search on `java count pages pdf`. https://stackoverflow.com/q/30483373/115145 shows up for a Google search on `android count pages pdf`. What is different about your question than those? – CommonsWare Aug 10 '17 at 19:39
  • I have seen those, and have come across most of them if not all. Non of them gave me a working answer. I have tried the "suggested" solutions, but to no avail. ...Or should I print out the error I'm having? because out of all I have checked none has given me a proper answer. – Lemuel Ogbunude Aug 10 '17 at 19:46
  • I suggest that you ask a separate Stack Overflow question, where you provide a [mcve] demonstrating what you tried and what specific problems you encountered. – CommonsWare Aug 10 '17 at 19:51
  • 1
    `PDDocument doc = PDDocument.load("file:///storage/emulated/0/Boom%20Player/cache/c2#.pdf".getBytes());` looks to me as if you're loading a byte sequence as PDF. PDF files do not start with "file://", they start with "%PDF". Please pass a file object. – Tilman Hausherr Aug 10 '17 at 20:55
  • indeed the `getByte‌​s()` call is totally wrong. – mkl Aug 10 '17 at 21:48
  • This link was helpful though https://commonsware.com/blog/2016/03/15/how-consume-content-uri.html – Lemuel Ogbunude Aug 11 '17 at 17:33

2 Answers2

2

Found the solution, instead of calling getpath() on the Uri and trying to open it with new file(Uri.getPath()), I Call openInputStream() to get an InputStream from the Uri. Wonderful help here ... getContentResolver().openInputStream(uri) Android: Getting a file URI from a content URI?:) Also thanks the link https://commonsware.com/blog/2016/03/15/how-consume-content-uri.html for making me understand more.

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==pickimage && resultCode==RESULT_OK && data!=null && data.getData()!=null)
    {
        filepath=data.getData();...//rest of code...

   PDDocument doc = PDDocument.load(getContentResolver().openInputStream(filepath));//main line
Lemuel Ogbunude
  • 184
  • 3
  • 16
1

PDFBox is the Java equivalent to what you are asking. There's a port of it for Android, though I'm not sure how complete it is.

An example would be:

PDDocument myDocument = PDDocument.load(new File("filename.pdf"));
int numPages = myDocument.getNumberOfPages();
mjonesjr90
  • 21
  • 5
  • Thanks,I saw that also when researching, the main issue is the NEW FILE part of the first line, how do i direct it to the PDF file without giving me an error. I have tried Bytes[] and similar solutions but keeps giving me that Error: End-of-File, expected line – Lemuel Ogbunude Aug 10 '17 at 20:14
  • Try just sending through the PDF and not the Byte representation – mjonesjr90 Aug 10 '17 at 20:18
  • Using the absolute path or just the name of the file itself? – Lemuel Ogbunude Aug 10 '17 at 20:19
  • file:///storage/emulated/0/Boom%20Player/cache/c2#.pdf – mjonesjr90 Aug 10 '17 at 20:22
  • 1
    did you have an issue with this? PDDocument.load(new File("file:///storage/emulated/0/Boom%20Player/cache/c2#.pdf")); – mjonesjr90 Aug 10 '17 at 20:24
  • Yes it gives me that error, ** file:/storage/emulated/0/Boom%20Player/cache/c2#.pdf: open failed: ENOENT (No such file or directory)** – Lemuel Ogbunude Aug 10 '17 at 20:25
  • @Charles Chibueze "file:///" is an URL, not a file name. Maybe start by learning how to get files on Android. https://developer.android.com/reference/java/io/File.html Check that your file exists with new File("....").exists(). – Tilman Hausherr Aug 10 '17 at 20:56
  • @CharlesChibueze You can also do this if you insist on working with URLs: `PDDocument.load(new File(new URL("file:///storage/emulated/0/Boom%20Player/ca‌​che/c2#.pdf").getFile()));`. – Tilman Hausherr Aug 11 '17 at 07:39