-1
我尝试了很多在互联网上搜索,但我没有找到任何解决方案。我的问题是我有一个名为main.xml的布局,其父布局是一个LinearLayout,它可以滚动。我想生成该布局的PDF。布局由报告组成,所以我想以pdf格式导出这些报告。我该怎么做请帮忙。如何将xml布局转换为pdf中的android
我尝试了很多在互联网上搜索,但我没有找到任何解决方案。我的问题是我有一个名为main.xml的布局,其父布局是一个LinearLayout,它可以滚动。我想生成该布局的PDF。布局由报告组成,所以我想以pdf格式导出这些报告。我该怎么做请帮忙。如何将xml布局转换为pdf中的android
最后,我得到了我的问题的解决方案。现在我可以使用itextpdf.jar文件轻松地将任何视图转换为PDF。我会在这里发布我的代码。该方法将以位图格式保存视图。
public void saveViewImage(View view){
File file = saveBitMap(this, layout); //which view you want to pass that view as parameter
if (file != null) {
Log.i("TAG", "Drawing saved to the gallery!");
} else {
Log.i("TAG", "Oops! Image could not be saved.");
}
}
private File saveBitMap(Context context, View drawView){
File pictureFileDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Handcare");
if (!pictureFileDir.exists()) {
boolean isDirectoryCreated = pictureFileDir.mkdirs();
if(!isDirectoryCreated)
Log.i("ATG", "Can't create directory to save the image");
return null;
}
String filename = pictureFileDir.getPath() +File.separator+ System.currentTimeMillis()+".jpg";
File pictureFile = new File(filename);
Bitmap bitmap =getBitmapFromView(drawView);
try {
createPdf(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
pictureFile.createNewFile();
FileOutputStream oStream = new FileOutputStream(pictureFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, oStream);
oStream.flush();
oStream.close();
} catch (IOException e) {
e.printStackTrace();
Log.i("TAG", "There was an issue saving the image.");
}
scanGallery(context,pictureFile.getAbsolutePath());
return pictureFile;
}
//create bitmap from view and returns it
private 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;
}
现在下面方法被用来生成PDF
private void createPdf(Bitmap bitmap) throws IOException, DocumentException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image signature;
signature = Image.getInstance(stream.toByteArray());
signature.setAbsolutePosition(50f, 100f);
signature.scalePercent(60f);
//Image image = Image.getInstance(byteArray);
File pdfFolder = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOCUMENTS), "pdfdemo");
if (!pdfFolder.exists()) {
pdfFolder.mkdirs();
Log.i("Created", "Pdf Directory created");
}
//Create time stamp
Date date = new Date() ;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date);
File myFile = new File(pdfFolder + timeStamp + ".pdf");
OutputStream output = new FileOutputStream(myFile);
//Step 1
Document document = new Document();
//Step 2
PdfWriter.getInstance(document, output);
//Step 3
document.open();
//Step 4 Add content
document.add(signature);
//document.add(new Paragraph(text.getText().toString()));
//document.add(new Paragraph(mBodyEditText.getText().toString()));
//Step 5: Close the document
document.close();
}
参考此链接http://stackoverflow.com/questions/29730402/how-to-convert-android-view-to- pdf – Vadivel
它给我的PDF屏幕的四分之一。我没有得到完整的宽度和高度 –