我正在尝试使用Android PDFDocument类将LinearLayout转换为PDF.我将布局扩展为ViewGroup,将视图缩放到画布,然后绘制到画布上.我正在成功制作PDF,但字体大小是根据设备分辨率/密度而不是PDF大小绘制的.基本上,字体最终在实际PDF上很大.我相信这是由于视图是关于设备屏幕的尺寸和密度绘制的,然后转换为画布.
我已经尝试在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);