Я пытаюсь создать pdf
.Данные напрямую берутся из текста редактирования, но проблема в том, что если я напишу какой-либо абзац в тексте редактирования, то в окончании pdf
все данные отображаются в 1 строке вместо многострочных.Хотя pdf
создается, и я вижу результат только в одной строке.Ссылки на изображения:
Активность: ![enter image description here](https://i.stack.imgur.com/fUY4m.png)
Файл создан: ![enter image description here](https://i.stack.imgur.com/A4xuV.png)
Просмотрено файла: ![enter image description here](https://i.stack.imgur.com/4StuH.png)
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();
}
}