Search code examples
javaandroidpdfandroid-recyclerviewitext

How I can get recyclerView list to itext PDF?


I want to add recyclerView list to itext PDF document, recyclerview is created from user input, I didn't Know how many item will user add, and each item of recyclerView contain many text.

How I can do it?


Solution

  • What ever item the user adds, it will be added to a list, and then the list populates the recyclerView. So, you can easily iterate the list, and for every element in the list add the details in the pdf. Here is an example given below, you can find the complete code here.

    private void createPdf(Order order) throws FileNotFoundException, DocumentException {
           
            File docsFolder = new File(Environment.getExternalStorageDirectory() + "/Documents");
            if (!docsFolder.exists()) {
                docsFolder.mkdir();
            }
            String pdfName = order.getOrderId() + "ST.pdf";
    
            // This is the list of product items, that populates the recyclerView
            List<ProductItem> items = order.getItemsList();
            
            File pdfFile = new File(docsFolder.getAbsolutePath(), pdfName);
            OutputStream output = new FileOutputStream(pdfFile);
            Document document = new Document(PageSize.A4);
    
            // We create a table to store different details of each product
            PdfPTable table = new PdfPTable(new float[]{3, 5, 3, 3});
            
            // This is some formatting done to make it look good
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setFixedHeight(28);
            table.setTotalWidth(PageSize.A4.getWidth());
            table.setWidthPercentage(100);
            table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE);
    
            // We add the headers for each column of the table
            table.addCell("Product Id");
            table.addCell("Name");
            table.addCell("Quantity");
            table.addCell("Price");
            table.setHeaderRows(1);
            PdfPCell[] cells = table.getRow(0).getCells();
            for (PdfPCell cell : cells) {
                cell.setBackgroundColor(BaseColor.LIGHT_GRAY);
            }
            
            // Here we iterate over the list, and for every product in the list we add a row in the table with details of the product.
            for (ProductItem item : items) {
                table.addCell(String.valueOf(item.getProductId()));
                table.addCell(String.valueOf(item.getProductName()));
                int quantity = item.getProductQuantity();
                table.addCell(String.valueOf(quantity));
                table.addCell(String.valueOf(item.getProductPrice() * quantity));
            }
    
            // Now we add the table to the document
            PdfWriter.getInstance(document, output);
            document.open();
            document.add(table);
            document.close();
        }