giowck
18th April 2011, 22:12
Hello guys,
I' trying to print a QTextDocument with custom header and footer.
I found this nice code: http://developer.qt.nokia.com/faq/answer/is_it_possible_to_set_a_header_and_footer_when_pri nting_a_qtextdocument
It works great, except that sometimes the text gets truncated on half page, so that it's not readable... Look here the result: http://dl.dropbox.com/u/1511663/test.pdf between page 2 and 3!
I think this is something related to page size.. But I don't know how it can be fixed, what is the problem?
Hope you can help with some hints. I don't expect you write full code for me, but I hope you can help me with some hints!
My full code is attached as test.zip
Thanks
ps. this is a great forum, thanks!
#include <QtGui>
int main(int argc, char **argv)
{
QApplication a(argc, argv);
QString content;
content.append("here my html");
QTextDocument td;
td.setHtml(content);
QPrinter p;
//p.setPageMargins(20, 20, 5, 20, QPrinter::Millimeter);
bool pdf = true; //use pdf as output?
if (pdf) {
QString outFile = QFileDialog::getSaveFileName(0, "Save PDF file",
QDir::homePath(),
"PDF files (*.pdf)");
if (outFile.isEmpty())
exit(1);
if (!outFile.contains(".pdf"))
outFile.append(".pdf");
p.setOutputFileName(outFile);
} else {
QPrintDialog pd(&p, 0);
pd.exec();
}
td.setPageSize(p.pageRect().size());
QRect innerRect = p.pageRect();
innerRect.setTop(innerRect.top() + 20);
innerRect.setBottom(innerRect.bottom() - 30);
QRect contentRect = QRect(QPoint(0,0), td.size().toSize());
QRect currentRect = QRect(QPoint(0,0), innerRect.size());
QPainter painter(&p);
int count = 0;
painter.save();
painter.translate(0, 30);
while (currentRect.intersects(contentRect)) {
td.drawContents(&painter, currentRect);
count++;
currentRect.translate(0, currentRect.height());
painter.restore();
painter.drawText(10, 10, QString("Header %1").arg(count));
painter.drawText(10, p.pageRect().bottom() - 10, QString("Footer %1").arg(count));
painter.save();
painter.translate(0, -currentRect.height() * count + 30);
if (currentRect.intersects(contentRect))
p.newPage();
}
painter.restore();
painter.end();
return 0;
}
I' trying to print a QTextDocument with custom header and footer.
I found this nice code: http://developer.qt.nokia.com/faq/answer/is_it_possible_to_set_a_header_and_footer_when_pri nting_a_qtextdocument
It works great, except that sometimes the text gets truncated on half page, so that it's not readable... Look here the result: http://dl.dropbox.com/u/1511663/test.pdf between page 2 and 3!
I think this is something related to page size.. But I don't know how it can be fixed, what is the problem?
Hope you can help with some hints. I don't expect you write full code for me, but I hope you can help me with some hints!
My full code is attached as test.zip
Thanks
ps. this is a great forum, thanks!
#include <QtGui>
int main(int argc, char **argv)
{
QApplication a(argc, argv);
QString content;
content.append("here my html");
QTextDocument td;
td.setHtml(content);
QPrinter p;
//p.setPageMargins(20, 20, 5, 20, QPrinter::Millimeter);
bool pdf = true; //use pdf as output?
if (pdf) {
QString outFile = QFileDialog::getSaveFileName(0, "Save PDF file",
QDir::homePath(),
"PDF files (*.pdf)");
if (outFile.isEmpty())
exit(1);
if (!outFile.contains(".pdf"))
outFile.append(".pdf");
p.setOutputFileName(outFile);
} else {
QPrintDialog pd(&p, 0);
pd.exec();
}
td.setPageSize(p.pageRect().size());
QRect innerRect = p.pageRect();
innerRect.setTop(innerRect.top() + 20);
innerRect.setBottom(innerRect.bottom() - 30);
QRect contentRect = QRect(QPoint(0,0), td.size().toSize());
QRect currentRect = QRect(QPoint(0,0), innerRect.size());
QPainter painter(&p);
int count = 0;
painter.save();
painter.translate(0, 30);
while (currentRect.intersects(contentRect)) {
td.drawContents(&painter, currentRect);
count++;
currentRect.translate(0, currentRect.height());
painter.restore();
painter.drawText(10, 10, QString("Header %1").arg(count));
painter.drawText(10, p.pageRect().bottom() - 10, QString("Footer %1").arg(count));
painter.save();
painter.translate(0, -currentRect.height() * count + 30);
if (currentRect.intersects(contentRect))
p.newPage();
}
painter.restore();
painter.end();
return 0;
}