Create PDF with automatically new page (QTExtDocument, QTextCursor, ...)
Unsolved
General and Desktop
-
Hello,
Here a code which allow me te create a pdf with automatically add a new page :
void Class1::addContent() { m_painter->setPen(Qt::green); m_painter->drawRect(m_rectContent); QTextDocument tdContent; QTextOption textOptionContent; textOptionContent.setAlignment(Qt::AlignJustify); textOptionContent.setWrapMode(QTextOption::WordWrap); tdContent.setDefaultTextOption(textOptionContent); tdContent.setTextWidth(m_writer->width()); setContent(&tdContent, 0); } void Class1::setContent(QTextDocument *doc, int beginPosition) { doc->clear(); QString htmlText; m_cursor = new QTextCursor(doc); m_cursor->insertHtml(m_totalHtml); //m_html is my QString to print int lenTextToPrint = m_cursor->selectionEnd()-beginPosition; qDebug() << " "; qDebug() << "============"; qDebug() << "Page n°" << m_nbPage; qDebug() << "Initial text lenght" << m_cursor->selectionEnd(); qDebug() << "Begin position" << beginPosition; qDebug() << "Text to print" << lenTextToPrint; if (beginPosition != 0) { m_cursor->setPosition(beginPosition-1); m_cursor->movePosition(QTextCursor::End, QTextCursor::KeepAnchor); htmlText = m_cursor->selection().toHtml(); m_cursor->select(QTextCursor::Document); m_cursor->removeSelectedText(); m_cursor->insertHtml(htmlText); } else { htmlText = m_totalHtml; } qDebug() << "Current TextDoc height" << doc->documentLayout()->documentSize().height(); qDebug() << "Available per page" << m_rectContent.height(); int actPosi = 0; if (doc->documentLayout()->documentSize().height() > m_rectContent.height()){ doc->clear(); while(doc->documentLayout()->documentSize().height() < m_rectContent.height()) { QTextCursor *cursor = new QTextCursor(doc); cursor->insertHtml(htmlText); cursor->setPosition(actPosi); cursor->movePosition(QTextCursor::Down); actPosi = cursor->position(); cursor->movePosition(QTextCursor::End, QTextCursor::KeepAnchor); cursor->removeSelectedText(); } paintContent(doc); m_alreadyPaint += actPosi; qDebug() << "Last Position" << actPosi; qDebug() << "Already print" << m_alreadyPaint; if (actPosi < lenTextToPrint){ newPage(); setContent(doc, m_alreadyPaint); } } else { paintContent(doc); } } void Class1::paintContent(QTextDocument *doc) { m_painter->save(); m_painter->translate(m_rectContent.topLeft()); doc->drawContents(m_painter); m_painter->restore(); } void Class1::print() { m_writer = new QPdfWriter(m_fileName); m_writer->setResolution(m_resolution); QPageSize pageSize(QPageSize::A4); QMarginsF margins(m_margins, m_margins, m_margins, m_margins); QPageLayout pageLayout(pageSize, QPageLayout::Portrait, margins, QPageLayout::Millimeter); m_writer->setPageLayout(pageLayout); m_nbPage = 1; m_heightAvailable = m_writer->height(); m_rectContent.setCoords(0, 0, m_writer->width(), 0); m_painter = new QPainter(m_writer); addContent(); }
I have 2 questions :
- Do you have any suggestions for improving my code or otherwise ?
- Sometimes the new pages begin with a letter less to the first, have you an idea to fixe the issues ?
You can test my code by downloading it here : https://github.com/jiyuusoft/QMLReports
Thank you in advance for your help and comments.
Charlie