PDFDocument - несколько страниц - PullRequest
1 голос
/ 14 апреля 2020

Я использую класс PDFDocument для преобразования RelativeLayout, который заполняется во время выполнения, в PDF. Я знаю, как сделать это для 1 страницы PDF, однако, если во время выполнения у RelativeLayout нет страниц> 1, то я не знаю, как это сделать. Я думаю, что я должен разделить текст - но я не знаю, как мне это сделать.

Вот это XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".Summary"
    android:id="@+id/mainwrapper"
    android:padding="@dimen/allpadding">

    <TextView
        android:id="@+id/heading"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/summary"
        android:textStyle="bold"
        android:textSize="@dimen/appbar_text_size"
        app:fontFamily="sans-serif-condensed"
        android:paddingStart="@dimen/allaroundmargin"
        android:paddingEnd="0dp">
    </TextView>

    <Button
        android:id="@+id/btnToPDF"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View as PDF"
        android:layout_alignParentEnd="true">
    </Button>

    <TextView
        android:id="@+id/datetext"
        android:text="date"
        android:layout_marginTop="@dimen/allaroundmargin"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/heading"
        android:textStyle="italic"
        android:paddingStart="@dimen/allaroundmargin"
        android:paddingEnd="0dp"
        app:fontFamily="sans-serif-condensed">
    </TextView>

    <ScrollView
        android:id="@+id/allQuizzesSV"
        android:layout_below="@id/datetext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bgsummary"
        android:layout_marginTop="@dimen/allaroundmargin">

        <RelativeLayout
            android:id="@+id/wrapperAllQuiz"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:id="@+id/quiz1LL"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/btn_quiz1"
                android:paddingLeft="@dimen/allaroundmargin"
                android:paddingStart="@dimen/allaroundmargin"
                android:paddingEnd="@dimen/allaroundmargin"
                android:paddingRight="@dimen/allaroundmargin">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_quiz1"
                    android:textColor="@color/appbarTcolor"
                    android:textStyle="bold"
                    android:textSize="@dimen/appbar_text_size"
                    app:fontFamily="sans-serif-condensed">
                </TextView>

                <RelativeLayout
                    android:id="@+id/correctanswersQuiz1"
                    android:layout_marginTop="@dimen/allaroundmargin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/tvCorrectQuestionsQuiz1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/correctquestions"
                        android:textColor="@color/appbarTcolor"
                        app:fontFamily="sans-serif-condensed">
                    </TextView>

                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/wronganswersQuiz1"
                    android:layout_marginTop="@dimen/allaroundmargin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:fontFamily="sans-serif-condensed">

                    <TextView
                        android:id="@+id/tvWrongQuestionsQuiz1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/wrongquestions"
                        android:layout_marginTop="@dimen/allaroundmargin"
                        android:textColor="@color/appbarTcolor"
                        app:fontFamily="sans-serif-condensed">
                    </TextView>

                </RelativeLayout>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/quiz2LL"
                android:layout_below="@id/quiz1LL"
                android:layout_marginTop="@dimen/margin_small_between_btns"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/btn_quiz2"
                android:paddingLeft="@dimen/allaroundmargin"
                android:paddingStart="@dimen/allaroundmargin"
                android:paddingEnd="@dimen/allaroundmargin"
                android:paddingRight="@dimen/allaroundmargin">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_quiz2"
                    android:textColor="@color/appbarTcolor"
                    android:textStyle="bold"
                    android:textSize="@dimen/appbar_text_size"
                    app:fontFamily="sans-serif-condensed">
                </TextView>

                <RelativeLayout
                    android:id="@+id/correctanswersQuiz2"
                    android:layout_marginTop="@dimen/allaroundmargin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/tvCorrectQuestionsQuiz2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/correctquestions"
                        android:textColor="@color/appbarTcolor"
                        app:fontFamily="sans-serif-condensed">
                    </TextView>

                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/wronganswersQuiz2"
                    android:layout_marginTop="@dimen/allaroundmargin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:fontFamily="sans-serif-condensed">

                    <TextView
                        android:id="@+id/tvWrongQuestionsQuiz2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/wrongquestions"
                        android:layout_marginTop="@dimen/allaroundmargin"
                        android:textColor="@color/appbarTcolor"
                        app:fontFamily="sans-serif-condensed">
                    </TextView>

                </RelativeLayout>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/quiz3LL"
                android:layout_below="@id/quiz2LL"
                android:layout_marginTop="@dimen/margin_small_between_btns"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/btn_quiz3"
                android:paddingLeft="@dimen/allaroundmargin"
                android:paddingStart="@dimen/allaroundmargin"
                android:paddingEnd="@dimen/allaroundmargin"
                android:paddingRight="@dimen/allaroundmargin">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_quiz3"
                    android:textColor="@color/appbarTcolor"
                    android:textStyle="bold"
                    android:textSize="@dimen/appbar_text_size"
                    app:fontFamily="sans-serif-condensed">
                </TextView>

                <RelativeLayout
                    android:id="@+id/correctanswersQuiz3"
                    android:layout_marginTop="@dimen/allaroundmargin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/tvCorrectQuestionsQuiz3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/correctquestions"
                        android:textColor="@color/appbarTcolor"
                        app:fontFamily="sans-serif-condensed">
                    </TextView>

                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/wronganswersQuiz3"
                    android:layout_marginTop="@dimen/allaroundmargin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:fontFamily="sans-serif-condensed">

                    <TextView
                        android:id="@+id/tvWrongQuestionsQuiz3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/wrongquestions"
                        android:layout_marginTop="@dimen/allaroundmargin"
                        android:textColor="@color/appbarTcolor"
                        app:fontFamily="sans-serif-condensed">
                    </TextView>

                </RelativeLayout>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/quiz4LL"
                android:layout_below="@id/quiz3LL"
                android:layout_marginTop="@dimen/margin_small_between_btns"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/btn_quiz4"
                android:paddingLeft="@dimen/allaroundmargin"
                android:paddingStart="@dimen/allaroundmargin"
                android:paddingEnd="@dimen/allaroundmargin"
                android:paddingRight="@dimen/allaroundmargin">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_quiz4"
                    android:textColor="@color/appbarTcolor"
                    android:textStyle="bold"
                    android:textSize="@dimen/appbar_text_size"
                    app:fontFamily="sans-serif-condensed">
                </TextView>

                <RelativeLayout
                    android:id="@+id/correctanswersQuiz4"
                    android:layout_marginTop="@dimen/allaroundmargin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/tvCorrectQuestionsQuiz4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/correctquestions"
                        android:textColor="@color/appbarTcolor"
                        app:fontFamily="sans-serif-condensed">
                    </TextView>

                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/wronganswersQuiz4"
                    android:layout_marginTop="@dimen/allaroundmargin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:fontFamily="sans-serif-condensed">

                    <TextView
                        android:id="@+id/tvWrongQuestionsQuiz4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/wrongquestions"
                        android:layout_marginTop="@dimen/allaroundmargin"
                        android:textColor="@color/appbarTcolor"
                        app:fontFamily="sans-serif-condensed">
                    </TextView>

                </RelativeLayout>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/quiz5LL"
                android:layout_below="@id/quiz4LL"
                android:layout_marginTop="@dimen/margin_small_between_btns"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/btn_quiz5"
                android:paddingLeft="@dimen/allaroundmargin"
                android:paddingStart="@dimen/allaroundmargin"
                android:paddingEnd="@dimen/allaroundmargin"
                android:paddingRight="@dimen/allaroundmargin">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_quiz3"
                    android:textColor="@color/appbarTcolor"
                    android:textStyle="bold"
                    android:textSize="@dimen/appbar_text_size"
                    app:fontFamily="sans-serif-condensed">
                </TextView>

                <RelativeLayout
                    android:id="@+id/correctanswersQuiz5"
                    android:layout_marginTop="@dimen/allaroundmargin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/tvCorrectQuestionsQuiz5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/correctquestions"
                        android:textColor="@color/appbarTcolor"
                        app:fontFamily="sans-serif-condensed">
                    </TextView>

                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/wronganswersQuiz5"
                    android:layout_marginTop="@dimen/allaroundmargin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:fontFamily="sans-serif-condensed">

                    <TextView
                        android:id="@+id/tvWrongQuestionsQuiz5"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/wrongquestions"
                        android:layout_marginTop="@dimen/allaroundmargin"
                        android:textColor="@color/appbarTcolor"
                        app:fontFamily="sans-serif-condensed">
                    </TextView>

                </RelativeLayout>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/quiz6LL"
                android:layout_below="@id/quiz5LL"
                android:layout_marginTop="@dimen/margin_small_between_btns"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/btn_quiz6"
                android:paddingLeft="@dimen/allaroundmargin"
                android:paddingStart="@dimen/allaroundmargin"
                android:paddingEnd="@dimen/allaroundmargin"
                android:paddingRight="@dimen/allaroundmargin">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_quiz6"
                    android:textColor="@color/appbarTcolor"
                    android:textStyle="bold"
                    android:textSize="@dimen/appbar_text_size"
                    app:fontFamily="sans-serif-condensed">
                </TextView>

                <RelativeLayout
                    android:id="@+id/correctanswersQuiz6"
                    android:layout_marginTop="@dimen/allaroundmargin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <TextView
                        android:id="@+id/tvCorrectQuestionsQuiz6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/correctquestions"
                        android:textColor="@color/appbarTcolor"
                        app:fontFamily="sans-serif-condensed">
                    </TextView>

                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/wronganswersQuiz6"
                    android:layout_marginTop="@dimen/allaroundmargin"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:fontFamily="sans-serif-condensed">

                    <TextView
                        android:id="@+id/tvWrongQuestionsQuiz6"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="@string/wrongquestions"
                        android:layout_marginTop="@dimen/allaroundmargin"
                        android:textColor="@color/appbarTcolor"
                        app:fontFamily="sans-serif-condensed">
                    </TextView>

                </RelativeLayout>

            </LinearLayout>

        </RelativeLayout>

    </ScrollView>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/adUnitID1">
    </com.google.android.gms.ads.AdView>
    <!--adview-->

</RelativeLayout>

Вот фрагмент кода createPDF ()

private void creatPDF() {
PdfDocument mypdfdoc = new PdfDocument();
PdfDocument.PageInfo mypageinfo1 = new PdfDocument.PageInfo.
Builder(1200, 2010, 1).create();

//page 1
PdfDocument.Page mypage1 = mypdfdoc.startPage(mypageinfo1);

//measurements for page 1
int measureWidth = View.MeasureSpec.makeMeasureSpec(mypage1.getCanvas().getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(mypage1.getCanvas().getHeight(), View.MeasureSpec.EXACTLY);

//put content on page 1
View contentQ1 = findViewById(R.id.allQuizzesSV);
contentQ1.measure(measureWidth, measuredHeight);
contentQ1.layout(0, 0, measureWidth, measuredHeight);
contentQ1.draw(mypage1.getCanvas());

//finish page 1
mypdfdoc.finishPage(mypage1);

String path1 = this.getExternalFilesDir(null).getAbsolutePath();
File mFile = new File(path1,"QuizSummaryN.pdf");

try{
mypdfdoc.writeTo(new FileOutputStream(mFile));
}catch (IOException e){
e.printStackTrace();
Toast.makeText(this, "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
}
mypdfdoc.close();
}

Вот пример вывода в формате PDF enter image description here

На рисунке я также описал свою проблему.

Не могли бы вы указать мне в правильном направлении? Большое спасибо

...