Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android create and print pdf from layout view

I am trying to create PDF file from xml layout view. I have a listview in that layout, adding items and setting height based on child. PDF is creating but not filling the whole page. What I have tried is,

 PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(2250, 1400, 1).create();

        // start a page
        PdfDocument.Page page = document.startPage(pageInfo);


        // draw something on the page
        LayoutInflater inflater = (LayoutInflater)
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View content = inflater.inflate(R.layout.pdf_layout, null);

        content.measure(2250, 1400);
        content.layout(0,0, 2250, 1400);

        tvName = (TextView)content.findViewById(R.id.tvName);
        tvDate = (TextView)content.findViewById(R.id.tvDate);
        tvAge = (TextView)content.findViewById(R.id.tvAge);
        tvGender = (TextView)content.findViewById(R.id.tvGender);
        tvPhone = (TextView)content.findViewById(R.id.tvPhone);
        lvList = (ListView)content.findViewById(R.id.lvList);
        lvList.setAdapter(adapter);
        Utils.setListViewHeight(lvList, CreatePDFDemo.this);

        tvName.setText(name);
        tvAge.setText(age + "Y");
        tvGender.setText(gender);
        tvPhone.setText(phone);

        content.draw(page.getCanvas());

        // finish the page
        document.finishPage(page);
        // add more pages
        // write the document content
        try {
            document.writeTo(output);
        } catch (IOException e) {
            e.printStackTrace();
        }

This its output is like this image,

enter image description here

How can I write layout view covering full width of pdf page?

like image 926
Exigente05 Avatar asked Apr 29 '17 19:04

Exigente05


People also ask

Can you create a PDF on an android?

Create a PDF on iOS and Android Android and iOS include similar options to create PDF files. In Android, open the Share menu, then use the Print option. Choose Save as PDF as your printer. In iOS, tap the Share button in an app, then tap the Options panel at the top.


1 Answers

Change to this,

        int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
        int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);

        content.measure(measureWidth, measuredHeight);
        content.layout(0, 0, page.getCanvas().getWidth(), page.getCanvas().getHeight());

This will get page full height and width.

like image 159
nuevo Avatar answered Oct 05 '22 04:10

nuevo