Android之PDF预览与创建

来源:互联网 发布:淘宝大促活动 编辑:程序博客网 时间:2024/06/06 03:22

预览

方式一: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);}
原创粉丝点击