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

QTextDocument page break



  • Hi everyone,

    I am trying to generate pdf files thanks to QTextDocument and QPrinter. The problem is that the QTextDocument API doesn't provide solution to make page break. I was thinking about something like this but it doesn't work :

    void newPage(QTextDocument *document, QTextCursor *cursor)
    {
        int count = document->pageCount(); 
        while (document->pageCount()==count)
            cursor->insertHtml("<br>");
    }
    

    The problem is the pageCount remains always the same, I tried with the line count but it also remains constant even after adding html line breaks.

    So for now, I add html line breaks manually until the document create a new page, but it is a very poor solution as it is absolutely not flexible.
    I would like to keep it as simple as possible, this is why I didn't chose to use an external lib as KDReport.

    Do you have any idea ?

    Thanks !



  • Maybe you can refer to

    void QTextBlockFormat::setPageBreakPolicy(QTextFormat::PageBreakFlags policy)
    

    and

    void QTextFrameFormat::setPageBreakPolicy(QTextFormat::PageBreakFlags policy)
    

    They should also be able to be set in CSS

    page-break-before [ auto | always ] Make it possible to enforce a page break before the paragraph/table
    page-break-after [ auto | always ] Make it possible to enforce a page break after the paragraph/table



  • Thanks it works ! Page break is automatically added after each block !


Log in to reply