【问题标题】:Setting text size using Android PDFDocument based on PDF document/canvas size使用基于 PDF 文档/画布大小的 Android PDFDocument 设置文本大小
【发布时间】:2016-03-19 09:32:53
【问题描述】:

我正在尝试使用 Android PDFDocument 类将 LinearLayout 转换为 PDF。我将布局膨胀到一个 ViewGroup 中,将视图缩放到画布上,然后在画布上绘制。我成功制作了 PDF,但字体大小是根据设备分辨率/密度而不是 PDF 大小绘制的。基本上,字体最终在实际 PDF 上会很大。我相信这是由于 View 是根据设备屏幕的尺寸和密度绘制的,然后翻译到画布上。

我已经尝试以 px 和 pt 设置尺寸,但我似乎无法正确设置。当尺寸设置非常小(1-2dp 或 px)时,字体会显示为正确大小,但我知道在不同设备上运行时会出现问题。

缩放文本和视图尺寸以使其在最终 PDF 上显示为适当大小的最佳方法是什么(在 300 dpi 时大约为 12pt 字体)?我是否需要从设备屏幕中提取某种尺寸,然后根据设备和 PDF 画布之间的比例调整所有视图的大小??

从墙上摔下来我的头很痛。

谢谢,安迪

//Sets print options
PrintAttributes printAttrs = new PrintAttributes.Builder().
setColorMode(PrintAttributes.COLOR_MODE_COLOR).
setMediaSize(PrintAttributes.MediaSize.NA_LETTER).
setResolution(new PrintAttributes.Resolution("res1", PRINT_SERVICE, 300, 300)).
setMinMargins(PrintAttributes.Margins.NO_MARGINS).build();

//create PDF Document
PdfDocument document = new PrintedPdfDocument(context, printAttrs);
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(pageWidth, pageHeight, 1).create();


//Inflate an XML template into a view with a LinearLayout root
LinearLayout container = new LinearLayout(context);
LayoutInflater inflater = LayoutInflater.from(context);
View view = new View(context);
view = inflater.inflate(R.layout.layout_pdf_meal_template, container, true);

//Pull data/strings from SQLite
//My codes goes through and populates the data gathered from the database to the LinearLayout's subviews

//create page
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();

//Draw view on the page
int measureWidth = View.MeasureSpec.makeMeasureSpec(canvas.getWidth(), View.MeasureSpec.EXACTLY);
int measuredHeight = View.MeasureSpec.makeMeasureSpec(canvas.getHeight(), View.MeasureSpec.EXACTLY);
container.measure(measureWidth, measuredHeight);
container.layout(0, 0, canvas.getWidth(), canvas.getHeight());


container.draw(canvas);
document.finishPage(page);

【问题讨论】:

  • 找到解决办法了吗?

标签: android pdf canvas


【解决方案1】:

我发现这也是我的 PDF 文档的一个问题。因此,我没有在画布上绘制视图,而是创建了一个 TextPaint 对象并使用了方法 canvas.drawtext("string", x-position, y-position, textPaint)。

【讨论】:

    【解决方案2】:

    试试canvas.setDensity(72),因为它适用于位图? 见here

    或者使用 72 作为PrintAttributes.Resolution

    【讨论】:

      【解决方案3】:

      对任何问题表示歉意,因为这是我在 SO 上的第一篇文章!我通过按如下方式缩放画布解决了这个问题(尽管使用了 PrintDocumentAdapter 提供的 PdfDocument),其中 hdpi 和 vdpi 是 PdfDocument 的水平和垂直分辨率(在你的情况下都是 300,但也许你想要更多以获得更好的结果? ):

      ....
      
      PrintAttributes.MediaSize pageSize = printAttributes.getMediaSize();
      PrintAttributes.Resolution resolution = printAttributes.getResolution();
      int hdpi = resolution.getHorizontalDpi();
      int vdpi = resolution.getVerticalDpi();
      int availableWidth = pageSize.getWidthMils() * hdpi / 1000;
      
      ....
      content.setLayoutParams(new ConstraintLayout.LayoutParams(availableWidth, ConstraintLayout.LayoutParams.WRAP_CONTENT));
      
      int measureWidth = View.MeasureSpec.makeMeasureSpec(availableWidth, View.MeasureSpec.EXACTLY);
      content.measure(measureWidth, View.MeasureSpec.UNSPECIFIED);
      
      content.layout(0, 0, content.getMeasuredWidth(), content.getMeasuredHeight());
      canvas.scale(72f / hdpi, 72f / vdpi);
      content.draw(canvas);
      
      ....
      

      上面的content是我要打印的ConstraintLayout,包含几个TextViews。 WRAP_CONTENTUNSPECIFIED 允许视图采用正确的高度并正确打印,无论如何在我的情况下。

      【讨论】:

        猜你喜欢
        • 2015-03-12
        • 2014-05-13
        • 1970-01-01
        • 2019-07-22
        • 2015-10-06
        • 2012-01-12
        • 2012-03-04
        • 2012-08-02
        • 1970-01-01
        相关资源
        最近更新 更多