0

I am using below code to draw text and write in PDF, but it is not able to draw in multiple pages? What would be the best approach for this problem.

 PdfDocument document = new PdfDocument();
        PdfDocument.PageInfo pageInfo = new
                PdfDocument.PageInfo.Builder(pageWidth, pageheight, 3).create();
        PdfDocument.Page page = document.startPage(pageInfo);
        Canvas canvas = page.getCanvas();


            TextPaint paint=new TextPaint(Paint.ANTI_ALIAS_FLAG);
            paint.setColor(Color.rgb(0, 0, 0));
            paint.setTextSize((int) (7 * 2));
            paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

            int textWidth = canvas.getWidth() - (int) (16 * 2);

            StaticLayout textLayout = new StaticLayout(
                    edittextContent.getText().toString(), paint, pageWidth-50, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);

            int textHeight = textLayout.getHeight();

            float x = ((pageWidth-15) - textWidth)/2;
            float y = ((pageheight-15) - textHeight)/2;

            // draw text to the Canvas center
            canvas.save();
            canvas.translate(15, 10);
            textLayout.draw(canvas);
            canvas.restore();

        document.finishPage(page);
        document.writeTo(fOut);
        document.close();
Jasmine John
  • 873
  • 8
  • 12
  • This is exactly the same problem which I faced and didn't find any good answers. I solved it by using StaticLayout and limiting the number of characters in a particular page, then divide the long string into that many pages accordingly, loop through it according to the number of pages and print. **Check my answer where I have explained with sample code how to handle it.** https://stackoverflow.com/a/71394812/12552434 – abby Mar 08 '22 at 13:54

1 Answers1

1

I assume you are using native android android.graphics.pdf.PdfDocument. It really looks like it only provides the low-level functionality of placing the text lines at specific coordinates on a specific page. If you want to stick to this API, you would probably need to call startPage() and finishPage() for every page that you want to create in your document and prepare split content for every page. Something like this:

// start 1st page
PdfDocument.Page page = document.startPage(pageInfo);
// draw something on the page
// finish 1st page
document.finishPage(page);

// start 2nd page
PdfDocument.Page page = document.startPage(pageInfo);
// draw something on the page
// finish 2nd page
document.finishPage(page);

...

If you want more high-level functionality which would span multiple pages if content doesn't fit, you would need to use some other tools.

E.g. you can try iText7 library:

PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
Document document = new Document(pdf, new PageSize(pageWidth, pageheight));
// set here any amount of text, it will span across multiple pages if needed
String text = ...;
document.add(new Paragraph(line));
document.close();

See https://kb.itextpdf.com/home/it7kb/ebooks/itext-7-building-blocks/chapter-2-adding-content-to-a-canvas-or-a-document for more examples.

  • Suppose If I have a very long string then how can your code help in auto multiline and multipage?..Even if I use StaticLayout to solve the multiline problem the multipage problem stays – abby Mar 02 '22 at 13:27