[英]How to create high quality PDF from a view in android studio (java)?
I want to create a PDF file of a linear layout of one of my activities.我想为我的一项活动创建一个线性布局的 PDF 文件。 An example can be found in the following: https://medium.com/@strawberryinc0531/convert-xml-to-pdf-in-android-studio-using-pdfdocument-class-757dee166d50
示例如下: https://medium.com/@strawberryinc0531/convert-xml-to-pdf-in-android-studio-using-pdfdocument-class-757dee166d50
I am creating a bitmap initially.我最初创建一个 bitmap。 With or without scaling, the generated pdf from the bitmap results in low-resolution content (ie, zoomed pdf file is blurry).
无论是否缩放,从 bitmap 生成的 pdf 都会产生低分辨率内容(即,缩放后的 pdf 文件是模糊的)。 Can I create a vectorized image of a linear layout to create a PDF?
我可以创建线性布局的矢量化图像来创建 PDF 吗?
Don't scale your Bitmap down to fit the PDF page size, set a scale on the Canvas you get from the PDF Document.不要缩小 Bitmap 以适应 PDF 页面大小,在从 PDF 文档获得的 Canvas 上设置比例。
This stores the bitmap at the original resolution but displays it at the correct size to fit your page size.这将以原始分辨率存储 bitmap,但以适合页面大小的正确大小显示它。 Thus as you zoom in on the PDF it does not pixelate until you run out of detail in the original bitmap.
因此,当您放大 PDF 时,它不会像素化,直到您用完原始 bitmap 中的细节。
eg例如
// Prep linearLayout to draw to Bitmap
....
// create page
PdfDocument.Page page = document.startPage(pageInfo);
Canvas pageCanvas = page.getCanvas();
// Work out scale to use to fit width of page
// In this instance the bitmap is wider than the height
// so we don't need to work out if we need a smaller scale to fit the height on to the page
float scaleWidth = (float) pageCanvas.getWidth() / linearLayout.getWidth();
// Draw the Layout to a bitmap at it's original size
Canvas bitmapCanvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(linearLayout.getWidth(), linearLayout.getHeight(), Bitmap.Config.ARGB_8888);
bitmapCanvas.setBitmap(bitmap);
linearLayout.draw(bitmapCanvas);
// The important bit to set the page scale
pageCanvas.scale(scaleWidth, scaleWidth);
// Draw the original sized bitmap to page
Paint paint = new Paint();
paint.setAntiAlias(true);
pageCanvas.drawBitmap(bitmap,0,0,paint);
If you look at the PDF it creates如果您查看它创建的 PDF
Thus in this instance the image contained in the PDF is 1322 x 669 but it is displayed in a MediaBox of 594 x 841, thus it has about 2.2 times more details to zoom in to因此在这个例子中,包含在 PDF 中的图像是 1322 x 669 但它显示在 594 x 841 的 MediaBox 中,因此它有大约 2.2 倍的细节可以放大
Another way is to draw your layout directly to the PDF page without going through a bitmap as this create create pdf "primatives" like text and line objects that scale better, BUT you have to size your layout to fit the PDF page size.另一种方法是将您的布局直接绘制到 PDF 页面,而无需通过 bitmap,因为这会创建 pdf “基元”,如缩放更好的文本和线条对象,但您必须调整布局大小以适应 PDF 页面大小。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.