Android 自带PDF SDK

从6.0开始,Android自带了PDF功能库:

PdfDocument from API 19

This class enables generating a PDF document from native Android content. You create a new document and then for every page you want to add you start a page, write content to the page, and finish the page. After you are done with all pages, you write the document to an output stream and close the document. After a document is closed you should not use it anymore. Note that pages are created one by one, i.e. you can have only a single page to which you are writing at any given time. This class is not thread safe.

A typical use of the APIs looks like this:

// create a new document

PdfDocument document = new PdfDocument();

// crate a page description

PageInfo pageInfo = new PageInfo.Builder(new Rect(0, 0, 100, 100), 1).create();

// start a page Page page = document.startPage(pageInfo);

// draw something on the page

View content = getContentView(); content.draw(page.getCanvas());

// finish the page document.finishPage(page); . . .

// add more pages . . .

// write the document content

document.writeTo(getOutputStream());

// close the document

document.close();

PdfRenderer from API 21

This class enables rendering a PDF document. This class is not thread safe.

If you want to render a PDF, you create a renderer and for every page you want to render, you open the page, render it, and close the page. After you are done with rendering, you close the renderer. After the renderer is closed it should not be used anymore. Note that the pages are rendered one by one, i.e. you can have only a single page opened at any given time.

A typical use of the APIs to render a PDF looks like this:

// create a new renderer

PdfRenderer renderer = new PdfRenderer(getSeekableFileDescriptor());

// let us just render all pages

final int pageCount = renderer.getPageCount();

for (int i = 0; i < pageCount; i++) {

    Page page = renderer.openPage(i);

    // say we render for showing on the screen

    page.render(mBitmap, null, null, Page.RENDER_MODE_FOR_DISPLAY);

    // do stuff with the bitmap

    // close the page

    page.close();

}

// close the renderer

renderer.close();

一个实例(创建一个pdf文件,包含两个页面,分别在第一页上画一个蓝色的圆,在第二页上画一个红色的圆以及一段字符串):

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.pdf.PdfDocument;

....

public void createPDF(View view) {

// create a new document

    PdfDocument document =new PdfDocument();

// crate a page description

    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(100,100,1).create();

// start a page

    PdfDocument.Page page = document.startPage(pageInfo);

    Canvas canvas = page.getCanvas();

    Paint paint =new Paint();

    paint.setColor(Color.RED);

    canvas.drawCircle(50,50,30, paint);

// finish the page

    document.finishPage(page);

// Create Page 2

    pageInfo =new PdfDocument.PageInfo.Builder(500,500,2).create();

    page = document.startPage(pageInfo);

    canvas = page.getCanvas();

    paint =new Paint();

    paint.setColor(Color.BLUE);

    canvas.drawCircle(200,200,100, paint);

    canvas.drawText("Text test",20.0f,200.0f,paint);

    document.finishPage(page);

    File file =new File(Environment.getExternalStorageDirectory(), "test.pdf");

// save the file

    try {

        document.writeTo(new FileOutputStream(file));

        Toast.makeText(this,"Done", Toast.LENGTH_LONG).show();

    }catch (IOException e) {

        e.printStackTrace();

        Toast.makeText(this,"Something wrong: " + e.toString(),

        Toast.LENGTH_LONG).show();

    }

// close the document

    document.close();

}

注: 此SDK 实际是在空白PDF页面中绘制所需的图像,页面的尺寸,背景通过pageinfo调整,画笔颜色通过paint调整,文本格式以及内容通过canvas调整。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,129评论 0 10
  • Awesome Ruby Toolbox Awesome A collection of awesome Ruby...
    debbbbie阅读 7,978评论 0 3
  • 关于爱情,这两个字被世人从古至今无数次用歌去唱,用诗词去吟诵,用琴弦在弹指之间拨动出动人的旋律,用笔素描出...
    简芯祥佳阅读 2,224评论 0 1
  • 早上醒来,拉开窗帘,外面并没有司空见惯的阳光。我抑制住即将要迸发出的惊喜,向地上看去,果然,下雨了。 回来差不多一...
    众人国士阅读 721评论 0 0
  • (在《以诗之名》里作者姬澜若的《凤凰台上忆吹箫——独忆》中的评论。原诗修改) 五岳走遍 山河如此娇艳 众友当歌 汗...
    守好本心追寻大唐远行僧阅读 3,715评论 18 38