PdfDocument Dynamically Inflate Layout

245 views Asked by At

Iam trying to inflate a layout into a pdf with PdfDocument. I want to dynamically insert childs to the root which is the TableLayout. But the child overlays the root view in the created pdf. Any ideas?

Iam trying to inflate a layout into a pdf with PdfDocument. I want to dynamically insert childs to the root which is the TableLayout. But the child overlays the root view in the created pdf. Any ideas?

    PdfDocument document = new PdfDocument();

                // crate a page description
                PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(800, 1124, 1).create();

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

                // draw something on the page
                View content = inflater.inflate(R.layout.pdf_page, null);
                content.measure(800, 1124);
                content.layout(0, 0, 800, 1124);

                View child = inflater.inflate(R.layout.child_row,null);
                child.measure(800, 1124);
                child.layout(0, 0, 800, 1124);

                ((TableLayout)content.findViewById(R.id.pdf)).addView(child);
                content.draw(page.getCanvas());

                    // finish the page
                    document.finishPage(page);

                    document.writeTo(fos);

**Pdf.layout**

    <TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:stretchColumns="0,1,2"
    android:id="@+id/pdf">

    <TableRow
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/fullName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="serif"
            android:text="FullName"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/checkIn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="serif"
            android:text="CheckIn"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/checkOut"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="serif"
            android:text="CheckOut"
            android:textAlignment="center"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textStyle="bold" />

    </TableRow>



</TableLayout>

**Child layout**

<?xml version="1.0" encoding="utf-8"?>

<TableRow
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="FullName"
            android:textAlignment="center"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CheckIn"
            android:textAlignment="center"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="CheckOut"
            android:textAlignment="center"/>

    </TableRow>

I get this

enter image description here

And i want this

enter image description here

0

There are 0 answers