首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我使用itextg从ListView创建pdf时,只会出现相同的listview子

当我使用itextg从ListView创建pdf时,只会出现相同的listview子
EN

Stack Overflow用户
提问于 2016-11-05 06:28:23
回答 1查看 148关注 0票数 1

我正在尝试创建一个pdf从列表查看项目。这是我的列表:

其结果是:

下面是我的代码:

代码语言:javascript
复制
ListView def = (ListView) findViewById(R.id.ist);
ListAdapter adapter = def.getAdapter();
int itemscount = adapter.getCount();
/*int itemsposition = adapter.getItem(position);*/
Toast.makeText(getApplicationContext(), itemscount + " temaxia", Toast.LENGTH_LONG).show();
int allitemsheight = 0;
List<Bitmap> bmps = new ArrayList<Bitmap>();

for (int i = 0; i < itemscount ; i++) {
    View childView = adapter.getView(i, null, def);
    /*View childView = def.getChildAt(1);*/
    childView.measure(View.MeasureSpec.makeMeasureSpec(def.getWidth(),
        View.MeasureSpec.EXACTLY),
    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    childView.layout(0, 0, childView.getMeasuredWidth(),
        childView.getMeasuredHeight());
    childView.setDrawingCacheEnabled(true);
    childView.buildDrawingCache();
    childView.getDrawingCache();
    /*bmps.add(childView.getDrawingCache());
    allitemsheight+=childView.getMeasuredHeight();*/
    Bitmap bigbitmap = Bitmap.createBitmap(def.getMeasuredWidth(),
        childView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas bigcanvas = new Canvas(bigbitmap);
    def.draw(bigcanvas);
    Paint paint = new Paint();
    bigcanvas.drawBitmap(bigbitmap,0,childView.getMeasuredHeight(),paint);
    bigbitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    Image myImg = Image.getInstance(stream.toByteArray());
    myImg.scalePercent(45, 60);
    myImg.setAlignment(Image.ALIGN_CENTER);

    // add image to document
    doc.add(myImg);
    doc.add( new Paragraph());
}

我只是找不出为什么它只能在第一位置获得子视图,尽管它在for循环中。有谁可以帮我?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-07 09:10:20

我将所有附件改为:从所有子视图创建一个大图像,然后将其剪切到多个页面A4 size.See below....NOTICE,即第一个文档从未打开,也从未关闭,但在创建图像文档后被正确地打开和关闭.

代码语言:javascript
复制
                     File file = new File(dir, flname + ".pdf");                         FileOutputStream fOut = new FileOutputStream(file);                         FileOutputStream fOut2 = new FileOutputStream(file);
代码语言:javascript
复制
                    pdfWriter.getInstance(doc, fOut);

                    // open the document
                   /* doc.open();*/
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();

                    //////////////////////

                    ListView def = (ListView) findViewById(R.id.ist);


                    ListAdapter adapter = def.getAdapter();
                    int itemscount = adapter.getCount();
                    /*int itemsposition = adapter.getItem(position);*/
                    Toast.makeText(getApplicationContext(), itemscount + " temaxia", Toast.LENGTH_LONG).show();
                    View childView =null;
                    int allitemsheight = 0;

                    List<Bitmap> bmps = new ArrayList<Bitmap>();
                    for (int i = 0; i < itemscount ; i++) {
                        childView = adapter.getView(i,null,def);
                        /*childView = def.getChildAt(i);*/
                        childView.measure(View.MeasureSpec.makeMeasureSpec(def.getWidth(),
                                View.MeasureSpec.EXACTLY),
                                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                        childView.layout(0, 0, childView.getMeasuredWidth(),
                                childView.getMeasuredHeight());
                        childView.setDrawingCacheEnabled(true);
                        childView.buildDrawingCache();
                        /*childView.getDrawingCache();*/
                        bmps.add(childView.getDrawingCache());

                        allitemsheight+=childView.getMeasuredHeight();

                    }
                    Bitmap bigbitmap = Bitmap.createBitmap(def.getMeasuredWidth(),
                            allitemsheight , Bitmap.Config.ARGB_8888);
                    Paint paint = new Paint();
                    Canvas bigcanvas = new Canvas(bigbitmap);

                    for (int i = 0; i < bmps.size(); i++) {
                        Bitmap bmp = bmps.get(i);
                        bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
                        /*bigcanvas.drawColor(Color.WHITE);
                        def.draw(bigcanvas);*/

                        iHeight+=bmp.getHeight();
                        bmp.recycle();
                        bmp=null;


                    }
                    bigbitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                    Image myImg = Image.getInstance(stream.toByteArray());
                    myImg.scalePercent(45, 60);
                    myImg.setAlignment(Image.ALIGN_CENTER);



                    /*if(myImg.getWidth() >= doc.getPageSize().getWidth() || myImg.getHeight() >= doc.getPageSize().getHeight()){
                        myImg.scaleToFit(doc.getPageSize());
                        doc.newPage();
                    }
                    myImg.setAbsolutePosition((doc.getPageSize().getWidth() - myImg.getScaledWidth()) / BaseField.BORDER_WIDTH_MEDIUM, (doc.getPageSize().getHeight() - myImg.getScaledHeight()) / BaseField.BORDER_WIDTH_MEDIUM);

                    doc.add(myImg);
                    doc.add( new Paragraph());*/


                    ///////////////////////////////////////
                    /////////////////////////////////////////
                    /////////////////////////////////////
                    Document document = new Document();
                    PdfWriter pdfWriter2 = PdfWriter.getInstance(document, fOut2);

                    document.open();

                    PdfContentByte content = pdfWriter2.getDirectContent();
                    myImg.scaleAbsolute(PageSize.A4);
                    myImg.setAbsolutePosition(0, 0);

                    float width = PageSize.A4.getWidth();
                    float heightRatio = myImg.getHeight() * width / myImg.getWidth();
                    int nPages = (int) (heightRatio / PageSize.A4.getHeight());
                    float difference = heightRatio % PageSize.A4.getHeight();

                    while (nPages >= 0) {
                        document.newPage();
                        content.addImage(myImg, width, 0, 0, heightRatio, 0, -((--nPages * PageSize.A4.getHeight()) + difference));
                    }
                    document.close();



                } catch (DocumentException de) {
                    Log.e("PDFCreator", "DocumentException:" + de);
                } catch (IOException e) {
                    Log.e("PDFCreator", "ioException:" + e);
                } finally {
                    /*doc.close();*/
                }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40435433

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档