[SOLVED] Page number in QTextDocument for envelopes



  • Hi,

    I'm writing an application that prints addresses directly onto envelopes. I'm using QTextDocument and the problem is that its method print() adds the page number, which is incorrect in envelopes.

    From the docs of the class:

    bq. If the document is not paginated, like for example a document used in a QTextEdit, then a temporary copy of the document is created and the copy is broken into multiple pages according to the size of the QPrinter's paperRect(). By default a 2 cm margin is set around the document contents. In addition the current page number is printed at the bottom of each page.

    How can I avoid this addition of the page number (which BTW I think should be optional)?

    Some code, in case you need it:
    @
    void MainWindow::print()
    {
    QString addressText = textEdit->document()->toPlainText();
    envelopeDocument = new QTextDocument(this);
    printer.setResolution(QPrinter::HighResolution);
    printer.setPrinterName("OKI B6200(PCL6)");
    printer.setOrientation(QPrinter::Landscape);
    QFont font("Trebuchet MS");
    switch (envelopeComboBox->currentIndex()){
    case 0:
    font.setPointSize(12);
    envelopeDocument->setDefaultFont(font);
    envelopeDocument->setPlainText(addressText);
    printer.setPaperSize(QSizeF(114,225),QPrinter::Millimeter);
    printer.setPageMargins(120,60,20,15,QPrinter::Millimeter);
    break;
    case 1:
    font.setPointSize(14);
    envelopeDocument->setDefaultFont(font);
    envelopeDocument->setPlainText(addressText);
    printer.setPaperSize(QSizeF(184,262),QPrinter::Millimeter);
    printer.setPageMargins(140,100,20,20,QPrinter::Millimeter);
    break;
    case 2:
    font.setPointSize(16);
    envelopeDocument->setDefaultFont(font);
    envelopeDocument->setPlainText(addressText);

        printer.setPaperSize(QSizeF(227,324), QPrinter::Millimeter);
        printer.setPageMargins(170,120,30,40,QPrinter::Millimeter);
        break;
    }
    
    QPrintPreviewDialog preview (&printer,this);
    preview.setWindowFlags(Qt::Window);
    connect(&preview, SIGNAL(paintRequested(QPrinter *)),SLOT(printPreview(QPrinter *)));
    preview.exec();
    

    }

    void MainWindow::printPreview(QPrinter *p)
    {
    envelopeDocument->print(p);
    }@

    Thank you!



  • hey u can try the following code, it might help you...

    @ QPrinter printer(QPrinter::ScreenResolution);
    printer.setPaperSize(QPrinter::A4);
    printer.setOutputFormat(QPrinter::PdfFormat);
    printer.setOutputFileName( fileName );
    // printer.setPageMargins(0.925, 0.8, 0.5, 0.8, QPrinter::Inch);

    QSizeF paperSize;
    paperSize.setWidth(printer.width());
    paperSize.setHeight(printer.height());
    document->setHtml(html);
    document->setPageSize(paperSize); // the document needs a valid PageSize
    document->print(&printer);@
    

    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 will be just printed, if pageNumberPos is not null.
    So just set a valid QTextDocumtent.pageSize() and you have no page numbers on your printed document.



  • I checked the source code and it seems there's no if() statement to check if you want to have the page number printed out or not. Correct me if I'm wrong, but I believe everything is actually printed by this function:
    @static void printPage(int index, QPainter *painter, const QTextDocument doc, const QRectF &body, const QPointF &pageNumberPos)@
    It's being internally used by the print() method of QTextDocument. Maybe it would be possible to used that one directly and if you won't specify the pageNumberPos then it won't be printed out:
    @if (!pageNumberPos.isNull())
    /

    printing page number code
    */@



  • @d2uriel: Hi! Could you please tell me in which class did you find that function? I checked QTextDocument and QPrinter source code and didn't find it.

    The thing is that that function should be public in order to be usable by other classes...

    Regards :-)



  • @David_Gil: I found it under Qt installation folder and then: src/gui/text/qtextdocument.cpp



  • hey did try my code snippet...
    did it work 4 u..



  • @d2uriel: Sorry, I just found it. The function printPage can't be used directly, it isn't public.

    Regards



  • @shoyeb: I'm going to do it right now :-)



  • @shoyeb: It worked!

    Thank you very much (and happy hacking :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.