问题描述
我显示网页
在的WebView
。现在如何创建的WebView PDF ?
例如:的WebView加载网址是www.google.co.in
。现在,如何将本网页保存为图片,并创建PDF ??
For Example : webview loads URL is "www.google.co.in"
. Now how to save this webpage as image and create pdf ??
我已搜查,但没有发现。
I have searched but nothing find.
请帮忙。
推荐答案
试试这样
的WebView有内置的方法叫做 setPictureListener
使用该方法如下
WebView have inbuilt method called setPictureListener
Use that method as below
webView1.setPictureListener(new PictureListener() {
public void onNewPicture(WebView view, Picture picture) {
if (picture != null) {
try {
bmp = pictureDrawable2Bitmap(new PictureDrawable(
picture));
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
有关获取位图我用 pictureDrawable2Bitmap
,这里是一个
For obtaining bitmap i have used pictureDrawable2Bitmap
and here is that one
private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable) {
Bitmap bitmap = Bitmap.createBitmap(
pictureDrawable.getIntrinsicWidth(),
pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
return bitmap;
}
现在你的位图是准备好了,现在设置的WebView客户端如下
Now Your Bitmap is ready,Now set webview client as below
webView1.setWebViewClient(new myWebClient());
这里是 myWebClient
public class myWebClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
Log.i("OnPageLoadFinished", url);
img.setImageBitmap(bmp);
}
由于在页面加载完成后显示我已设置图像的位图是当前加载的url扣在你的WebView
As shown on page load finished i have set image bitmap which is snap of current loaded url on your webview
现在位图已经准备好传位图使用iText库全文
Now Bitmap is ready pass that bitmap to Pdf using IText Library
下面是写的PDF与图像上的iText使用以下函数,该函数的例子
Here is an example of writing pdf with image on iText Use Below function for that
public void SimplePDFTable() throws Exception {
File direct = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/AamirPDF");
if (!direct.exists()) {
if (direct.mkdir()) {
Toast.makeText(MainActivity.this,
"Folder Is created in sd card", Toast.LENGTH_SHORT)
.show();
}
}
String test = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/AamirPDF";
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(test
+ "/mypdf.pdf"));
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Image image = Image.getInstance(byteArray);
image.scaleToFit(PageSize.A4.getHeight(), PageSize.A4.getWidth());
document.add(image);
document.close();
}
好运
这篇关于如何在Android中创建的WebView PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!