2016-06-11 69 views
2

我正在使用Android Studio並試圖讓我的應用程序生成PDF。我已經設置了一個活動來生成我希望我的PDF具有的內容。如果我讓它在屏幕上顯示,這工作正常。該活動的onCreate部分低於:Android PDFDocument正在生成空白文檔

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Connect to database 
    connectToDatabase(); 

    // Set view 
    setContentView(R.layout.activity_export_pattern); 

    // find various views and set up their contents 
    // (I've left this bit out as it's quite long, but it doesn't contain 
    // anything controversial) 

    // Now try to export to PDF. 
    // Return success or failure to calling activity 
    // (basically just displays a toast indicating success/failure) 
    try { 
     if(exportToPDF()) { 
      Intent returnIntent = new Intent(); 
      setResult(Activity.RESULT_OK, returnIntent); 
      finish(); 
     }else { 
      Intent returnIntent = new Intent(); 
      setResult(Activity.RESULT_CANCELED, returnIntent); 
      finish(); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Intent returnIntent = new Intent(); 
     setResult(Activity.RESULT_CANCELED, returnIntent); 
     finish(); 
    } 
} 

如果我省略了最後一部分(在try-catch),就讓它在屏幕上顯示,它工作正常。屏幕顯示我期望它顯示的內容。

但是,爲了讓它創建一個PDF,我使用try-catch與調用exportToPDF,它包含以下代碼(這基本上是Android文檔中的代碼,並有一些更改,如註釋所示下面):

public boolean exportToPDF() { 
     // Create PDF document 
     PdfDocument document = new PdfDocument(); 
     // Create page description 
     // Line below changed as Android Studio was highlighting an error; 
     // instead of Rect, I have just put numbers. 
     // I've varied the numbers from 100 up to 1000, to no effect 
     PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(720, 720, 1).create(); 
     // Start page 
     PdfDocument.Page page = document.startPage(pageInfo); 
     // Changed the line below from content = getContentView, as that was causing an error 
     // pageContent is the id of the overall LinearLayout in the XML file 
     // If I break after this in the debugger, content seems to have found the correct view and be populated with the appropriate page elements 
     View content = this.findViewById(R.id.pageContent); 
     // Added the line below after finding it suggested on here in another question 
     // Doesn't seem to make any difference 
     content.layout(0, 0, 200, 200); 
     if (content != null) { 
      content.draw(page.getCanvas()); 
     } 
     // Finish page 
     document.finishPage(page); 

     // Write document content to external storage 
     // I'm using a FileOutputStream instead of BufferedOutputStream as given in the documentation, but, since this does at least produce a file, I don't think this is the source of the problem 
     String filename = this.item.getName() + ".pdf"; 
     File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + filename); 
     FileOutputStream fileOut = null; 
     try { 
      fileOut = new FileOutputStream(file); 
      document.writeTo(fileOut); 
      fileOut.close(); 
      document.close(); 
      return true; 
     } catch (IOException e) { 
      e.printStackTrace(); 
      if(fileOut != null) fileOut.close(); 
      document.close(); 
      return false; 
     } 
    } 

因此,運行這將產生一個pdf在正確的目錄中,正確命名,但它是空白的。我不知道還有什麼可以嘗試,並會感激任何幫助!謝謝!

+0

檢查內容視圖的寬度和hieght – USKMobility

回答

2

在活動中將視圖寫入PDF onCreate()時,它將無法工作,並且因爲您的視圖返回0高度和寬度。您需要等待活動窗口附加,然後將視圖寫入pdf。您可以嘗試從您的活動的onWindowFocusChanged()方法中調用exportToPDF。

public void onWindowFocusChanged(boolean hasFocus) { 
    // TODO Auto-generated method stub 
    super.onWindowFocusChanged(hasFocus); 
try { 
     if(exportToPDF()) { 
      Intent returnIntent = new Intent(); 
      setResult(Activity.RESULT_OK, returnIntent); 
      finish(); 
     }else { 
      Intent returnIntent = new Intent(); 
      setResult(Activity.RESULT_CANCELED, returnIntent); 
      finish(); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Intent returnIntent = new Intent(); 
     setResult(Activity.RESULT_CANCELED, returnIntent); 
     finish(); 
    } 

    } 

您還可以使用ViewTreeObserver

View content = this.findViewById(R.id.pageContent); 
    ViewTreeObserver vto = content.getViewTreeObserver(); 
      vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
       @Override 
       public void onGlobalLayout() { 
       try { 
      if(exportToPDF()) { 
       Intent returnIntent = new Intent(); 
       setResult(Activity.RESULT_OK, returnIntent); 
       finish(); 
      }else { 
       Intent returnIntent = new Intent(); 
       setResult(Activity.RESULT_CANCELED, returnIntent); 
       finish(); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      Intent returnIntent = new Intent(); 
      setResult(Activity.RESULT_CANCELED, returnIntent); 
      finish(); 
     } 
content.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
    } 
      }); 
+0

謝謝 - 我給一個去。 – Sharon

+0

工作!謝謝。我不需要使用第二部分(viewTreeObserver)。只需要現在擺弄頁面和內容大小。再次感謝你的幫助。 – Sharon