How to print the same header on multiple pages
Hi;
I have to print a document of variable length and therefore have to check on several places for possible page breaks. I am using QPrinter with QPainter. That would not be a problem if I did not have to print a header and footer to every one of that pages. Is there any sane way of doing that? What would you recommend?
Thank you for your answers! Regards; Luka
Re: How to print the same header on multiple pages
Every time you call newPage() output your header and footer and remove that space from the page height available to your other content.
Re: How to print the same header on multiple pages
I'm afraid I don't quite understand your advice. Below is a sample code. In real code I have dozen while and for loops that may or may not run. And the header and footer expand through multiple lines using graphic, text and tables. I would like to make the code for header/footer reusable, so if I make a correction I would not have to scroll through the code and possibly miss something... I also have to print several different documents that have the same header/footer. Is it possible? And how? Thanks; Luka
Code:
dialog->setWindowTitle(tr("Print Document"));
if (dialog
->exec
() == QDialog::Accepted) { QString lorem
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc quis augue erat.";
ui
->lineEdit
->setText
(QString::number(printer.
height(),
10));
if (! painter.begin(&printer)) { // failed to open file
qWarning("Is the output file writable?");
return;
}
int i = 1;
int position = 0;
int our_height = 0;
while ( i <= 40 ) {
// make text 1 pt (px?) larger
painter.
setFont(QFont("Arial", i
));
// get size of rectangle of text
QRect text_size
= painter.
boundingRect(0,
0, printer.
width(),
0, Qt
::AlignJustify | Qt
::TextWordWrap,
QString::number(i,
10) + ") " + lorem
);
// calculate the height of so far used text
our_height = position + text_size.height();
// if our height is equal or greater than height of the page we introduce page break
if( our_height >= printer.height() ) {
printer.newPage();
our_height = 0;
position = 0;
}
/**
* How would I make this possible?
**/
if ( our_height == 0 ) {
// write_header();
// write_footer();
// position = position + height_of_header + height_of_footer;
}
// wrote our text
painter.
drawText(QRectF(0, position, printer.
width(), text_size.
height()), Qt
::AlignJustify | Qt
::TextWordWrap,
QString::number(i,
10) + ") " + lorem
);
// set our new position
i++;
if (our_height == 0) {
position = text_size.height();
}
else {
position = our_height;
}
}
painter.end();
}
Re: How to print the same header on multiple pages
Something like this:
Code:
#include <QtGui>
#include <QDebug>
static const QString header
("This is the\nheader\ntext, but you can render anything here");
static const QString footer
("This is the\nfooter\ntext");
{
painter.save();
painter.resetTransform();
painter.
setFont(QFont("Courier New",
12));
QRect boundingRect
= painter.
boundingRect(painter.
window(), Qt
::AlignJustify | Qt
::TextWordWrap, header
);
painter.drawText(boundingRect, Qt::AlignJustify | Qt::TextWordWrap, header);
painter.drawRect(boundingRect);
painter.restore();
return boundingRect.height();
}
{
painter.save();
painter.resetTransform();
painter.
setFont(QFont("Courier New",
12));
QRect boundingRect
= painter.
boundingRect(painter.
window(), Qt
::AlignJustify | Qt
::TextWordWrap, footer
);
painter.translate(0, painter.window().height() - boundingRect.height());
painter.drawText(boundingRect, Qt::AlignJustify | Qt::TextWordWrap, footer);
painter.drawRect(boundingRect);
painter.restore();
return boundingRect.height();
}
int main(int argc, char *argv[])
{
dialog.setWindowTitle("Print Document");
if (dialog.
exec() == QDialog::Accepted) { QString lorem
= "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc quis augue erat.";
if (painter.begin(&printer)) {
int position = 0;
// Render the first header and footer
int headerHeight = renderHeader(painter);
int footerHeight = renderFooter(painter);
position = headerHeight;
painter.translate(0, headerHeight); // I use the translation to avoid doing it manually
for (int i = 0; i < 40; ++i) {
painter.
setFont(QFont("Arial", i
));
QRect boundingRect
= painter.
boundingRect(painter.
window(),
Qt::AlignJustify | Qt::TextWordWrap,
QString::number(i,
10) + ") " + lorem
);
// if our height is equal or greater than height of the page we introduce page break
if( position + boundingRect.height() + footerHeight > painter.window().height() ) {
printer.newPage();
headerHeight = renderHeader(painter);
footerHeight = renderFooter(painter);
position = headerHeight;
painter.resetTransform();
painter.translate(0, headerHeight);
}
// write our text
painter.drawText(boundingRect,
Qt::AlignJustify | Qt::TextWordWrap,
QString::number(i,
10) + ") " + lorem
);
painter.translate(0, boundingRect.height());
position += boundingRect.height();
}
painter.end();
}
}
return 0;
}
Re: How to print the same header on multiple pages
Thank you for this demo, it is just what I needed. I read about save(), restore(), but did not know how to use them. Thank you also for showing me translate(), again it was a mystery for me. This will reduce my app for several thousand lines of code and make it useful again :)
Regards; Luka