如何将 PDFBox 添加到 Android 项目或建议替代方案 [英] How to add PDFBox to an Android project or suggest alternative

查看:26
本文介绍了如何将 PDFBox 添加到 Android 项目或建议替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我想试试 PDFBox.由于许可条款/价格,iTextPDF 等其他解决方案不适合我们公司.

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.

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

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.

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

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

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

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

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?

谢谢,

推荐答案

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

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

我刚刚测试了 PDFjet http://pdfjet.com/os/edition.html 它是 bsd 许可的(加上具有更多功能的商业版本),使用此示例代码(从 Example_03.java 中提取),我能够将 jpeg 转换为 pdf

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();
    }

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

这篇关于如何将 PDFBox 添加到 Android 项目或建议替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆