2

활동의 "전체 페이지"로 PDF를 만들고 싶습니다. 보기에는 많은 항목이있는 RecyclerView가 있습니다.RecyclerView의 PDF를 전체 길이로 작성

내 Recyclerview의 전체 크기를 사용할 수 있지만 파일은 현재보기에서만 그리게됩니다.
enter image description here

내 Recyclerview의 모든 값을 사용하여 PDF 파일을 만들 수 : 이것이

public void tela(){ // create a new document 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) 

    { 
     PdfDocument document = new PdfDocument(); 

     getScreenshotFromRecyclerView(mRecyclerView); 
     content = mRecyclerView; 
     content.setBackgroundColor(Color.parseColor("#303030")); 


     PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(wil, 
       height, 1).create(); 

    // create a new page from the PageInfo 
     PdfDocument.Page page = document.startPage(pageInfo); 

    // repaint the user's text into the page 
     content.draw(page.getCanvas()); 

    // do final processing of the page 
     document.finishPage(page); 

    // saving pdf document to sdcard 
     SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy - HH-mm-ss",Locale.getDefault()); 
     String pdfName = "Revisões_" 
       + sdf.format(Calendar.getInstance().getTime()) + ".pdf"; 

    // all created files will be saved at path /sdcard/PDFDemo_AndroidSRC/ 
     File outputFile = new File(Environment.getExternalStorageDirectory().getPath(), pdfName); 

     try { 
      outputFile.createNewFile(); 
      OutputStream out = new FileOutputStream(outputFile); 
      document.writeTo(out); 
      document.close(); 
      out.close(); 
      Toast.makeText(this,"PDF gerado com sucesso",Toast.LENGTH_SHORT).show(); 
      Log.i("Gerou", "pdf"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

//이 한계

public void getScreenshotFromRecyclerView(RecyclerView view) { 
    RecyclerView.Adapter adapter = view.getAdapter(); 
    if (adapter != null) { 
     int size = adapter.getItemCount(); 

     for (int i = 0; i < size; i++) { 
      RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i)); 
      adapter.onBindViewHolder(holder, i); 
      holder.itemView.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY), 
        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 
      holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(), holder.itemView.getMeasuredHeight()); 
      height += holder.itemView.getMeasuredHeight(); 
     } 
     wil=view.getMeasuredWidth(); 


    } 
} 

결과를 점점이 내 코드?

감사합니다.

답변

0

PDF로 이미지 저장을 처리하기 위해 도우미 클래스를 구현했습니다.() 메소드 saveImageToPDF에서 나는 통과 :

  • 내 recyclerView
  • recyclerView 비트 맵을 얻기 위해 나는이 사용

컨텍스트 recyclerView

  • 의 비트 맵 위에있는 TabLayout, Take a screenshot of RecyclerView in FULL length

    public class PDFHelper { 
    
        private File mFolder; 
        private File mFile; 
        private Context mContext; 
    
        public PDFHelper(File folder, Context context) { 
    
         this.mContext = context; 
         this.mFolder = folder; 
    
         if(!mFolder.exists()) 
          mFolder.mkdirs(); 
        } 
    
        public void saveImageToPDF(View title, Bitmap bitmap, String filename) { 
    
         mFile = new File(mFolder, filename + ".pdf"); 
         if (!mFile.exists()) { 
          int height = title.getHeight() + bitmap.getHeight(); 
          PdfDocument document = new PdfDocument(); 
          PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(bitmap.getWidth(), height, 1).create(); 
          PdfDocument.Page page = document.startPage(pageInfo); 
          Canvas canvas = page.getCanvas(); 
          title.draw(canvas); 
    
          canvas.drawBitmap(bitmap, null, new Rect(0, title.getHeight(), bitmap.getWidth(),bitmap.getHeight()), null); 
    
          document.finishPage(page); 
    
          try { 
           mFile.createNewFile(); 
           OutputStream out = new FileOutputStream(mFile); 
           document.writeTo(out); 
           document.close(); 
           out.close(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
          } 
         } 
    
        } 
    } 
    
  • 0

    나는 그것을하는 더 간단한 방법을 발견했다. 내 코드가 깨끗한 지 또는 올바른 방법인지 확실하지 않습니다. 가변 크기의 리사이클 뷰가 있습니다. 나는 pdf로 리사이클 뷰의 전체 길이를 프린트하고 싶었다. 또한 참조 용으로 IText Pdf를 사용합니다.

    내가 한 것. 다음은 리사이클 러 뷰의 단일 항목에 대한 XML 파일입니다. 카드보기를 사용할 때 카드보기에 ID를 부여했습니다 (루트, LinearLayout 또는 RelativeLayout 등 원하는 항목을 사용할 수 있음). android : id = "@ + id/preview_order_list_card_root"

    <?xml version="1.0" encoding="utf-8"?> 
    <android.support.v7.widget.CardView 
        xmlns:android="http://schemas.android.com/apk/res/android" 
        android:id="@+id/preview_order_list_card_root" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="5dp" 
        android:layout_marginBottom="5dp"> 
    
        <LinearLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:orientation="vertical"> 
    
         <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:layout_margin="5dp"> 
    
          <LinearLayout 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:orientation="vertical"> 
    
           <ImageView 
            android:layout_width="50dp" 
            android:layout_height="50dp" 
            android:layout_gravity="center_horizontal" 
            android:src="@drawable/car"/> 
    
           <TextView 
            android:layout_width="1dp" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center" 
            android:background="@android:color/darker_gray" 
            android:text="12"/> 
    
           <TextView 
            android:id="@+id/preview_car_name" 
            android:layout_width="90dp" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center" 
            android:layout_marginTop="7dp" 
            android:gravity="center" 
            android:textSize="20sp" 
            android:textStyle="bold"/> 
    
           <TextView 
            android:id="@+id/preview_car_model" 
            android:layout_width="90dp" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center" 
            android:gravity="center"/> 
    
          </LinearLayout> 
    
          <TextView 
           android:layout_width="1dp" 
           android:layout_height="match_parent" 
           android:layout_marginLeft="5dp" 
           android:layout_marginRight="5dp" 
           android:background="@android:color/darker_gray"/> 
    
          <LinearLayout 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:orientation="vertical"> 
    
           <TextView 
            android:id="@+id/preview_part_name" 
            android:layout_width="100dp" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center" 
            android:layout_marginTop="7dp" 
            android:gravity="center" 
            android:textSize="20sp" 
            android:textStyle="bold"/> 
    
           <TextView 
            android:id="@+id/preview_part_model" 
            android:layout_width="100dp" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center" 
            android:layout_marginBottom="7dp" 
            android:gravity="center"/> 
    
           <TextView 
            android:layout_width="1dp" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center" 
            android:background="@android:color/darker_gray" 
            android:text="12"/> 
    
           <ImageView 
            android:layout_width="50dp" 
            android:layout_height="50dp" 
            android:layout_gravity="center_horizontal" 
            android:src="@drawable/car_part"/> 
    
          </LinearLayout> 
    
          <TextView 
           android:layout_width="1dp" 
           android:layout_height="match_parent" 
           android:layout_marginLeft="5dp" 
           android:background="@android:color/darker_gray"/> 
    
          <LinearLayout 
           android:layout_width="70dp" 
           android:layout_height="wrap_content" 
           android:layout_gravity="center" 
           android:layout_marginLeft="5dp" 
           android:orientation="vertical"> 
    
           <TextView 
            android:id="@+id/preview_quantity" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:textColor="@android:color/holo_green_light" 
            android:gravity="center" 
            android:textSize="25sp" 
            android:layout_gravity="center"/> 
    
           <TextView 
            android:id="@+id/preview_quantity_bucket" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_gravity="center" 
            android:layout_marginTop="15dp" 
            android:gravity="center"/> 
    
          </LinearLayout> 
    
         </LinearLayout> 
    
         <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:layout_gravity="bottom" 
          android:layout_margin="15dp" 
          android:orientation="vertical"> 
    
          <TextView 
           android:id="@+id/preview_extra_info" 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:layout_centerVertical="true" 
           android:gravity="center"/> 
    
          <LinearLayout 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:orientation="horizontal" 
           android:layout_marginTop="15dp"> 
    
           <TextView 
            android:id="@+id/preview_ordered_from" 
            android:layout_width="100dp" 
            android:layout_height="wrap_content" 
            android:textColor="@android:color/holo_red_light"/> 
    
           <TextView 
            android:id="@+id/preview_list_price" 
            android:layout_width="100dp" 
            android:layout_height="wrap_content" 
            android:gravity="center"/> 
    
           <TextView 
            android:id="@+id/preview_list_less" 
            android:layout_width="80dp" 
            android:layout_height="wrap_content" 
            android:gravity="right"/> 
    
          </LinearLayout> 
    
         </LinearLayout> 
    
        </LinearLayout> 
    
    </android.support.v7.widget.CardView> 
    

    이제 어댑터에서 onBindViewHolder를 호출 할 때 카드보기 객체를 저장할 getters와 setters를 사용하여 arraylist를 만듭니다.

    private static ArrayList<CardView> cardViewArrayList = new ArrayList<>(); 
    
    @Override 
        public void onBindViewHolder(final PreviewOrderAdapter.MyViewHolder holder, int position) 
        { 
         OrderItemDetailsModel orderItemDetailsModel = namesArrList.get(position); 
         holder.orderedFrom.setText(orderItemDetailsModel.getOrderedFrom()); 
         holder.listPrice.setText(orderItemDetailsModel.getListPrice()); 
         holder.listLess.setText(orderItemDetailsModel.getListLess()); 
    
         // ADDING THE CARD VIEW OBJECT IN THE ARRAYLIST 
         addCardView(holder.cardView); 
        } 
    
        @Override 
        public int getItemCount() 
        { 
         return namesArrList.size(); 
        } 
    
        private static void addCardView(CardView cardView) 
        { 
         cardViewArrayList.add(cardView); 
        } 
    
        public static ArrayList<CardView> getCardViewList() 
        { 
         return cardViewArrayList; 
        } 
    

    마지막으로 리사이클 러 뷰를 인쇄하는 동안이 작업이 수행됩니다. 우리가 문서에 별도로 각 뷰를 추가로 우리가 인해 재활용 뷰의 큰 크기로 메모리 누수에 대해 걱정할 필요 없다 이런 식으로

    //Getting the card view array list from the adapter above 
    ArrayList<CardView> cardViewArrayList = adapter.getCardViewList(); 
           for (int i = 0; i < cardViewArrayList.size(); i++) 
           { 
            // Iterate till the last of the array list and add each view individually to the document. 
            addContent(document, cardViewArrayList.get(i)); 
           } 
    
    //Adding the content to the document 
    private void addContent(Document document, View view) 
          throws DocumentException 
        { 
         try 
         { 
          view.buildDrawingCache(); 
    
          Bitmap bmp = view.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); 
          document.add(image); 
         } 
         catch (Exception ex) 
         { 
          Log.e("TAG-ORDER PRINT ERROR", ex.getMessage()); 
         } 
        } 
    

    .

    +0

    내가 어떻게 당신의 아이디어를 해결할 수 있었는지 그리고 성공적인 시도 였지만 문제가 있습니다. recyclerview에서 볼 수있는 셀만 저장된 PDF 파일에 표시됩니다. 이런 문제가 발생 했습니까? – Nikhil

    관련 문제