title: Creating PDF file using iText from a Recyclerview (Entire items inside) in Android?
body: <p>Hi i was trying to create a PDF output file from a Recyclerview using the library <a href="http://developers.itextpdf.com/examples-itext5" rel="nofollow noreferrer">iText</a> . After hours of struggle i was able to create PDF from recylerview .</p>
  
  <p>Following is are classes which i used to create PDF</p>
  
  <p><strong>Codes from Main Class</strong> </p>
  
  <pre><code>     private void getPrint() {
  
  
      ArrayList&lt;View&gt; viewArrayList = mAdapter.getPrintView(); // A function from Adapter class which returns ArrayList of VIEWS
      Document document = new Document(PageSize.A4);
      final File file = new File(getStorageDir("PDF"), "print.pdf");
      try {
          PdfWriter.getInstance(document, new FileOutputStream(file));
      } catch (DocumentException | FileNotFoundException e) {
          e.printStackTrace();
      }
  
      for (int im = 0; im &lt; viewArrayList.size(); im++) {
          // Iterate till the last of the array list and add each view individually to the document.
          try {
              viewArrayList.get(im).buildDrawingCache();         //Adding the content to the document
              Bitmap bmp = viewArrayList.get(im).getDrawingCache();
              ByteArrayOutputStream stream = new ByteArrayOutputStream();
              bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
              Image image = Image.getInstance(stream.toByteArray());
              image.scalePercent(70);
              image.setAlignment(Image.MIDDLE);
              if (!document.isOpen()) {
                  document.open();
              }
              document.add(image);
  
          } catch (Exception ex) {
              Log.e("TAG-ORDER PRINT ERROR", ex.getMessage());
          }
      }
  
      if (document.isOpen()) {
          document.close();
      }
  
      AlertDialog.Builder builder = new AlertDialog.Builder(Index.this);
      builder.setTitle("Success")
              .setMessage("PDF File Generated Successfully.")
              .setIcon(android.R.drawable.ic_dialog_alert)
              .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int whichButton) {
                      Intent intent = new Intent(Intent.ACTION_VIEW);
                      intent.setDataAndType(Uri.fromFile(file), "application/pdf");
                      intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                      startActivity(intent);
                  }
  
              }).show();
  
  }
  </code></pre>
  
  <p><strong>Common RecyclerView Adapter Class</strong></p>
  
  <pre><code>    public class RecyclerAdapter&lt;T, VM extends ViewDataBinding&gt; extends RecyclerView.Adapter&lt;RecyclerAdapter.RecyclerViewHolder&gt; {
  private final Context context;
  private ArrayList&lt;T&gt; items;
  private int layoutId;
  private RecyclerCallback&lt;VM, T&gt; bindingInterface;
  private static ArrayList&lt;View&gt; mPrintView = new ArrayList&lt;&gt;();
  
  public RecyclerAdapter(Context context, ArrayList&lt;T&gt; items, int layoutId, RecyclerCallback&lt;VM, T&gt; bindingInterface) {
      this.items = items;
      this.context = context;
      this.layoutId = layoutId;
      this.bindingInterface = bindingInterface;
  }
  
  public class RecyclerViewHolder extends RecyclerView.ViewHolder {
  
      VM binding;
  
      public RecyclerViewHolder(View view) {
          super(view);
          binding = DataBindingUtil.bind(view);
      }
  
      public void bindData(T model) {
          bindingInterface.bindData(binding, model);
          binding.executePendingBindings();
      }
  
  }
  
  @Override
  public RecyclerViewHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
      View v = LayoutInflater.from(parent.getContext())
              .inflate(layoutId, parent, false);
      return new RecyclerViewHolder(v);
  }
  
  @Override
  public void onBindViewHolder(RecyclerAdapter.RecyclerViewHolder holder, int position) {
      T item = items.get(position);
      Log.e("PRINT ", holder.binding.getRoot().getId() + "");
      mPrintView.add(holder.binding.getRoot());
      holder.bindData(item);
  
  }
  
  @Override
  public int getItemCount() {
      if (items == null) {
          return 0;
      }
      return items.size();
  }
  
  public static ArrayList&lt;View&gt; getPrintView() {
      return mPrintView;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I am using an Arraylist called <strong>mPrintView</strong> to save views inside RecylerView . Problem arises when a USER scroll the recylerview UP and DOWM multiple time the data inside ArrayList becomes duplicated </p>
  
  <p>Following are the images of results which i got after PDF convertion</p>
  
  <p><strong>Case 1 : When user scrolled only single time</strong></p>
  
  <p><a href="https://i.stack.imgur.com/KPRRs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KPRRs.png" alt="enter image description here"></a></p>
  
  <p><strong>Case 2 : When user Scroll UP and DOWN multiple time</strong></p>
  
  <p><a href="https://i.stack.imgur.com/bA5B4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bA5B4.png" alt="enter image description here"></a></p>
  
  <p>In the above image you can notice that the <strong>Apple</strong> has been duplicated </p>
  
  <p>Any help is appreciated </p>