【问题标题】:Create 2nd Page in a PDF Document在 PDF 文档中创建第二页
【发布时间】:2015-11-10 15:16:10
【问题描述】:

我写了一个安卓应用来做检查。它收集数据,然后对其进行格式化并将其放置在 PDF 文档中。在保存并通过电子邮件发送之前,我在创建 PDF 的第二页时遇到问题。我已注释掉“PDF 第 2 页”。直到声明 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();
    }
}

【问题讨论】:

  • “在保存 PDF 并通过电子邮件发送之前,我在创建 PDF 的第二页时遇到问题”——准确地说,问题出在哪里? “直到声明 pdfName 的这段代码才是问题所在”——准确地说,问题出在哪里?另外,请不要对路径进行硬编码(特别是,我希望您的路径在许多设备上都是错误的)。在ContextEnvironment 上使用适当的方法来获取根目录以供您在构建路径时使用。
  • 感谢您对路径的建议。我这样做只是为了确保轻松访问创建的文件。确切的问题是当我创建第二页时,应用程序停止了。 LogCat 并没有真正告诉我任何事情,因为它会引发一堆错误。关于为什么代码的注释部分不起作用的任何想法???

标签: java android pdf android-studio


【解决方案1】:
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);
}

【讨论】:

  • 如果当前页面充满了文本,有什么想法可以自动创建下一页吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-08
相关资源
最近更新 更多