Search code examples
androidpdfdocument

How to render selectable text using PdfDocument


I am generating a document from a custom model using PdfDocument but I am facing two problems:

  • the text written is not selectable on the generated pdf, and;
  • the pdf size is too big (probably because it renders the layout from the xml as a Bitmap).

Is there any way to solve those two problems without using a third-party lib?

Here's an example of how I'm generating a PDF (medium.com/@strawberryinc0531/…). I am using Android API 29+ for the tests


Solution

  • As the example you give is drawing a view to a bitmap and then adding a bitmap to a pdf, so you will get a pdf with a picture of text that is not selectable.

    So that is a really bad example of how to use the PDFDocument class.

    You can get a canvas directly from the PDFDocument page when you draw a view to this Text will be text and selectable.

    A better example of drawing views direct to a PDF is at https://github.com/Zardozz/RecyclerviewPdf

    Here it

    // Get PDF page canvas
    Canvas pageCanvas = page.getCanvas();
    // Draw current page
    parentView.draw(pageCanvas);