是否有任何兼容的android库将PDF页面转换为图像?我发现了几个适合我需求的java库(pdfbox,jpod,jpedal,icepdf),但他们给我编译错误(基于awT或swt)。我目前正在android上制作pdf查看器,如果我不必从头开始编写pdf解码,我会很棒。用于从pdf文件创建图像的Android兼容库?
回答
最好的办法是这个库:http://code.google.com/p/apv/
这是一个C项目,所以你需要使用JNI调用。
这是基于MuPDF这是AGPL许可证,并不完全兼容商业软件。只是FYI ... – startoftext 2014-12-12 21:04:00
感谢您的帮助。我检查了项目并使用了MuPDF库(APV项目基于此库)。它工作正常!非常感谢上帝保佑。
使用itextpdf5.3.1.jar
下面是从PDF提取图像的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);`enter code here`
PdfReader reader;
File file = new File("/sdcard/vineeth/anni.prc");
try{
reader = new PdfReader(file.getAbsolutePath());
for (int i = 0; i < reader.getXrefSize(); i++) {
PdfObject pdfobj= reader.getPdfObject(i);
if (pdfobj == null || !pdfobj.isStream()) {
continue;
}
PdfStream stream = (PdfStream) pdfobj;
PdfObject pdfsubtype = stream.get(PdfName.SUBTYPE);
if (pdfsubtype != null && pdfsubtype.toString().equals(PdfName.IMAGE.toString())) {
byte[] img = PdfReader.getStreamBytesRaw((PRStream) stream);
FileOutputStream out = new FileOutputStream(new
File(file.getParentFile(),String.format("%1$05d", i) + ".jpg"));
out.write(img); out.flush(); out.close();
}
}
} catch (Exception e) { }
}
你应该提供一些解释和代码。 – 2012-10-01 18:04:53
上面的代码仅包含pdf中的_extracts_图像,它不会生成PDF文件的图像。 – Minsky 2014-08-07 08:16:34
以上答案都不帮我,所以我写我的解决方案。
使用这个库:android-pdfview和下面的代码,就可以可靠地转换的PDF页面转换成图片(JPG,PNG):
DecodeServiceBase decodeService = new DecodeServiceBase(new PdfContext());
decodeService.setContentResolver(mContext.getContentResolver());
// a bit long running
decodeService.open(Uri.fromFile(pdf));
int pageCount = decodeService.getPageCount();
for (int i = 0; i < pageCount; i++) {
PdfPage page = decodeService.getPage(i);
RectF rectF = new RectF(0, 0, 1, 1);
// do a fit center to 1920x1080
double scaleBy = Math.min(AndroidUtils.PHOTO_WIDTH_PIXELS/(double) page.getWidth(), //
AndroidUtils.PHOTO_HEIGHT_PIXELS/(double) page.getHeight());
int with = (int) (page.getWidth() * scaleBy);
int height = (int) (page.getHeight() * scaleBy);
// you can change these values as you to zoom in/out
// and even distort (scale without maintaining the aspect ratio)
// the resulting images
// Long running
Bitmap bitmap = page.renderBitmap(with, height, rectF);
try {
File outputFile = new File(mOutputDir, System.currentTimeMillis() + FileUtils.DOT_JPEG);
FileOutputStream outputStream = new FileOutputStream(outputFile);
// a bit long running
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.close();
} catch (IOException e) {
LogWrapper.fatalError(e);
}
}
你应该通过使用AsyncTask
或IE在后台做这个工作类似于相当多的方法需要计算或IO时间(我在注释中标记了它们)。
- 1. 从pdf创建图像
- 2. 创建图像文件,android
- 3. 用于从HTML生成PDF的JavaScript库(与Phonegap兼容)
- 4. 用文件内容创建图像
- 5. 用于STEP的Java(p21)中的API - 创建STEP兼容文件
- 6. 使用iText从TIFF图像创建PDF
- 7. 使用TCPDF从PDF创建图像
- 8. 创建CoRD兼容的RDP文件
- 9. 用图像创建PDF
- 10. 如何从Python创建PDF文件,包括图像和文本?
- 11. 用数据库中的内容创建PDF文件
- 12. 从数据库创建PDF和文件夹中的图片
- 13. 用于图像创建的Scala库
- 14. 用于创建.TMB图像的库?
- 15. onView使用兼容库创建
- 16. iText是否创建基于图像或基于文本的PDF文件?
- 17. 允许用户创建PDF或图像文件的Flash插件
- 18. Android试图下载数据库创建的图像文件
- 19. 从NSMutableData创建PDF文件
- 20. Java:使用PDFBox库从图像创建PDF页面
- 21. 如何使用PDFBox for Android从tiff图像创建PDF?
- 22. 适用于Android的GPL兼容图形库
- 23. iphone从pdf页面创建PDF文件
- 24. 如何从Android中的图像文件创建位图
- 25. ImageMagick从图像创建PDF版本1.4?
- 26. iphone从图像集创建pdf
- 27. 从图像列表中创建PDF
- 28. ImageMagick或Ghostscript:从PDF创建图像
- 29. Coldfusion:从图像创建PDF非常大
- 30. 用php创建图像重pdf格式的文件
尝试此:http://stackoverflow.com/a/16294833/2027232 – 2013-04-30 07:58:36