首页 > 解决方案 > PdfDocument - Cut page at the end of the layout

问题描述

I'm creating a Pdf file using the Android PdfDocument class. I'm inflating the layout from an XML file (a LinearLayout inside a ConstraintLayout), which is then completed at run-time by adding new rows with external data.

The problem is that the output pdf can be very long or short. I would like to avoid specifying the page height before inflating the layout. However, I can't find a solution to measure the height of the layout before adding it to the page

This is the actual code:

PdfDocument document = new PdfDocument();

//set page width and height
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(227,992, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);

//inflating the layout
View v = getLayoutInflater().inflate(R.layout.rapportino, null);

//add data to layout
//...

//measure the layout, draw it and finish page
int measureWidth = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(page.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);
v.measure(measureWidth, measuredHeight);
v.layout(0, 0, measureWidth, measuredHeight);
v.draw(page.getCanvas());
document.finishPage(page);

//write to file..

Is there a way to create the pdf depending on the height of the view?

EDIT: this is the layout i'm inflating

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/rapportino_indirizzo_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:gravity="end"
            android:textColor="#000000"
            android:textSize="@dimen/rapportino_global_textsize" />

        <!-- 
            other textview, linearlayout and imageview 
            ..
            ..  
        -->

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidpdf-generationandroid-linearlayout

解决方案


So, I solved with a quite strange solution. Since adding a layout to a view causes the height to be calculated, I created an invisible view in the activity where I'm creating the pdf (with the width of the pdf):

<!-- add this to your activity -->
<LinearLayout
    android:visibility="invisible"
    android:id="@+id/output_pdf_view"
    android:orientation="vertical"
    android:layout_width="226dp"
    android:layout_height="wrap_content" />

Then I added the inflated layout to the invisible view (after having filled it with external data). With an observer you can get the height at the time it is calculated.

//inflating the layout
View v = getLayoutInflater().inflate(R.layout.rapportino, null);

//add data to layout (rows, images etc.)
//...

//find the invisible view
LinearLayout invisibleOutputPdfView = findViewById(R.id.output_pdf_view);

//add the pdf layout and observe changes
invisibleOutputPdfView.addView(v);
invisibleOutputPdfView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            //remove the listener
            invisibleOutputPdfView.getViewTreeObserver().removeOnGlobalLayoutListener(this);

            int height = invisibleOutputPdfView.getHeight();
            createPDF(height, ..);
        }
    });

NOTE: for some reason it seems that there is a 1:1 ratio between dp and pt in the generation of the pdf. So no conversions are needed.


推荐阅读