2016-11-21 91 views
-1

我嘗試了很多在互聯網上搜索,但我沒有找到任何解決方案。我的問題是我有一個名爲main.xml的佈局,其父佈局是一個LinearLayout,它可以滾動。我想生成該佈局的PDF。佈局由報告組成,所以我想以pdf格式導出這些報告。我該怎麼做請幫忙。如何將xml佈局轉換爲pdf中的android

+0

參考此鏈接http://stackoverflow.com/questions/29730402/how-to-convert-android-view-to- pdf – Vadivel

+0

它給我的PDF屏幕的四分之一。我沒有得到完整的寬度和高度 –

回答

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(); 
}