Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Printing large text on multiple pages



  • Hello everyone,
    I'm developing a program for printing plain text. My code works successfully with small text. However, with large text, only the first page is printed and the last line is cropped.
    How to predict which part of the text will be the last on a page in order to continue printing this text on the next page?

    My snippet:

    //...
    QPrinter *printer = paramParser.getPrinter();
    painter.begin(printer);
    
    QFile file(path);
    file.open(QIODevice::ReadOnly);
    QString text = QString::fromUtf8(file.readAll());
    file.close();
    painter.drawText(contentArea,text);
    
    painter.end();
    //...
    


  • @R-Kagirov Hi,
    the simplest way would be to wrap up your text in QTextDocument (there is a setter for plain text too), init the printer as usual and use QTextDocument::drawContents() - this way you don't need to worry about pagination, it is all done for you.

    Edit: corrected a typo.



  • It works. Thank you.


Log in to reply