2015-01-15 18 views
2

許多問題後,我終於能夠生成一個PDF文檔,其內容並沒有損壞使用PrintedPDFClass。PrintedPDFDocument導致PDF格式包含灰色框格排列

然而,在PDF內容似乎問題不承擔任何關係,我給它渲染視圖...

,我想呈現的觀點:

<?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:

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(我的眼睛至少)如此不同從我的原始視圖,並解釋如何解決它?

編輯:下面是情況下生成的PDF的鏈接rovides任何有用的信息:https://dl.dropboxusercontent.com/u/134344/KingdomSpasAgreement.pdf

+0

你究竟在哪裏渲染視圖? –

+0

該視圖呈現在最初的活動片段中,並且當用戶單擊該按鈕時,該視圖將被繪製爲pdf。 –

+0

你需要顯示你的整個代碼。 –

回答

2

按照documentation

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

創建一個具有強制性的頁面信息屬性的新建設者。

參數

頁寬中的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頁面的內容居然連包含字符串「提交」,但它是迄今爲止可視區域外)

當你使用的是PrintedPdfDocument,你應該使用:

Page page = document.startPage(1); 

(參見the documentation

+0

也許這[link](https://developer.android.com/training/printing/custom-docs.html#draw-content)可以幫助 – Meet