如何打印listview中的所有项目

uemypmqf  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(294)

你好吗?哈哈,我有一些大的问题进行项目,我想把listview中的所有项目转换为pdf打印,但结果是如此可怕。。这是我的鲁尔斯特

我有超过4项..(实际上是25项)哦,你应该知道我使用的是printedpdfdocument而不是com.itextpdf.text.document
这是我的密码。
按打印机按钮,然后运行printpdf功能

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void printPDF()
{
    PrintManager pManager= (PrintManager) getSystemService(Context.PRINT_SERVICE);
    pManager.print("jonname", new ViewPrintAdapter(getApplicationContext(),listView),null);
}

视图打印适配器

public class ViewPrintAdapter extends PrintDocumentAdapter {
Context mContext;
ListView mView;

PrintedPdfDocument mDocument=null;

public ViewPrintAdapter(Context context, ListView view) {
    mContext=context;
    mView=view;

}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
    mDocument= new PrintedPdfDocument(mContext,newAttributes);
    if(cancellationSignal.isCanceled())
    {
        callback.onLayoutCancelled();
        return;
    }
    PrintDocumentInfo.Builder builder=new PrintDocumentInfo
            .Builder("print_output.pdf")
            .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
            .setPageCount(1);
    PrintDocumentInfo info=builder.build();
    callback.onLayoutFinished(info,true);

}

@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
    ListAdapter adapter=mView.getAdapter();
    int itemscount=adapter.getCount();
    int allitemsheight=0;
    List<Bitmap> bmps=new ArrayList<Bitmap>();
    for(int i=0;i<itemscount;i++)
    {
        View childView=adapter.getView(i,null,mView);
        childView.measure(
                View.MeasureSpec.makeMeasureSpec(mView.getWidth(),View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED));
        childView.layout(0,0,childView.getMeasuredWidth(),childView.getMeasuredHeight());
        childView.setDrawingCacheEnabled(true);
        childView.buildDrawingCache();
        bmps.add(childView.getDrawingCache());
        allitemsheight+=childView.getMeasuredHeight();
    }
    Bitmap bigbitmap=Bitmap.createBitmap(mView.getMeasuredWidth(),mView.getMeasuredHeight(),Bitmap.Config.ARGB_8888);
    Canvas canvas=new Canvas(bigbitmap);
    Paint paint=new Paint();
    int iHeight=0;
    for(int i=0;i<bmps.size();i++)
    {
        Bitmap bmp=bmps.get(i);
        canvas.drawBitmap(bmp,0,iHeight,paint);
        iHeight+=bmp.getHeight();
        bmp.recycle();
        bmp=null;
    }

    PdfDocument.Page page=mDocument.startPage(0);

    mView.draw(canvas);
    Rect src=new Rect(0, 0, mView.getWidth(), mView.getHeight());
    Canvas pageCanvas=page.getCanvas();
    Float pageWidth= Float.valueOf(pageCanvas.getWidth());
    Float pageHeight= Float.valueOf(pageCanvas.getHeight());
    Float scale=Math.min(pageWidth/src.width(),pageHeight/src.height());
    Float left=pageWidth/2-src.width()*scale/2;
    Float top=pageHeight/2-src.height()*scale/2;
    Float right=pageWidth/2+src.width()*scale/4;
    Float bottom=pageHeight/2+src.height()*scale/4;
    RectF dst=new RectF(0.0f,0.0f,right,bottom);
    pageCanvas.drawBitmap(bigbitmap,src,dst,null);
    mDocument.finishPage(page);
    try {// Write PDF document to file
        mDocument.writeTo(new FileOutputStream(destination.getFileDescriptor()));
    } catch (IOException e) {
        callback.onWriteFailed(e.toString());
        return;
    } finally {
        mDocument.close();
        mDocument = null;
    }
    callback.onWriteFinished(new PageRange[]{new PageRange(0, 0)});
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题