android - PrintedPDFDocument 生成的 PDF 包含具有某种网格布局的灰色框

标签 android pdf pdf-generation

在解决了许多问题之后,我终于能够使用 PrintedPDFClass 生成一个包含内容且未损坏的 PDF 文档。

但是问题是,PDF 中的内容似乎与我提供给它渲染的 View 没有任何关系...

我想要渲染的 View :

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

<ScrollView xmlns:kingdomspas="http://www.kingdomspas.com/android/custom"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TableLayout
            android:id="@+id/salesAgreementTableLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"></TableLayout>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >



                <ImageView
                    android:id="@+id/companyLogo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:contentDescription="@string/logo_description"
                    android:src="@drawable/company_logo" />

                <LinearLayout android:layout_height="match_parent" android:layout_width="match_parent"
                    android:orientation="vertical"
                    android:layout_weight="1">
                <ImageView
                    android:id="@+id/companyAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:contentDescription="@string/address_description"
                    android:src="@drawable/company_address" />

                    <FrameLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" >

                        <EditText
                            android:id="@+id/salesExecInitials"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:background="@drawable/round"
                            android:inputType="text"
                            android:maxLines="1"
                            android:paddingLeft="130dp"
                            android:singleLine="true" >

                            <requestFocus />
                        </EditText>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:paddingLeft="10dp"
                            android:text="@string/sales_exec_initials" />
                    </FrameLayout>

                </LinearLayout>
            </TableRow>

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >

                    <EditText
                        android:id="@+id/salesExecInitials"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/round"
                        android:inputType="text"
                        android:maxLines="1"
                        android:paddingLeft="130dp"
                        android:singleLine="true" >

                        <requestFocus />
                    </EditText>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:paddingLeft="10dp"
                        android:text="@string/sales_exec_initials" />
                </FrameLayout>
            </TableRow>
        </TableLayout>

        <View
            android:paddingTop="50dp"
            android:layout_width="fill_parent"
            android:layout_height="2dip"
            android:layout_weight="1"
            android:background="@android:color/darker_gray" />

        <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/submitButton"
            android:text="@string/submit_form"
            android:padding="10dp"/>
    </LinearLayout>

</ScrollView>

这会产生这样的用户界面:

Screenshot

我用来生成 PDF 的代码是:

Builder printAttrsBuilder = new Builder();
        printAttrsBuilder.setMediaSize(PrintAttributes.MediaSize.ISO_A4);
        printAttrsBuilder.setMinMargins(new Margins(5, 5, 5, 5));



        PrintedPdfDocument document = new PrintedPdfDocument(context, printAttrsBuilder.build());

        PageInfo pageInfo = new PageInfo.Builder(150, 150, 1).create();
        Page page = document.startPage(pageInfo);
        Canvas pdfCanvas = page.getCanvas();
        salesFragmentTableLayout.draw(page.getCanvas());
        document.finishPage(page);
        File result = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos= null;
        FileDescriptor descriptor = null;
        try {
            result = File.createTempFile("Kingdom Spas Agreement", ".pdf", context.getCacheDir());
            fos = new FileOutputStream(result);
            bos = new BufferedOutputStream(fos);
            document.writeTo(bos);
            descriptor = fos.getFD();
            descriptor.sync();
        } catch (FileNotFoundException e) {
            throw new KingdomSpasException("Failed to find relevent file", e);
        } catch (IOException e) {
            throw new KingdomSpasException("IO Problem occured while creatin the PDF", e);
        } finally {
            try {
                if (bos != null) { bos.flush(); bos.close(); }
            } catch (SyncFailedException e) {
                throw new KingdomSpasException("Failed to correctly sync PDF file - may be corrupted", e);
            } catch (IOException e) {
                throw new KingdomSpasException("Failed to correctly clean up streams", e);
            }
        }
        document.close();

但是创建的 PDF 看起来像(以 100% 缩放查看):

Resulting PDF

谁能向我解释为什么生成的 PDf(至少在我看来)与我的原始 View 如此不同,并解释如何修复它?

编辑:这里是生成的 PDF 的链接,以防提供任何有用的信息:https://dl.dropboxusercontent.com/u/134344/KingdomSpasAgreement.pdf

最佳答案

根据documentation :

public PdfDocument.PageInfo.Builder (int pageWidth, int pageHeight, int pageNumber)

Creates a new builder with the mandatory page info attributes.

Parameters

pageWidth The page width in PostScript (1/72th of an inch).

pageHeight The page height in PostScript (1/72th of an inch).

pageNumber The page number.

你使用一个

PageInfo pageInfo = new PageInfo.Builder(150, 150, 1).create();
Page page = document.startPage(pageInfo);

因此,您创建的页面大小仅为 2 英寸 x 2 英寸。在这个小区域中,只有提交按钮的一部分适合。 (PDF页面内容实际上甚至包含字符串“Submit”,但它远远超出了可视区域。)

当您使用PrintedPdfDocument时,您应该使用:

Page page = document.startPage(1);

(cf. the documentation again)

关于android - PrintedPDFDocument 生成的 PDF 包含具有某种网格布局的灰色框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27965538/

相关文章:

java - 使用 JavaFX 中的关联程序打开 PDF 文件

pdf - 使输出 pdf 文件二进制相同

asp.net-core - 在 ASP.NET Core 中将 html 导出为 pdf

ruby-on-rails - Rails PDFKit命令失败

android - 从服务启动 AlertDialog.Builder

java - 在 Java 项目中使用 android.jar - RuntimeException Stub?

android - "' 这个 ' is not available"在 Android Studio 的调试窗口中

java - 以编程方式设置工作日

pdf - 如何全局替换PDF中的字体

java - 无法下载内容类型为 text/html 的文件