如何将Android View转换为PDF [英] How to convert Android View to PDF
问题描述
我创建了一个Android发票应用.生成的发票是带有嵌套视图的标准Android布局.我正在寻找一个可用于将该视图转换为pdf文档的库.
I have created an Android Invoice app. The generated invoice is standard Android layout with nested views. I am looking for a library that I can use to convert this view to an pdf document.
令我惊讶的是,我的搜索中没有直截了当的选择,或者也许我已经做完了第一件事.也许我要找的东西是不可能的.
I am surprised there is no straight forward option coming up in my search or Perhaps I have the done the first thing last. Or perhaps what I am looking for is not possible.
请有人帮我指出一个可以帮助我从Android视图转换或生成PDF的工具.我对免费和适度的付费选项持开放态度.或者让我知道,如果我找不到想要的东西.
Would someone please help point me to a tool that will help me convert or generate a PDF from an Android view. I am open to free and modest paid option. Or let me know is if what I am looking for is not possible.
推荐答案
在设备上查看屏幕:
Bitmap screen;
View v1 = MyView.getRootView();
v1.setDrawingCacheEnabled(true);
screen= Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
如果您将ScrollView
作为根视图,则:
If you're having ScrollView
as root view then:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
RelativeLayout root = (RelativeLayout) inflater.inflate(R.layout.activity_main, null); //RelativeLayout is root view of my UI(xml) file.
root.setDrawingCacheEnabled(true);
Bitmap screen= getBitmapFromView(this.getWindow().findViewById(R.id.relativelayout)); // here give id of our root layout (here its my RelativeLayout's id)
这是getBitmapFromView()
方法:
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
它将显示整个屏幕,包括隐藏在ScrollView
中的内容.
现在我们有了位图屏幕,让我们将其保存为pdf(您必须下载 itextpdf- 5.3.2.jar 文件并附加到您的项目中.)
It will display entire screen including content hidden in your ScrollView
.
Now that we have our bitmap screen let's save it to pdf (you have to download itextpdf-5.3.2.jar file and attach in your project..)
private static String FILE = "mnt/sdcard/invoice.pdf"; // add permission in your manifest...
try
{
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
screen.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
addImage(document,byteArray);
document.close();
}
catch (Exception e)
{
e.printStackTrace();
}
private static void addImage(Document document,byte[] byteArray)
{
try
{
image = Image.getInstance(byteArray);
}
catch (BadElementException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// image.scaleAbsolute(150f, 150f);
try
{
document.add(image);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
没有测试任何东西.这是我使用过的所有资源: source1 , source2 ,
Haven't tested anything. Here are all the sources I used: source1, source2, source3.
这篇关于如何将Android View转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!