2016-06-17 79 views
0

首先,我是法國人,我會盡我所能用英語解釋我的問題。我如何在PDF中獲得我的完整活動

所以,我想在的PDF轉換畫面:listeTrouActivity.java

但你可以看到,有滾動視圖和孔柱正在下降到18洞。

所以我跟着一些教程:

http://valokafor.com/how-to-convert-android-view-to-pdf/#comment-1018

我開始使用的庫ITextG

但我的問題是,當我將我的位圖轉換成PDF它給我的只是一個PDF我的屏幕大小。我的活動完全被削減。

所以,我希望你們能幫助我與,有我的代碼,將我的位圖到我的活動PDF叫ListTrouActivity.java:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    CoursePdf coursePdf = new CoursePdf(); 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    if (id == R.id.saveHasPDF) { 
     View mRootView = findViewById(R.id.RootView); 
     String state = Environment.getExternalStorageState(); 
     if (!Environment.MEDIA_MOUNTED.equals(state)){ 
      Toast.makeText(ListTrouActivity.this, "OK", Toast.LENGTH_SHORT).show(); 
     } 

     File pdfDir = new File(DEFAULT_PATH, "MyApp"); 
     if (!pdfDir.exists()){ 
      pdfDir.mkdirs(); 
     } 

     LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); 
     LinearLayout root = (LinearLayout) inflater.inflate(R.layout.liste_trou_activity, null); //RelativeLayout is root view of my UI(xml) file. 
     root.setDrawingCacheEnabled(true); 
     Bitmap screen = coursePdf.getBitmapFromView2(this.getWindow().findViewById(R.id.RootView)); 

     // here give id of our root layout (here its my RelativeLayout's id) 

     File pdfFile = new File(pdfDir, "myPdfFile.pdf"); 

     try { 
      Rectangle pagesize = new Rectangle(1720f, 3200f); 
      Document document = new Document(pagesize, 36f, 72f, 108f, 180f); 

      PdfWriter.getInstance(document, new FileOutputStream(pdfFile)); 
      document.open(); 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      screen.compress(Bitmap.CompressFormat.PNG, 100, stream); 
      byte[] byteArray = stream.toByteArray(); 

      coursePdf.addImage(document,byteArray); 
      document.close(); 
     } 
     catch (Exception e){ 
      e.printStackTrace(); 
     } 

     Intent intent = new Intent(Intent.ACTION_VIEW); 
     Uri uri = Uri.fromFile(new File(pdfDir, "myPdfFile.pdf")); 
     intent.setDataAndType(uri, "application/pdf"); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
     startActivity(intent); 
    } 
    return super.onOptionsItemSelected(item); 
} 

代碼getBitmapFromView在CoursePDF.java:

在我CoursePDF.java
public static Bitmap getBitmapFromView2(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; 
} 

代碼addImage:

public static void addImage(Document document, byte[] byteArray) 
{ 
    Image image = null; 
    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(); 
    } 
} 

Unfortun我不能過去我的liste_trou_activity.xml代碼,否則我會達到157 000個字符。

如果你們真的需要它,只要問一問,我會再試一次。

我希望你們都明白我在問什麼,如果沒有,就再問一遍,我會再試一次。

感謝您的閱讀,希望你們能幫助我。

回答

0

看看我能想到的最好的是拍攝完整的可滾動活動的快照,然後動態地將其內容作爲快照來編寫pdf。 對於快照偏好[拍攝快照的可滾動活動]

然後寫入動態pdf。你會得到你想要的東西

+0

但是,你如何拿出一個可滾動活動的快車?這或多或少是我不能做的... – Vascote

+0

http://android.stackexchange.com/questions/35012/how-to-take-a-screenshot-of-an-entire-scrolling-activity閱讀鏈接你會得到它.. –

+0

謝謝,我會嘗試。 – Vascote

相關問題