Graphics - Text to PDF

try {



android.graphics.pdf.PdfDocument document = new android.graphics.pdf.PdfDocument();


android.graphics.pdf.PdfDocument.PageInfo pageInfo = new android.graphics.pdf.PdfDocument.PageInfo.Builder(1080, 1920, 2).create();


android.graphics.pdf.PdfDocument.Page page1 = document.startPage(pageInfo);

View content1 = linear1;

content1.draw(page1.getCanvas());

document.finishPage(page1);


android.graphics.pdf.PdfDocument.Page page2 = document.startPage(pageInfo);

View content2 = linear2;

content2.draw(page2.getCanvas());

document.finishPage(page2);


java.io.File myFile = new java.io.File(getExternalCacheDir() + "/newpdf.pdf");

myFile.createNewFile();


java.io.FileOutputStream fOut = new java.io.FileOutputStream(myFile);

document.writeTo(fOut);

fOut.close();

document.close();


Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);

emailIntent.setType("*/*");

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new java.io.File(getExternalCacheDir() +"/newpdf.pdf")));

startActivity(Intent.createChooser(emailIntent, "Send text file"));


Toast.makeText(getBaseContext(), "File saved as .pdf", Toast.LENGTH_SHORT).show();

}

catch (Exception e) {

Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();

}


// if you want to save file in new folder use
/*
final File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Folder_name/");
if (!dir.exists()){
if(!dir.mkdirs()){
Log.e("ALERT","could not create the directories"); }}
String fname = edittext1.getText() +".pdf";
final File myFile = new File(dir, fname);
myFile.createNewFile();
*/

Komentar

Cara pembuatan Apps paling Populer

Create Stopwatch App in Android using Sketchware

How to enable download in webview in Sketchware apps?

TextInputLayout in Sketchware

Create Stopwatch App in Android using Sketchware

How to integrate Admob Ads in Sketchware project using AIDE?

How to find and​ highlight a word in a text field in Sketchware?

Code for implementing Notifications in Sketchware

A Flash Light App in Sketchware

Create a Stopwatch App using Chronometer in Sketchware

Create app to save all passwords, protected using voice key