2015-06-17 78 views
2

將視圖繪製到PrintedPdfDocument畫布中時,PDF的大小(以字節爲單位)可能會顯着增加,特別是當視圖包含位圖(例如ImageView)時。以減少最終的尺寸Android:PrintedPdfDocument分辨率無效

一個方法應該是在PrintAttributes分辨率領域,例如:

PrintAttributes printAttrs = new PrintAttributes.Builder(). 
       setColorMode(PrintAttributes.COLOR_MODE_COLOR). 
       setMediaSize(PrintAttributes.MediaSize.ISO_A4). 
       setResolution(new Resolution("zooey", PRINT_SERVICE,hDpi,vDpi)). 
       setMinMargins(Margins.NO_MARGINS). 
       build(); 
PdfDocument document = new PrintedPdfDocument(this, printAttrs); 

然而,無論我選擇的華電國際和vDpi的PDF最終的大小不會改變的。

我做錯了什麼?我怎樣才能減小PDF的大小?

回答

1

根據我的經驗,分辨率設置不影響PrintedPDfDocument文件生成的最終結果。

以下是PrintedPDfDocument構造函數的源代碼。

private static final int POINTS_IN_INCH = 72; 

public PrintedPdfDocument(Context context, PrintAttributes attributes) { 
    MediaSize mediaSize = attributes.getMediaSize(); 

    // Compute the size of the target canvas from the attributes. 
    mPageWidth = (int) (((float) mediaSize.getWidthMils()/MILS_PER_INCH) 
      * POINTS_IN_INCH); 
    mPageHeight = (int) (((float) mediaSize.getHeightMils()/MILS_PER_INCH) 
      * POINTS_IN_INCH); 

    // Compute the content size from the attributes. 
    Margins minMargins = attributes.getMinMargins(); 
    final int marginLeft = (int) (((float) minMargins.getLeftMils()/MILS_PER_INCH) 
      * POINTS_IN_INCH); 
    final int marginTop = (int) (((float) minMargins.getTopMils()/MILS_PER_INCH) 
      * POINTS_IN_INCH); 
    final int marginRight = (int) (((float) minMargins.getRightMils()/MILS_PER_INCH) 
      * POINTS_IN_INCH); 
    final int marginBottom = (int) (((float) minMargins.getBottomMils()/MILS_PER_INCH) 
      * POINTS_IN_INCH); 
    mContentRect = new Rect(marginLeft, marginTop, mPageWidth - marginRight, 
      mPageHeight - marginBottom); 
} 

你可以看到,代碼不使用DPI參數,它採用了72作爲固定DPI來計算的頁面寬度/高度,我想是不對的。

當我試圖使用PrintedPDfDocument API在平板電腦上打印15頁網頁時,我收到了1G PDF文件。

所以我對你的問題的建議是使用另一個PDF生成庫,直到PrintedPDfDocument稍後證明自己。

祝你好運。