PdfDocument Inflatting Layout

468 views Asked by At

Iam trying to inflate a layout into a pdf with PdfDocument. In the design view everything looks ok, but when the pdf is created, spaces arent preserved. As you can see in pictures i want the spaces exactly like the first one.Any ideas?

Iam trying to inflate a layout into a pdf with PdfDocument. In the design view everything looks ok, but when the pdf is created, spaces arent preserved. As you can see in pictures i want the spaces exactly like the first one.Any ideas?

                // create a new document
                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 = MainActivity.this.getLayoutInflater().inflate(R.layout.pdf_page, null);
                content.measure(800, 1124);
                content.layout(0, 0, 800, 1124);
                content.draw(page.getCanvas());

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

                document.writeTo(fos);

Pdf.layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pdf">


    <TableRow
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="FullName"
            android:textAlignment="center"
            android:textSize="14sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/checkIn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="text"
            android:textAlignment="center"
            android:textSize="14sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/checkOut"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="text"
            android:textAlignment="center"
            android:textSize="14sp"
            android:textStyle="bold" />

    </TableRow>



</android.support.constraint.ConstraintLayout>

I want this

enter image description here

but i get this

enter image description here

1

There are 1 answers

2
Mehedi Hasan Chonchol On BEST ANSWER

You can try taking TableLayout rather than CoordinatorLayout

Study android:stretchColumns="1" attribute of TableLayout. Wish your problem will be solved. Have a look on following code:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/simpleTableLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:stretchColumns="1"> <!-- stretch the second column of the layout-->

        <!-- first row of the table layout-->
        <TableRow

            android:id="@+id/firstRow"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <!-- first element of the row-->
            <TextView

                android:id="@+id/simpleTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#b0b0b0"
                android:padding="18dip"
                android:text="Text 1"
                android:textColor="#000"
                android:textSize="12dp" />

            <TextView

                android:id="@+id/simpleTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#FF0000"
                android:padding="18dip"
                android:text="Text 2"
                android:textColor="#000"
                android:textSize="14dp" />

        </TableRow>
    </TableLayout>