简体   繁体   English

如何从 android 工作室(java)中的视图创建高质量的 PDF?

[英]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.

相关问题 如何调整图库中的高质量图像的尺寸以使其出现在图像视图中? - How to resize high quality image from gallery to appear in image view? 如何从 android SpeechRecognizer 中的 RecognitionListener 获取高质量的录制音频? - How to get a high quality recorded audio from RecognitionListener in android SpeechRecognizer? 如何从图库或照相机中获取图像并在android中进行高质量裁剪 - How to get the images from gallery or camera and crop with high quality in android 如何在Android中将高质量图像提升为imageview? - How to uplaod high quality image to imageview in android? 如何在android中获得高质量的位图? - How to get high quality bitmap in android? 在图像视图中旋转两次以获取图库中的高质量图像时,Android应用程序崩溃 - Android app crashes when rotated twice in image view for high quality images from gallery pdf查看android studio - Pdf view android studio Android Studio呈现:属性“ breakStrategy”中的“ high_quality”不是有效的整数 - Android Studio Rendering: “high_quality” in attribute “breakStrategy” is not a valid integer 如何从arcore获得高质量的图像 - How to get high quality image from arcore 我如何从视图的屏幕截图中获取高质量的图片 - how can i get a high quality picture from a view’s screenshots
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM