All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Convert Layout To PDF In Android Github

Last Updated : Jul 16, 2023

How To Convert Layout To PDF In Android Github

In this article we will show you the solution of how to convert layout to pdf in android github, the Action bar,any edit or delete buttons, and other elements that are not required inside a PDF version for your view are removed from the copy of a layout that is identical to the one you wish to convert to PDF.

For instance, it is sensible to include a button for delete or increase the number of items inside the listview if your view includes one.

Putting all of your display data inside a class instance is one way to do this.

As an illustration, suppose your app is a registration app for events and you have a component or activity for customer registration.

After the user enters their username, email address, and other information into the form, you can use the Customer.java class to build a customer object using that information.

You will be expanding a layout that is optimized for data input in the onCreate() and onCreateView method on the Activity or Fragment, and if the user clicks on produce PDF, you can either start new Fragment Transaction that will expand the print optimized layout or begin a fresh Activity.

Step By Step Guide On How To Convert Layout To Pdf In Android Github :-

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfWriter;
String dirpath;
public void layoutToImage(View view) {
    // get view group using reference
    relativeLayout = (RelativeLayout) view.findViewById(R.id.print);
    // convert view group to bitmap
    relativeLayout.setDrawingCacheEnabled(true);
    relativeLayout.buildDrawingCache();
    Bitmap bm = relativeLayout.getDrawingCache();
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
    try {
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public void imageToPDF() throws FileNotFoundException {
    try {
        Document document = new Document();
        dirpath = android.os.Environment.getExternalStorageDirectory().toString();
        PdfWriter.getInstance(document, new FileOutputStream(dirpath + "/NewPDF.pdf")); // Change pdf's name.
        document.open();
        Image img = Image.getInstance(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
        float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
                - document.rightMargin() - 0) / img.getWidth()) * 100;
        img.scalePercent(scaler);
        img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_TOP);
        document.add(img);
        document.close();
        Toast.makeText(this, "PDF Generated successfully!..", Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
    }
}
  1. Prepare your data for PDF. Creating a data class which can store the dynamic data that will be on the PDF is the first step.
  2. In our example, each student report card is displayed in the PDF. Hence, the student name,subject marks,and overall marks must be included in the data class. Two data classes will be used: one for all the details and one for the topic marks.
  3. Create your PDF layout in XML. The following step is to build the required XML files, which specify how our PDF should be laid out. For our PDF, you will need 2 layout files: one for the overall layout of the document and the other for the layout of the item rows in our recycler view.
  4. Create a bitmap image from the layout. We must produce a bitmap image which holds its layout view in order to utilize the Xml document as that of the layout of our PDF.
  5. Add the bitmap into your PDF document. We can now insert this bitmap object into our PDF file as seen below:
  6. Make a PDFDocument object & set the page info to 595 × 842 PostScript points, or 1/72 of an inch, for the A4 size portrait.

Conclusion :-

Several apps provide users the option to download a PDF file that contains the app's data.

Hence, in this scenario, we must appropriately represent the data inside our app as a PDF file from it.

So, using this method, we can quickly make a new PDF that meets our needs.

In this post, we'll look at how to create a new Pdf from the information in an Android app and to save it to the user's device's external storage.

I hope this article on how to convert layout to pdf in android github helps you and the steps and method mentioned above are easy to follow and implement.