如何将PDF页面转换为Android中的图像?

我需要做的就是取一个(本地保存的) PDF-document并将其中的一个或全部页面转换为JPG或PNG等图像格式。

我已经尝试了很多的PDF渲染/查看解决scheme,如APV PDF查看器 , APDFViewer , droidreader , androidpdf , MuPdf和许多其他,但到目前为止还不知道如何将PDF页面转换为图像?

编辑:另外,我宁愿有一个PDF格式的图像转换器,而不是PDF格式的渲染器,我需要编辑将PDF转换为图像。

Solutions Collecting From Web of "如何将PDF页面转换为Android中的图像?"

你需要看看这个开源项目的相同的要求,这可以帮助你做更多的事情也。

项目: PdfRenderer

pdfview包中有一个名为PDFPage.java的 Java类。 该类有一个方法来获取页面的图像。

我也在我的testing项目中实现了相同的function,并且java代码在这里给你 。 我创build了一个方法showPage ,它接受页面号和缩放级别,并将该页面作为Bitmap返回。

希望这可以帮到你。 您只需要获取该项目或JAR,阅读完整的JAVADOC文档,然后尝试和执行相同的操作。

把你的时间,快乐编码:)

要支持API 8及更高版本,请按照

使用这个库: 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或类似的方法,因为有很多方法需要计算或IO时间(我已经在注释中标记了它们)。

从Android API 21开始PdfRenderer是你在找什么。

我会说你一个简单的伎俩不是一个完整的解决scheme。一旦成功呈现PDF页面,您将获得其屏幕的位图如下

 View view = MuPDFActivity.this.getWindow().getDecorView(); if (false == view.isDrawingCacheEnabled()) { view.setDrawingCacheEnabled(true); } Bitmap bitmap = view.getDrawingCache(); 

然后你可以保存这个位图,这就是你的PDF页面在本地的图像

 try { new File(Environment.getExternalStorageDirectory()+"/PDF Reader").mkdirs(); File outputFile = new File(Environment.getExternalStorageDirectory()+"/PDF Reader", System.currentTimeMillis()+"_pdf.jpg"); FileOutputStream outputStream = new FileOutputStream(outputFile); // a bit long running bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.close(); } catch (IOException e) { Log.e("During IMAGE formation", e.toString()); } 

就这些,希望你能帮上忙。

最后我发现这个很简单的解决scheme,从这里下载库。

使用下面的代码从PDF获取图像:

 import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.RectF; import android.net.Uri; import android.os.AsyncTask; import android.os.Environment; import android.provider.MediaStore; import org.vudroid.core.DecodeServiceBase; import org.vudroid.core.codec.CodecPage; import org.vudroid.pdfdroid.codec.PdfContext; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.ArrayList; /** * Created by deepakd on 06-06-2016. */ public class PrintUtils extends AsyncTask<Void, Void, ArrayList<Uri>> { File file; Context context; ProgressDialog progressDialog; public PrintUtils(File file, Context context) { this.file = file; this.context = context; } @Override protected void onPreExecute() { super.onPreExecute(); // create and show a progress dialog progressDialog = ProgressDialog.show(context, "", "Please wait..."); progressDialog.show(); } @Override protected ArrayList<Uri> doInBackground(Void... params) { ArrayList<Uri> uris = new ArrayList<>(); DecodeServiceBase decodeService = new DecodeServiceBase(new PdfContext()); decodeService.setContentResolver(context.getContentResolver()); // a bit long running decodeService.open(Uri.fromFile(file)); int pageCount = decodeService.getPageCount(); for (int i = 0; i < pageCount; i++) { CodecPage page = decodeService.getPage(i); RectF rectF = new RectF(0, 0, 1, 1); // do a fit center to A4 Size image 2480x3508 double scaleBy = Math.min(UIUtils.PHOTO_WIDTH_PIXELS / (double) page.getWidth(), // UIUtils.PHOTO_HEIGHT_PIXELS / (double) page.getHeight()); int with = (int) (page.getWidth() * scaleBy); int height = (int) (page.getHeight() * scaleBy); // Long running Bitmap bitmap = page.renderBitmap(with, height, rectF); try { OutputStream outputStream = FileUtils.getReportOutputStream(System.currentTimeMillis() + ".JPEG"); // a bit long running bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream); outputStream.close(); // uris.add(getImageUri(context, bitmap)); uris.add(saveImageAndGetURI(bitmap)); } catch (IOException e) { e.printStackTrace(); } } return uris; } @Override protected void onPostExecute(ArrayList<Uri> uris) { progressDialog.hide(); //get all images by uri //ur implementation goes here } public void shareMultipleFilesToBluetooth(Context context, ArrayList<Uri> uris) { try { Intent sharingIntent = new Intent(); sharingIntent.setAction(Intent.ACTION_SEND_MULTIPLE); sharingIntent.setType("image/*"); // sharingIntent.setPackage("com.android.bluetooth"); sharingIntent.putExtra(Intent.EXTRA_STREAM, uris); context.startActivity(Intent.createChooser(sharingIntent,"Print PDF using...")); } catch (Exception e) { e.printStackTrace(); } } private Uri saveImageAndGetURI(Bitmap finalBitmap) { String root = Environment.getExternalStorageDirectory().toString(); File myDir = new File(root + "/print_images"); myDir.mkdirs(); String fname = "Image-"+ MathUtils.getRandomID() +".jpeg"; File file = new File (myDir, fname); if (file.exists ()) file.delete (); try { FileOutputStream out = new FileOutputStream(file); finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } return Uri.parse("file://"+file.getPath()); } } 

FileUtils.java

 package com.airdata.util; import android.net.Uri; import android.os.Environment; import android.support.annotation.NonNull; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStream; /** * Created by DeepakD on 21-06-2016. */ public class FileUtils { @NonNull public static OutputStream getReportOutputStream(String fileName) throws FileNotFoundException { // create file File pdfFolder = getReportFilePath(fileName); // create output stream return new FileOutputStream(pdfFolder); } public static Uri getReportUri(String fileName) { File pdfFolder = getReportFilePath(fileName); return Uri.fromFile(pdfFolder); } public static File getReportFilePath(String fileName) { /*File file = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DOWNLOADS), FileName);*/ File file = new File(Environment.getExternalStorageDirectory() + "/AirPlanner/Reports"); //Create report directory if does not exists if (!file.exists()) { //noinspection ResultOfMethodCallIgnored file.mkdirs(); } file = new File(Environment.getExternalStorageDirectory() + "/AirPlanner/Reports/" + fileName); return file; } } 

您可以在图库或SD卡中查看转换的图像。 请让我知道你是否需要任何帮助。