2017-02-10 18 views
10

Tôi đang cố tạo một tệp PDF bằng android.graphics.pdf. Vấn đề của tôi là với nhiều trang. Tôi có thể cung cấp cho android.graphics.pdf html mà sau đó có thể được in ra một tệp PDF. Bây giờ điều đó không hoạt động nếu văn bản làm tràn kích thước trang đã đặt. Có thể cho nó tất cả các html và nó sẽ tạo ra nhiều trang theo nội dung liên quan đến kích thước trang? Cũng như TCPDF :)Tạo nhiều trang PDF bằng android.graphics.pdf

Lưu ý. Tôi đang cố gắng tránh tạo nhiều trang riêng biệt bằng cách tính chiều cao của nội dung.

+0

hey cố gắng liên kết này có thể bạn sẽ nhận được câu trả lời của bạn http://stackoverflow.com/a/36349822/2888952 – Arpan24x7

+0

Có lỗi khi nội dung bị tràn không? – Michael

Trả lời

0

Đối với điều này, bạn sẽ cần phải thêm lọ iTextG để dự án của bạn:

public void createandDisplayPdf(String text) { 

    Document doc = new Document(); 

    try { 
     String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Dir"; 

     File dir = new File(path); 
     if(!dir.exists()) 
      dir.mkdirs(); 

     File file = new File(dir, "newFile.pdf"); 
     FileOutputStream fOut = new FileOutputStream(file); 

     PdfWriter.getInstance(doc, fOut); 

     //open the document 
     doc.open(); 

     Paragraph p1 = new Paragraph(text); 
     Font paraFont= new Font(Font.COURIER); 
     p1.setAlignment(Paragraph.ALIGN_CENTER); 
     p1.setFont(paraFont); 

     //add paragraph to document 
     doc.add(p1);  

    } catch (DocumentException de) { 
     Log.e("PDFCreator", "DocumentException:" + de); 
    } catch (IOException e) { 
     Log.e("PDFCreator", "ioException:" + e); 
    } 
    finally { 
     doc.close(); 
    } 

    viewPdf("newFile.pdf", "Dir"); 
} 

// Method for opening a pdf file 
private void viewPdf(String file, String directory) { 

    File pdfFile = new File(Environment.getExternalStorageDirectory() + "/" + directory + "/" + file); 
    Uri path = Uri.fromFile(pdfFile); 

    // Setting the intent for pdf reader 
    Intent pdfIntent = new Intent(Intent.ACTION_VIEW); 
    pdfIntent.setDataAndType(path, "application/pdf"); 
    pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    try { 
     startActivity(pdfIntent); 
    } catch (ActivityNotFoundException e) { 
     Toast.makeText(TableActivity.this, "Can't read pdf file", Toast.LENGTH_SHORT).show(); 
    } 
} 
Các vấn đề liên quan