pdf sdk for android,Android 自带PDF SDK

这篇博客介绍了如何从Android 6.0开始利用PdfDocument和PdfRenderer API创建和渲染PDF文件。首先,通过PdfDocument生成PDF文档,逐页添加内容,然后写入输出流并关闭文档。接着,使用PdfRenderer打开PDF进行渲染,每页渲染后关闭。示例代码展示了创建一个包含两个页面的PDF,第一页画一个红色圆圈,第二页则画一个蓝色圆圈及一段文字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

优看PDFSDK For Android是优看科技出品的Android平台下PDF底层程序库,提供PDF及TXT阅读器最基础的功能,适合需要高级定制功能的开发者。优看PDF SDK For Android提供Android java接口,允许开发人员将PDF显示、TXT显示、导航、搜索、笔记、平滑翻页等功能无缝地集成到Android应用程序中。如果需要iOS版PDFSDK请访问http://www.ycanpdf.cn/2017/04/623。 此外,我们还提供基于以上核心技术的定制开发服务,开发人员可通过所提供的示例代码快速上手,更专注于程序开发而不是PDF及TXT,让您的应用快速投入市场。 以下简单介绍优看PDFSDK For AndroidAndroid平台上的具体应用实例: - 支持PDF及TXT基本阅读功能(缩放、目录跳转、指定页跳转、搜索、记录阅读进度等) - 支持文字选择,添加笔记,显示书签等操作 - 支持画线,箭头,椭圆等功能(PDF文件) - 支持翻页动画:滑动 - 支持夜间模式 - 支持设置背景色,字体大小,行间距(TXT文件) - 支持打开受标准密码保护的PDF文件 优看PDF SDK Android版本支持下列操作环境: - 运行环境:Android4.0或更高版本 - 支持的语言:Java 本公司经过近几年的持续的技术积累,产品线有了较大拓展,除了原有的PDF在线阅读有了较大优化和更新(目前已经能够支持Google Chrome、Mozilla Firefox、苹果Safari、Opera等几乎所有主流的浏览器)以外,还推出了以下几款新产品: 1、移动端阅读系列:包含PDF、EPUB、TXT等格式的阅读器和SDK,支持安卓和IOS两个平台; 2、网上书城、网上党员(公职人员)教育学习平台,同时支持PC、安卓、IOS,支持DRM(数字版权保护)功能; 3、PDF类转换控件、PDF电子签章控件等PDF扩展应用。 商务代理/合作联系方式: 联系电话:400-092-1680 029-88869745 QQ:1003059540 E-mail:sales@ycanpdf.cn
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值