Check Out Mobile Application Proposal

Create Pdf in android studio, XML layout to pdf in android studio, open pdf in android studio.



    I'm using View binding here so I use binding class to bind the xml layout.

    GITHUB

    Lets see the XML code first...

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<!-- we are going to pdf this linear layout -->
<LinearLayout
android:id="@+id/pdfLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="@color/white"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="User Name" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="User Name" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Gender" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Male" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Age" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="25" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Height" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="173 cm" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Weight" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="70" />
</LinearLayout>
</LinearLayout>


<!-- lets create a button here to save pdf-->
<Button
android:id="@+id/pdfButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pdf"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/openPDf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Let's show you the click action and binding properties code below.

// using view binding so i'm implemnt ing the layout like this.
// see my last mvvm video to implement the viewbinding.
private XmlPdfBinding xmlPdfBinding;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
xmlPdfBinding = XmlPdfBinding.inflate(getLayoutInflater());
setContentView(xmlPdfBinding.getRoot());
/*To use the xml view in view binding need to define the id in xml */
xmlPdfBinding.pdfButton.setOnClickListener(view -> {
/*Lets start coding here how to convert the xml layout to pdf.
* for the first we need to load the layout to Bitmap..
* lets do that --> convert the pdfLayout to bitmap...
* */
Bitmap bitmap = viewToBitmap(xmlPdfBinding.pdfLayout,xmlPdfBinding.pdfLayout.getWidth(),xmlPdfBinding.pdfLayout.getHeight());
createPdf(bitmap);
});

xmlPdfBinding.openPDf.setOnClickListener(view -> {
OpenCreatedFile();
});
}


Then we going to create a method to convert the view to bitmap .

/* This method is use to convert the view to bitmap
* In bitmap we need to provide HEIGHT, WIDTH, COLOR ALOGRITHM
* params @v = View, @Width = width of the pdf view @height = height of the pdf view
* */
public static Bitmap viewToBitmap(View v,int width, int height){
Bitmap viewMap = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(viewMap);
v.draw(canvas);
/*process is done.. returing the bitmap*/
return viewMap;
}

/*Lets start creating the pdf using this function
* @params @bitmap= Bitmap
* */
public void createPdf(Bitmap bitmap){
/*First get the height and width of the pdf
* */
DisplayMetrics displayMetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
float heigh = displayMetrics.heightPixels;
float width = displayMetrics.widthPixels;

int mheight = (int) heigh, mwidth = (int) width;

/* Started createing a pdf from here....
* */
PdfDocument document = new PdfDocument();
/* You can specify the page count here and height and width of the page count
* this is the layout height and width
* if you need A4 size .. USE 594 and 842
* */
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(mheight,mwidth,1).create();
PdfDocument.Page page = document.startPage(pageInfo);

Canvas canvas = page.getCanvas();
Paint paint = new Paint();
canvas.drawPaint(paint);

// A4 size .. USE 594 and 842
bitmap = Bitmap.createScaledBitmap(bitmap,mwidth,mheight,true);

paint.setColor(Color.BLUE);

canvas.drawBitmap(bitmap, 0,0,null);
document.finishPage(page);

try {

/*Need to specify the pathb ...
* Lets get that from the specific method ... */
document.writeTo(getFileName());
}catch (Exception e){
Log.d("TAG", "createPdf: " + e.getMessage());
}
}

/* To get the file path to store the pdf file
* */
public FileOutputStream getFileName(){
File pdfDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+ "/PDF");
if (!pdfDir.exists()) pdfDir.mkdir();
File myPath = new File(pdfDir, "cmlPdf" + ".pdf");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(myPath);
}catch (Exception ee){
Log.d("TAG", "getFileName: " + ee.getMessage());
}
return fos;
}

public void OpenCreatedFile(){
File pdfDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+ "/PDF");
if (!pdfDir.exists()) pdfDir.mkdir();
File myPath = new File(pdfDir, "cmlPdf" + ".pdf");
if (myPath.exists()){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_TEXT, "OpenPDF");
intent.setType("application.pdf");

Intent shareIntent = Intent.createChooser(intent,null);
startActivity(shareIntent);

}
}


THANK YOU








Comments