Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Page number in QTextDocument for envelopes

Tags:

c++

qt

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.

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!

like image 754
shofee Avatar asked Feb 21 '23 08:02

shofee


1 Answers

You 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.

like image 124
ken Avatar answered Mar 04 '23 07:03

ken