2016-06-17 43 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

相关问题