2016-04-14 14 views
1

Androidアプリケーション用のPDFを生成する方法を学びました。
私は公式の文書に従ったが、コードは機能しなかった。
コンパイラでコンパイルエラーが発生しましたが、コードからコードをコピー/ペーストするしかありませんでした。
(コードを見てみてください、その理由を教えてください:Android PDFの生成

PrintAttributes printAttributes = new PrintAttributes.Builder(). 
     setMediaSize(PrintAttributes.MediaSize.ISO_A4) 
     .setColorMode(PrintAttributes.COLOR_MODE_COLOR) 
     .setMinMargins(PrintAttributes.Margins.NO_MARGINS) 
     .build(); 


PrintedPdfDocument document = new PrintedPdfDocument(this, printAttributes); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 



// crate a page description 
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(new Rect(0, 0, 100, 100), 1).create(); 

// start a page 
PdfDocument.Page page = document.startPage(pageInfo); 

// draw something on the page 
View content = getContentView(); 
content.draw(page.getCanvas()); 

// finish the page 
document.finishPage(page); 
// add more pages 
// write the document content 
document.writeTo(getOutputStream()); 

// close the document 
document.close(); 
+1

貼り付けエラー? – Kathi

+0

PDF生成コードをメソッドにラップする必要があります。 –

+0

@AllanPereiraもし私が生成コードをメソッドにラップすると、私はまだこれらのエラーがあります: https://gyazo.com/d83aff5a0a4afddfc7a10ae85973a633 (エラーはインラインではないので、私はそれをスクリーニングしなければならなかった..) – Matnako

答えて

0
  1. PageInfo.Builderは、あなたが使用するパラメータを取らない

    PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(100,100,1).create();` 
    

    PdfDocument.PageInfo pageInfo = 
         new PdfDocument.PageInfo.Builder(new Rect(0, 0, 100, 100), 1).create(); 
    

    を交換してください。

    のドキュメントを確認してください

  2. が正しいクラスから呼び出されていない、Activityクラスには存在しませんhere

  3. getOutputStream()を確認してください。あなたがしたいのは、ファイルにコンテンツをコピーすることです、あなたはおそらく新しいファイルを作成し、そのファイルに出力をコピーする必要があります。

  4. すべてのコードをメソッドに入れて、onCreate()メソッドから呼び出す必要があります。

Android用PDFの生成の詳細については、[このリンク]を確認してください。 3