PDA

View Full Version : QPrinter without page number



estanisgeyer
6th March 2008, 12:35
Good Day,

I'm perfectly printing the contents of a QTextEdit using QPrinter, but do not want to appear the page number. Has how?

Thanks,

Marcelo Geyer
Brazil

wysota
12th March 2008, 21:14
You can probably use QTextDocument::documentLayout() and QAbstractTextDocumentLayout::draw() to print each page yourself.

olosie
14th April 2009, 12:25
You can probably use QTextDocument::documentLayout() and QAbstractTextDocumentLayout::draw() to print each page yourself.

Is there any example how to do this? I cannot find anything in the network :(

wysota
14th April 2009, 22:12
I'm not sure what example you would want. You have to pass a QPainter initialized on the printer object to the method and an empty (default) paint context object and you're done.

waldyrious
15th October 2015, 13:19
This has been answered elsewhere (https://forum.qt.io/topic/14370/solved-page-number-in-qtextdocument-for-envelopes/2), but for those who arrive here seeking the solution, I'm reposting it: you need to set a pageSize in the document, like so:



QPrinter printer(QPrinter::PrinterResolution);
printer.setPaperSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOutputFileName(fileName);

QTextDocument document;
document.setHtml("<p>Lorem ipsum</p>");
document.setPageSize(printer.pageRect().size()); // <-- Here is key to the solution
document.print(&printer);

The explanation is simple, albeit not entirely intuitive. Paraphrasing @shoyeb from that thread:


When you refer the source code of print(), then you will recognize that the < QPointF pageNumberPos; > is only defined when there is no valid QTextDocument.pageSize().
In printPage() the page number only will be printed if pageNumberPos is not null.
So just set a valid QTextDocumtent.pageSize() and you'll have no page numbers on your printed document.