从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调整。