預覽
方式一:https://github.com/JoanZapata/android-pdfview
方式二:https://github.com/deepankar1994/MuPDF_For_Android (mupdf)
創建
public abstract class PdfCreator {
private PdfDocument mDocument;
private int mPageWidth = 600;
private int mPageHeight = 400;
/**
*
* @param pageWidth The page width in PostScript (1/72th of an inch).
* @param pageHeight The page height in PostScript (1/72th of an inch).
*/
public PdfCreator(int pageWidth, int pageHeight) {
mDocument = new PdfDocument();
this.mPageWidth = mPageWidth;
this.mPageHeight = mPageHeight;
}
public PdfCreator() {
mDocument = new PdfDocument();
}
/**
* 保存pdf
* @param path
* @throws IOException
*/
public void saveToFile(String path) throws IOException {
mDocument.writeTo(new FileOutputStream(new File(path)));
}
public void createPage(int pageNumber) {
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(mPageWidth, mPageHeight, pageNumber).create();
PdfDocument.Page page = mDocument.startPage(pageInfo);
Canvas canvas = page.getCanvas();
onDrawPage(canvas, pageInfo.getPageNumber());
mDocument.finishPage(page);
}
public void endCreate() {
mDocument.close();
}
public abstract void onDrawPage(Canvas canvas, int pageNum);
public int getPageWidth() {
return mPageWidth;
}
public void setPageWidth(int mPageWidth) {
this.mPageWidth = mPageWidth;
}
public int getPageHeight() {
return mPageHeight;
}
public void setPageHeight(int mPageHeight) {
this.mPageHeight = mPageHeight;
}
}
PDF轉圖片
PdfRenderer renderer = openRenderer(path);
int pageCount = renderer.getPageCount();
for(int i = 0; i < pageCount; i++) {
PdfRenderer.Page page = renderer.openPage(i);
Bitmap bitmap = Bitmap.createBitmap(page.getWidth(), page.getHeight(),
Bitmap.Config.ARGB_8888);
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
}