Writing .doc in Qt



  • Hello.

    Someone knows how to export the QPrinter contents in .doc?

    I could do it in pdf but, I can't do in .doc. Any idea?

    Thank you very much



  • I doubt you can "print" to doc...


  • Moderators

    @ivanicy
    Could you elaborate on what you want to do exactly?

    Maybe you just want to write to a Microsoft Word doc file format?



  • Yes sorry. This code generates a pdf document. I want to do something similar but in .doc to open with Microsoft Word:

    if (pdfButtonClicked) {
            QIcon icon(":/MainIcons/Resources/pdf_pressed_2.png");
            icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf_pressed_2.png"));
            ui->pdfButton->setIcon(icon);
    
            printer.setPageSize(QPrinter::A4);
            printer.setOrientation(QPrinter::Portrait);
            printer.setOutputFormat(QPrinter::PdfFormat);
    
            QString fileName = QFileDialog::getSaveFileName(this, "Save pdf...", QCoreApplication::applicationDirPath(), "PDF (*.pdf)" );
            if (!fileName.isNull())
            {
                printer.setOutputFileName(fileName);
                QImage imagen = screenshot.toImage();
                imagen = imagen.scaled(imagen.width() - 300, imagen.height()-100);
                QPainter painter(&printer);
                QRect rect = painter.viewport();
                rect.setSize(QSize(imagen.width(), imagen.height()));
                QSize size = imagen.size();
                size.scale(rect.size(), Qt::KeepAspectRatio);
                double xscale = printer.pageRect().width() / double(imagen.width()*1.19);
                double yscale = printer.pageRect().height() / double(imagen.height());
    
                double scale = qMin(xscale, yscale);
                painter.scale(scale, scale);
                painter.translate(150, 200);
                painter.drawImage(0, 0, imagen);
                painter.end();
    
                QIcon icon(":/MainIcons/Resources/pdf.png");
                icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf.png"));
                ui->pdfButton->setIcon(icon);
                pdfButtonClicked = false;
                QMessageBox::information(NULL, "Export PDF", "PDF created.", QMessageBox::Ok);
            } else {
                QIcon icon(":/MainIcons/Resources/pdf.png");
                icon.addPixmap(QPixmap(":/MainIcons/Resources/pdf.png"));
                ui->pdfButton->setIcon(icon);
                pdfButtonClicked = false;
            }
        }
    

    Thank you very much


  • Moderators

    @ivanicy
    Where do you get the data you want to write in the doc file? What is your starting point?



  • @Eddy In this moment, the "screenshot" is a QImage which I want to show in .doc file. Later, maybe I will want to add some text under the image. I put the image in the pdf document with "painter.drawImage()" function.



  • The closest thing is using QTextDocumentWriter

    QTextDocument tempDocument;
    QTextCursor tempCursor(&tempDocument);
    tempCursor.insertImage(imagen);
    // Use tempCursor to add text if you want
    QTextDocumentWriter writer;
    writer.setFormat("odf");
    writer.setFileName(fileName);
    if(!writer.write(&tempDocument))
    qCritical("Save Failed!");
    

    This format is recognised by Libre Office, Open Office and (recent versions of) MS Word



  • @VRonin Thank you very much. I made a few changes and it worked perfect!


Log in to reply
 

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