首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用itextg在android studio中创建多页pdf

使用itextg在android studio中创建多页pdf
EN

Stack Overflow用户
提问于 2018-04-15 18:35:22
回答 1查看 1.1K关注 0票数 0

你好,你好朋友……我来这里是因为我想从android listview创建多个页面pdf,我已经成功地实现了将所有的listview项目转换为pdf,即使它们不都是可见的。我面临的问题是,如果列表视图中的项目或捕获的列表视图的图像超过了pdf中的页面长度,它剪切的是其余的项目...所以我想要的是创建一个多页pdf,如果第一页是满的,然后将其余的图像部分写入第二个图像....i,希望我将门响应soon.........these是通过捕获所有列表视图项目来创建pdf的代码。

代码语言:javascript
复制
//method wich generate pdf of the attendance data
    private void save_as_pdf() {
        //First Check if the external storage is writable
        String state = Environment.getExternalStorageState();
        if (!Environment.MEDIA_MOUNTED.equals(state)) {
            // Toast.makeText(context,"")
        }

        //Create a directory for your PDF
        final File pdfDir = new File(Environment.getExternalStorageDirectory() + "/Documents", "attendance_report");
        if (!pdfDir.exists()) {

            pdfDir.mkdir();
        }

        //take  screen shoot of the entire listview of the attendance report
        ListView listview = studentlist;
        ListAdapter adapter = listview.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, listview);
            childView.measure(
                    View.MeasureSpec.makeMeasureSpec(listview.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(listview.getMeasuredWidth(), allitemsheight,
                Bitmap.Config.ARGB_8888);
        Canvas bigcanvas = new Canvas(bigbitmap);
        bigcanvas.drawColor(getResources().getColor(R.color.white));
        Paint paint = new Paint();
        int iHeight = 0;
        for (int i = 0; i < bmps.size(); i++) {
            Bitmap bmp = bmps.get(i);
            bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
            iHeight += bmp.getHeight();
            bmp.recycle();
            bmp = null;
        }

        //Now create the name of your PDF file that you will generate
        File pdfFile = new File(pdfDir, "report_for( " + course + "," + semister + section + "," + date + ").pdf");


        try {
            com.itextpdf.text.Document document = new com.itextpdf.text.Document();

            PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
            document.addTitle("Attendance Report Generated For course:" + course + "  Semester:" + semister + "  Section:" + section + "  Date:" + date);
            document.addHeader("Header", "Department Of Information Science");
            document.addCreator("Department Of Information Science");
            document.addCreationDate();
            document.bottomMargin();
            document.setPageCount(3);
            document.open();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bigbitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] byteArray = stream.toByteArray();
            addImage(document, byteArray);
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void addImage(com.itextpdf.text.Document document, byte[] byteArray) {
        Image image = null;
        try {
            image = Image.getInstance(byteArray);
        } catch (BadElementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // image.scaleAbsolute(150f, 150f);
        try {
            document.add(image);

        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2018-08-03 23:20:19

代码语言:javascript
复制
//take screen shot all records from listview
    for (int i = 0; i < itemscount; i++) {
        listview.setSelection(i);
        View childView = adapter.getView(i, null, listview);
        ViewGroup.LayoutParams lp = childView.getLayoutParams();
        if (lp == null) {
            lp = new ViewGroup.LayoutParams(-2,-2);
        }
        lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        childView.setLayoutParams(lp);
        childView.measure(View.MeasureSpec.makeMeasureSpec(listview.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();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49840938

复制
相关文章

相似问题

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