从Android的多页的WebView创建PDF文档 [英] Android create pdf document from webview with multiple pages

查看:252
本文介绍了从Android的多页的WebView创建PDF文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

即时通讯使用于Android的PdfDocument框架(链接)创建从我的WebView内容的PDF文档。在PDF创建很好,但它只有一个页文档。当视图内容大我需要创建多页文档。我需要的是要拆分视图内容中的页面。我怎样才能做到这一点?
我不想使用的iText或任何第三方库。

需要帮助,请。先谢谢了。

  //创建一个新文档
PdfDocument文档=新PdfDocument();//创建一个页面描述
PdfDocument.PageInfo pageInfo =新PdfDocument.PageInfo.Builder(宽度,高度,1).create();//启动页
PdfDocument.Page页= document.startPage(pageInfo);//画东西在页面上
查看内容= myWebview;
content.draw(page.getCanvas());//完成页面
document.finishPage(页);FileOutputStream中FOS;
尝试{
    FOS =新的FileOutputStream(fileNameWithPath,FALSE);
    //写文档内容
    document.writeTo(FOS);}赶上(FileNotFoundException异常五){
    e.printStackTrace();
}赶上(IOException异常五){
    e.printStackTrace();
}//关闭文档
document.close();


解决方案

如果您要创建多个页面,然后只需调用起始页()和finishPage()为你希望你的文档中创建的每一页。结果
事情是这样的:

  //创建文档
PdfDocument文档=新PdfDocument();//创建一个页面描述
PdfDocument.PageInfo pageInfo =新PdfDocument.PageInfo.Builder(宽度,高度,1).create();//启动第1页
PdfDocument.Page页= document.startPage(pageInfo);
//画东西在页面上
查看内容= myWebview;
content.draw(page.getCanvas());
//结束第1页
document.finishPage(页);//启动第2页
PdfDocument.Page页= document.startPage(pageInfo);
//画东西在页面上
查看内容= someOtherWebview;
content.draw(page.getCanvas());
//完成第2页
document.finishPage(页);// 等等...FileOutputStream中FOS;
尝试{
    FOS =新的FileOutputStream(fileNameWithPath,FALSE);
    //写文档内容
    document.writeTo(FOS);}赶上(FileNotFoundException异常五){
    e.printStackTrace();
}赶上(IOException异常五){
    e.printStackTrace();
}//关闭文档
document.close();

Im using the PdfDocument framework from Android (link) to create a pdf document from my webview content. The pdf is created well but it is only one page document. When the webview content is large i need to create a multipage document. ALL I NEED IS TO SPLIT WEBVIEW CONTENT IN PAGES. How can i achieve this? I dont want to use iText or any third party library.

Need help please. Thanks in advance.

// create a new document
PdfDocument document = new PdfDocument();

// create a page description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(width, height, 1).create();

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

// draw something on the page
View content = myWebview;
content.draw(page.getCanvas());

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

FileOutputStream fos;
try {
    fos = new FileOutputStream(fileNameWithPath, false);
    // write the document content
    document.writeTo(fos);

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

// close the document
document.close();

解决方案

If you want to create multiple pages then just call startPage() and finishPage() for every page that you want to create in your document.
Something like this :

// create document
PdfDocument document = new PdfDocument();

// create a page description
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(width, height, 1).create();

// start 1st page
PdfDocument.Page page = document.startPage(pageInfo);
// draw something on the page
View content = myWebview;
content.draw(page.getCanvas());
// finish 1st page
document.finishPage(page);

// start 2nd page
PdfDocument.Page page = document.startPage(pageInfo);
// draw something on the page
View content = someOtherWebview;
content.draw(page.getCanvas());
// finish 2nd page
document.finishPage(page);

// and so on...

FileOutputStream fos;
try {
    fos = new FileOutputStream(fileNameWithPath, false);
    // write the document content
    document.writeTo(fos);

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

// close the document
document.close();

这篇关于从Android的多页的WebView创建PDF文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆