【问题标题】:Creating Pdf with data from editText android使用来自editText android的数据创建Pdf
【发布时间】:2018-11-23 10:31:24
【问题描述】:

我正在尝试创建一个pdf。数据直接取自编辑文本,但问题是,如果我在编辑文本中写入任何段落,pdf 中的最终输出将在 1 行而不是多行中显示所有数据。 虽然 pdf 正在创建,但我只能在 1 行中看到输出。 图片链接:

陈述活动:

文件已创建:

已查看文件:

public class Main2Activity extends AppCompatActivity {
    Button btnCreate;
    EditText editText,editText2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        btnCreate = (Button)findViewById(R.id.create);
        editText =(EditText) findViewById(R.id.edittext);
        editText2 =(EditText) findViewById(R.id.edittext2);
        btnCreate.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onClick(View view) {
                createPdf(editText.getText().toString(),editText2.getText().toString());
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void createPdf(String title,String description){
        // create a new document
        PdfDocument document = new PdfDocument();
        // crate a page description

        PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(300, 600, 1).create();
        // start a page
        PdfDocument.Page page = document.startPage(pageInfo);

        Canvas canvas = page.getCanvas();
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        canvas.drawText(title, 20, 40, paint);
        canvas.drawText(description, 20, 60, paint);
       // canvas.drawText(description,1,20,20.0f,30.0f,paint);
        //canvas.drawt
        // finish the page
        document.finishPage(page);
// draw text on the graphics object of the page

        // write the document content
        String directory_path = Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
        File file = new File(directory_path);
        if (!file.exists()) {
            file.mkdirs();
        }
        String targetPdf = directory_path+title+".pdf";
        File filePath = new File(targetPdf);
        try {
            document.writeTo(new FileOutputStream(filePath));
            Toast.makeText(this, "Done", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
            Log.e("main", "error "+e.toString());
            Toast.makeText(this, "Something wrong: " + e.toString(),  
Toast.LENGTH_LONG).show();
        }
        // close the document
        document.close();
    }
}

【问题讨论】:

  • 我认为您必须找到 EOF 字符并相应地更改要写入文件的字符串。(使用转义序列)
  • 对艾米其他瓦乌没有帮助
  • createPdf(editText.getText().toString()+"\n",editText2.getText().toString());
  • @Abr 没有工作
  • 试试xml里面的maxlines属性,也许

标签: java android android-layout


【解决方案1】:

我认为这是因为multilineEditText 的属性,而不是String,所以如果你从ET 中读取它,它总是在单行中。您可以尝试设置最大行长,然后读取字符限制,然后将其添加到新的String,例如ArrayList<String>(您不知道文本会有多长)。如果行限制不是以,.space 结尾,那么您将继续阅读直到出现,.space。毕竟,您将整个 ArrayList 添加到您的 pdf 中,每行都以 \n 结尾。

【讨论】:

    【解决方案2】:

    尝试用一些预定义的长度分割你从editText获得的字符串,然后在canvas.drawText()中使用它

    while(description.length()>15){
        if(description.length()>15){
            String first = description.substring(0,15);
            canvas.drawText(description, 20, 60, paint);
        }
        else{
            String second =description.substring(0,description.length());
            canvas.drawText(description, 20, 60, paint);
        }
    
    }
    

    这个我没试过。这可能有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-30
      • 2021-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 2011-11-04
      相关资源
      最近更新 更多