在PDF文档中创建第二页

时间:2015-11-10 15:16:11

标签: java android pdf android-studio

我已经编写了一个Android应用程序来进行检查。它收集数据然后对其进行格式化并将其放在PDF文档中。在保存并发送电子邮件之前,我在创建PDF的第二页时遇到问题。我已经评论过"第2页PDF和#34;。这段代码直到pdfName的声明是问题所在。我不想使用像iText或Apose这样的东西。谁能帮忙???

public void createPDF(){

// Create a object of PdfDocument
    PdfDocument document = new PdfDocument();

// content view is TableLayout of data
    View content = findViewById(id.final_table_layout_for_pdf_page_1);

// create a page info with attributes as below
// page number, height and width
// i have used height and width to that of pdf content view
    int pageNumber = 1;
    PdfDocument.PageInfo pageInfo = new     PdfDocument.PageInfo.Builder(content.getWidth(),
            content.getHeight() - 20, pageNumber).create();

// create a new page from the PageInfo
    PdfDocument.Page page = document.startPage(pageInfo);

// repaint the user's text into the page
    content.draw(page.getCanvas());

    // do final processing of the page
    document.finishPage(page);

/* PAGE 2 OF PDF

    content = findViewById(id.final_table_layout_for_pdf_page_2);

// create a page info with attributes as below
// for 2nd page
// i have used height and width to that of pdf content view
    pageNumber = 2;
    pageInfo = new PdfDocument.PageInfo.Builder(content.getWidth(),
            content.getHeight() - 20, pageNumber).create();

// create a new page from the PageInfo
    page = document.startPage(pageInfo);

// repaint the user's text into the page
    content.draw(page.getCanvas());

// do final processing of the page
    document.finishPage(page);*/

// saving pdf document to root dir
    String pdfName = "pdf_inspection_demo.pdf";

// all created files will be saved at path /sdcard/PDFDemo_AndroidSRC/
    File outputFile = new File("/storage/emulated/0/", pdfName);

    try {
        outputFile.createNewFile();
        OutputStream out = new FileOutputStream(outputFile);
        document.writeTo(out);
        document.close();
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
        errorString = e.getMessage();
    }
}

1 个答案:

答案 0 :(得分:0)

private void generatePDF() {
   PdfDocument pdfDocument = new PdfDocument();

    criNewPag(1,"ان شاء الله ",pdfDocument);
    criNewPag(2,"ان شاء الله ربي  ",pdfDocument);

    File file = new File(Environment.getExternalStorageDirectory(), "GFG.pdf");
    try {
        pdfDocument.writeTo(new FileOutputStream(file));
        Toast.makeText(getApplicationContext(), "PDF file generated successfully.", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        e.printStackTrace();
    }
    pdfDocument.close();


}
private void criNewPag(int numpag,String text, PdfDocument pdfDocument ){
    Paint paint = new Paint();
    Paint title = new Paint();
    PdfDocument.PageInfo mypageInfo = new PdfDocument.PageInfo.Builder(pagewidth, pageHeight, numpag).create();
    PdfDocument.Page   myPage = pdfDocument.startPage(mypageInfo);
    Canvas canvas = myPage.getCanvas();
    bmp = BitmapFactory.decodeResource(getResources(), R.drawable.gfgimage);
    scaledbmp = Bitmap.createScaledBitmap(bmp, 140, 140, false);
    canvas.drawBitmap(scaledbmp, 56, 40, paint);
    title.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL));
    title.setTextSize(15);
    title.setColor(ContextCompat.getColor(this, R.color.purple_200));
    canvas.drawText("A portal for IT professionals.", 209, 100, title);
    canvas.drawText("Geeks for Geeks", 209, 80, title);
    title.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
    title.setColor(ContextCompat.getColor(this, R.color.purple_200));
    title.setTextSize(15);
    title.setTextAlign(Paint.Align.CENTER);
    canvas.drawText(text, 150, 240, title);

    pdfDocument.finishPage(myPage);
}