我在互联网上尝试了很多搜索,但我没有找到任何有关此问题的解决方案。我的问题是我有一个名为main.xml的布局,其父布局是一个LinearLayout,它是可滚动的。我想生成该布局的pdf。布局由报告组成,因此我想以pdf格式导出这些报告。我该怎么办请帮助。
答案 0 :(得分:1)
您可以使用此library简化几行代码中的操作-
PdfGenerator.getBuilder()
.setContext(context)
.fromLayoutXMLSource()
.fromLayoutXML(R.layout.layout_your_scroll_view)
.setDefaultPageSize(PdfGenerator.PageSize.A4)
.setFileName("Test-PDF")
.build();
您还可以在其中设置膨胀式滚动视图实例(单个或多个视图)。此外,您还可以在PdfGeneratorListener()
中传递回调(.build()
),以通知pdf生成是由于异常完成还是失败
答案 1 :(得分:0)
最后我得到了解决问题的方法。现在,我可以使用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();
}