Pdf Creation Using Android Native API (Above Kitkat OS version)
This is the activity class
public class MainActivity extends AppCompatActivity { int docX, docY; // holds the size of an A4 page int viewX, viewY; // holds the size of your view float scaleX, scaleY; TextView one, two, three; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final RelativeLayout con = (RelativeLayout) findViewById(R.id.totlay); one = (TextView) findViewById(R.id.one); three = (TextView) findViewById(R.id.three); two = (TextView) findViewById(R.id.two); ViewTreeObserver vto = con.getViewTreeObserver(); vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // We will need to request the permission if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) { // Explain to the user why we need to read the storage Toast.makeText(MainActivity.this, "Signature will saved as image", Toast.LENGTH_SHORT).show(); } requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } else { File FilesFolder = new File(Environment.getExternalStorageDirectory() + File.separator, "PasuraContent"); if (!FilesFolder.exists()) { FilesFolder.mkdirs(); } File TheFile = new File(FilesFolder, "PdfCretion.pdf"); try { boolean b = TheFile.createNewFile(); if (b == true) { Toast.makeText(MainActivity.this, "file created" + TheFile.getAbsolutePath(), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "not created", Toast.LENGTH_SHORT).show(); } } catch (IOException e) { e.printStackTrace(); } try { PrintAttributes printAttributes = new PrintAttributes.Builder().setColorMode(PrintAttributes.COLOR_MODE_COLOR). setMediaSize(PrintAttributes.MediaSize.ISO_A4). setMinMargins(new PrintAttributes.Margins(10, 10, 10, 10)).build(); PrintedPdfDocument Doc = new PrintedPdfDocument(MainActivity.this, printAttributes); docY = Doc.getPageHeight(); docX = Doc.getPageWidth(); // use arrayList item for id terminology change your layout is now referred to as a View viewY = con.getHeight(); viewX = con.getWidth(); one.setText("hi this is sunil"); two.setText("i am here to check pdf creation"); three.setText("result?"); scaleX = (float) docX / viewX; // this will be lower or higher than 1.0 scaleY = (float) docY / viewY; // higher increasing the size lower decreasing PdfDocument.Page currentPage = Doc.startPage(0); // draw something on the page con being the view that you’ve created and // scale it before drawing it Canvas draw = currentPage.getCanvas(); draw.scale(scaleX, scaleY); con.draw(draw); // finish the page and assign it to the document to be later be transferred Doc.finishPage(currentPage); try { Doc.writeTo(new FileOutputStream(TheFile)); /* self explanitory really write whats been queued in Doc to the file we create whats worth saying is that a String value required for OutputFile not a File type */ } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //close the document Doc.close(); } catch (Exception e) { e.printStackTrace(); } } } } }); } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case 1: try { } catch (Exception e) { Log.v("Gestures", e.getMessage()); e.printStackTrace(); } break; } } }
And Here is xml layout
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/totlay" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="pmgnt.gwebitsol.com.pdfcreate.MainActivity"> <TextView android:id="@+id/one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:id="@+id/two" android:layout_below="@id/one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:id="@+id/three" android:layout_below="@id/two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> <TextView android:layout_below="@id/three" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </RelativeLayout>
Comments
Post a Comment