如何将 PDFBox 添加到 Android 项目或建议替代方案

发布于 2024-12-28 17:20:15 字数 859 浏览 2 评论 0原文

我正在尝试打开现有的 pdf 文件,然后从 Android 应用程序中将另一个页面添加到 pdf 文档中。在添加的页面上,我需要添加一些文本和图像。

我想尝试一下 PDFBox。由于许可条款/价格的原因,iTextPDF 等其他解决方案不适合我们公司。

我有一个带有主代码库的库项目,还有引用该库项目的完整项目和精简项目。

我已经从 http://pdfbox.apache.org/download.html 下载了 jar 并将其复制到库项目中lib 文件夹并将 pdfbox-app-1.6.0.jar 文件添加到 java 构建路径库中。

我能够成功导入库,例如 import org.apache.pdfbox.pdmodel.PDDocument; 并编译所有项目。但是,当我运行该应用程序时,它在 PDDocument document = new PDDocument(); 上崩溃,并出现以下错误。

E/AndroidRuntime(24451): java.lang.NoClassDefFoundError: org.apache.pdfbox.pdmodel.PDDocument

我在某处读到 PDFBox 1.5 版及以上版本不适用于 Android,因此我尝试下载 pdfbox-app-1.4.0.jar 文件,但遇到了同样的问题。我还将库添加到了完整项目和精简项目中的构建路径中,但我遇到了相同的错误,或者 Eclipse 因内存不足错误而不断崩溃。

谁能告诉我我做错了什么?我下载了错误的文件吗?我正确导入了吗?

谢谢,

I'm attempting to open an existing pdf file and then add another page to the pdf document from within an Android application. On the added page, I need to add some text and an image.

I am wanting to give PDFBox a try. Other solutions such as iTextPDF aren't suitable for our company because of the licencing terms/price.

I have a library project with the main code base, and also full and lite projects that reference the library project.

I have downloaded the jar from http://pdfbox.apache.org/download.html and copied it into the library projects lib folder and added the pdfbox-app-1.6.0.jar file to the java build path libraries.

I am able to import the librarys successfully eg import org.apache.pdfbox.pdmodel.PDDocument; and compile all the projects. However when I run the application it crashes on PDDocument document = new PDDocument(); with the following error.

E/AndroidRuntime(24451): java.lang.NoClassDefFoundError:
org.apache.pdfbox.pdmodel.PDDocument

I read somewhere that version 1.5 of PDFBox onwards didn't work with Android so I tried downloading the pdfbox-app-1.4.0.jar file but got the same issue. I also added the library to the build path in my full and lite projects but I got the same error or eclipse kept crashing with an out of memory error.

Can anyone tell me what I am doing wrong? Have I downloaded the wrong file? Have I imported it correctly?

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凯凯我们等你回来 2025-01-04 17:20:15

PDFBox 使用 java awt 和 swing,即使对于非 UI 任务,我也尝试过删除引用,但文件很多,而且我删除了太多

我刚刚测试过的 PDFjet http://pdfjet.com/os/edition.html 它是 bsd 许可的(加上具有更多功能的商业版本),带有此示例代码(摘自 Example_03.java )我能够转换jpeg 到 pdf

    FileOutputStream fos = null;
    try
    {
        fos = new FileOutputStream("/sdcard/sample.pdf");
        PDF pdf = new PDF(fos);
        InputStream f = getApplicationContext().getAssets().open("img0.jpg"); 
        Image image = new Image(pdf, f, ImageType.JPEG);
        Page page = new Page(pdf, A4.PORTRAIT);
        image.setPosition(0, 0);
        image.drawOn(page);
        pdf.flush();
        fos.close();
    } catch (Exception e)
    {
        e.printStackTrace();
    }

我在这里找到了链接 http://java-source.net/open-source/pdf-libraries

PDFBox uses java awt and swing, even for non UI tasks, I've tried to remove references but there are a lot of files, and I was removing too much stuff

I've just tested PDFjet http://pdfjet.com/os/edition.html it's bsd licensed (plus commercial version with more features), with this sample code (ripped from Example_03.java) I was able to convert a jpeg to a pdf

    FileOutputStream fos = null;
    try
    {
        fos = new FileOutputStream("/sdcard/sample.pdf");
        PDF pdf = new PDF(fos);
        InputStream f = getApplicationContext().getAssets().open("img0.jpg"); 
        Image image = new Image(pdf, f, ImageType.JPEG);
        Page page = new Page(pdf, A4.PORTRAIT);
        image.setPosition(0, 0);
        image.drawOn(page);
        pdf.flush();
        fos.close();
    } catch (Exception e)
    {
        e.printStackTrace();
    }

I found the link here http://java-source.net/open-source/pdf-libraries

酒几许 2025-01-04 17:20:15

PDFBox 的 Android 端口

这里有一个免费的 PDFBox Android 端口:

https://github.com/TomRoush/PdfBox -Android

Android Port of PDFBox

There's a free Android Port of PDFBox available here:

https://github.com/TomRoush/PdfBox-Android

吾家有女初长成 2025-01-04 17:20:15

我认为 apk 文件中不包含库类文件。库类需要转换成dex文件才能被检测到。请参考 http://developer.android.com/guide/developing/building/index.html

i think the library class files are not included in the apk file. The library classes need to be converted into dex files then only it will be detected.please refer http://developer.android.com/guide/developing/building/index.html

So要识趣 2025-01-04 17:20:15

我遇到了同样的错误。我已经通过这种方式解决了,文档说:

Before calls to PDFBox are made it is required to initialize the library's resource loader. Add the following line before calling PDFBox methods:

PDFBoxResourceLoader.init(getApplicationContext());

An example app is located in the sample directory and includes examples of common tasks.

要使用 PDFBox 从 Uri 读取 PDF,我只需执行以下操作:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == ESCOGER_DOCUMENTO_REQUEST_CODE) {
        if (resultCode == RESULT_OK && data != null) {
            Uri archivo = data.getData();
            switch (extensionArchivo(this, archivo)) {
                case "jpg":
                case "png":
                    //Procesar imagen
                    break;
                case "pdf":
                    PDFBoxResourceLoader.init(getApplicationContext());
                    ContentResolver contentResolver = getContentResolver();      // Get a content resolver to access the Uri
                    InputStream inputStream = null;                              // Open an input stream to the Uri
                    try {
                        inputStream = contentResolver.openInputStream(archivo);
                    } catch (IOException e) {
                        //Log.e("PdfBox-Android-Sample", "Exception thrown while loading document to strip", e);
                    }
                    String parsedText = null;                                    // Load the PDF document from the input stream
                    PDDocument document = null;
                    try {
                        document = PDDocument.load(inputStream);
                    } catch (IOException e) {
                        //Log.e("PdfBox-Android-Sample", "Exception thrown while loading document to strip", e);
                    }

                    try {
                        PDFTextStripper pdfStripper = new PDFTextStripper();
                        pdfStripper.setStartPage(0);
                        pdfStripper.setEndPage(1);
                        parsedText = "Parsed text: " + pdfStripper.getText(document);
                    } catch (IOException e) {
                        //Log.e("PdfBox-Android-Sample", "Exception thrown while stripping text", e);
                    } finally {
                        try {
                            if (document != null) document.close();
                        } catch (IOException e) {
                            //Log.e("PdfBox-Android-Sample", "Exception thrown while closing document", e);
                        }
                    }
                    Toast.makeText(this, parsedText, Toast.LENGTH_SHORT).show();
                    break;
                default:
                    // Salir y mostrar mensaje "Archivo no permitido"
            }

        } else {
            Toast.makeText(this, "Error al subir documento", Toast.LENGTH_LONG).show();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

I got the same error. I've solved this way, the documentation says:

Before calls to PDFBox are made it is required to initialize the library's resource loader. Add the following line before calling PDFBox methods:

PDFBoxResourceLoader.init(getApplicationContext());

An example app is located in the sample directory and includes examples of common tasks.

To read a PDF from Uri with PDFBox i just do:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == ESCOGER_DOCUMENTO_REQUEST_CODE) {
        if (resultCode == RESULT_OK && data != null) {
            Uri archivo = data.getData();
            switch (extensionArchivo(this, archivo)) {
                case "jpg":
                case "png":
                    //Procesar imagen
                    break;
                case "pdf":
                    PDFBoxResourceLoader.init(getApplicationContext());
                    ContentResolver contentResolver = getContentResolver();      // Get a content resolver to access the Uri
                    InputStream inputStream = null;                              // Open an input stream to the Uri
                    try {
                        inputStream = contentResolver.openInputStream(archivo);
                    } catch (IOException e) {
                        //Log.e("PdfBox-Android-Sample", "Exception thrown while loading document to strip", e);
                    }
                    String parsedText = null;                                    // Load the PDF document from the input stream
                    PDDocument document = null;
                    try {
                        document = PDDocument.load(inputStream);
                    } catch (IOException e) {
                        //Log.e("PdfBox-Android-Sample", "Exception thrown while loading document to strip", e);
                    }

                    try {
                        PDFTextStripper pdfStripper = new PDFTextStripper();
                        pdfStripper.setStartPage(0);
                        pdfStripper.setEndPage(1);
                        parsedText = "Parsed text: " + pdfStripper.getText(document);
                    } catch (IOException e) {
                        //Log.e("PdfBox-Android-Sample", "Exception thrown while stripping text", e);
                    } finally {
                        try {
                            if (document != null) document.close();
                        } catch (IOException e) {
                            //Log.e("PdfBox-Android-Sample", "Exception thrown while closing document", e);
                        }
                    }
                    Toast.makeText(this, parsedText, Toast.LENGTH_SHORT).show();
                    break;
                default:
                    // Salir y mostrar mensaje "Archivo no permitido"
            }

        } else {
            Toast.makeText(this, "Error al subir documento", Toast.LENGTH_LONG).show();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文