在经历了许多问题之后,我终于能够生成一个具有内容并且没有使用PrintedPDFClass损坏的PDF文档。
但问题是,PDF中的内容似乎与我给它呈现的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>
这会导致这样的UI:
生成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%缩放的方式查看):
有人能向我解释为什么最终的PDf (至少在我看来)与我的原始视图如此不同,并解释如何修复它吗?
编辑:这里有一个链接到生成的PDF,以防出现任何有用的信息:https://dl.dropboxusercontent.com/u/134344/KingdomSpasAgreement.pdf
发布于 2015-01-26 14:56:19
根据文档
公共PdfDocument.PageInfo.Builder (int pageWidth,int pageHeight,int pageNumber) 创建一个具有强制页面信息属性的新构建器。 参数 pageWidth以PostScript为单位的页面宽度(1/72英寸)。 pageHeight页面高度,以PostScript (1/72英寸)为单位。 pageNumber的页码。
你用的是
PageInfo pageInfo = new PageInfo.Builder(150, 150, 1).create();
Page page = document.startPage(pageInfo);
因此,您创建的页面大小仅为2×2。在这个小区域中,只有提交按钮的一部分适合。( PDF页面内容实际上甚至包含字符串"Submit“,但它远远超出了可视区域。)
在使用PrintedPdfDocument
时,应该使用:
Page page = document.startPage(1);
(请参阅再来一次https://developer.android.com/reference/android/print/pdf/PrintedPdfDocument.html )
https://stackoverflow.com/questions/27965538
复制相似问题