Java Question Wrapping PDFBox for Android. Resources in the AAR can not be found

DonManfred

Expert
Licensed User
Longtime User
Hello,

i am wrapping PDFBox for Android

Internally the Library is using this class to load Resources at appstart.

Java:
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.tom_roush.pdfbox.android;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;

public class PDFBoxResourceLoader{
    /**
     * The AssetManager used to load the resources
     */
    private static AssetManager ASSET_MANAGER = null;
    /**
     * Initializes the loader
     *
     * @param context the context of the calling app
     */
    public static void init(Context context){
      ASSET_MANAGER = context.getApplicationContext().getAssets();
    }
    /**
     * Checks whether the loader has been initialized
     *
     * @return whether the loader has been initialized or not
     */
    public static boolean isReady()
    {
        return ASSET_MANAGER != null;
    }
    /**
     * Loads a resource file located in the assets folder
     *
     * @param path the path to the resource
     *
     * @return the resource as an InputStream
     * @throws IOException if the resource cannot be found
     */
    public static InputStream getStream(String path) throws IOException{
      if (ASSET_MANAGER == null){
        Log.e("PdfBox-Android","PDFBoxResourceLoader is not initialized, call PDFBoxResourceLoader.init() before use");
      }
      return ASSET_MANAGER.open(path);
    }
}

an example use of this class can be found in different locations inside the code...

One example
Java:
    protected InputStream getExternalCMap(String name) throws IOException
    {
        if (PDFBoxResourceLoader.isReady())
        {
            return new BufferedInputStream(PDFBoxResourceLoader.getStream("com/tom_roush/fontbox/resources/cmap/" + name));
        }

        InputStream resourceAsStream = getClass().getResourceAsStream("/com/tom_roush/fontbox/resources/cmap/" + name);
        if (resourceAsStream == null)
        {
            throw new IOException("Error: Could not find referenced cmap stream " + name);
        }
        return new BufferedInputStream(resourceAsStream);
    }

But when i try to call
Java:
PDFBoxResourceLoader.init(ba.applicationContext);
from within my wrapper i get an Exception

*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
Using FileProvider? true
*** mainpage: B4XPage_Created
*** mainpage: B4XPage_Appear
** Activity (main) Resume **
Success: true
b4xmainpage_button2_click (java line: 150)
java.lang.ExceptionInInitializerError
at de.donmanfred.pdfbox.PDFBoxwrapper.StripText(PDFBoxwrapper.java:114)
at b4a.example.pdfbox.b4xmainpage._button2_click(b4xmainpage.java:150)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:7471)
at android.view.View.performClickInternal(View.java:7448)
at android.view.View.access$3600(View.java:813)
at android.view.View$PerformClick.run(View.java:28408)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:250)
at android.app.ActivityThread.main(ActivityThread.java:7755)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)
Caused by: java.lang.RuntimeException: java.io.FileNotFoundException: com/tom_roush/pdfbox/resources/glyphlist/additional.txt
at com.tom_roush.pdfbox.text.LegacyPDFStreamEngine.<clinit>(LegacyPDFStreamEngine.java:107)
... 17 more
Caused by: java.io.FileNotFoundException: com/tom_roush/pdfbox/resources/glyphlist/additional.txt
at android.content.res.AssetManager.nativeOpenAsset(Native Method)
at android.content.res.AssetManager.open(AssetManager.java:874)
at android.content.res.AssetManager.open(AssetManager.java:851)
at com.tom_roush.pdfbox.android.PDFBoxResourceLoader.getStream(PDFBoxResourceLoader.java:69)
at com.tom_roush.pdfbox.text.LegacyPDFStreamEngine.<clinit>(LegacyPDFStreamEngine.java:95)
... 17 more

My guess is that the Android AssetManager isn´t used at all when running from B4A.

Can this class be rewritten to work with B4A. Problem is that i need to add the adapted class in AS and i´m not sure how to add a reference to any B4X Library. 🙈

I am doing a wrapper for this Project:
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've made a test by adding a reference to the aar:
B4X:
#AdditionalJar: C:\Users\H\Downloads\pdfbox-android-2.0.27.0.aar

And:
B4X:
Dim ctxt As JavaObject
ctxt.InitializeContext
Dim ResourceLoader As JavaObject
ResourceLoader.InitializeStatic("com/tom_roush/pdfbox/android/PDFBoxResourceLoader".Replace("/", ".")).RunMethod("init", Array(ctxt))
It works fine.

I also see these resources in the generated apk:

1674454618973.png
 

DonManfred

Expert
Licensed User
Longtime User
It works fine
The error happens not when initializing the PDFBoxResourceLoader.

It happens when you want to extract Text from a pdf which was loaded.
The internal PDFBox-code is using the PDFBoxResourceLoader to load the resources. But here it crashes.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

DonManfred

Expert
Licensed User
Longtime User

PJLPJLPJL

Member
Licensed User
Hi DonManfred,
Extracting text from a pdf (in android) is something I'm interested in (as a hobbyist).
Are you planning to release this project as a library?
With best wishes.
 
Top