Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

convert word doc to pdf using qt cpp



  • how to convert document from word to pdf using qt cpp, that word document will contain tables and also images , text..



  • @harsha123
    Are you under Windows? For a Word document with that complexity I believe you will have to get Word application to do that itself, via "Print to PDF file". You would need to use Automation ("ActiveX" from Qt, QAxObject Class etc.) to run up Word application and get it to read in the file and print/save it to file as PDF.



  • yes i am doing in windows, taking text from word doc converting in to pdf is done, is it possible to convert word doc that contains tables and also images along with text.



  • Yes, doing what @JonB suggested is by far the easiest way to achieve this with anything you can put in a Word document.



  • Hi i am new to work with QAxobject class, I am facing some difficulty in converting word to pdf using ExportAsFixedFormat getting the following error, i problem is to convert from word doc to pdf doc in the current directory can u please suggest,![QAxBase::dynamicCallHelper: ExportAsFixedFormat(OutputFileName,ExportFormat,BOOL,BOOL,BOOL): No such property in [Microsoft Word Document]
    Candidates are:
    Email
    EmbedLinguisticData
    EmbedSmartTags
    EmbedTrueTypeFonts
    Endnotes
    EnforceStyle
    Envelope
    QAxBase::dynamicCallHelper: ExportAsFixedFormat(OutputFileName,ExportFormat,BOOL,BOOL,BOOL): No such property in [Microsoft Word Document]
    Candidates are:
    Email
    EmbedLinguisticData
    EmbedSmartTags
    EmbedTrueTypeFonts
    Endnotes
    EnforceStyle
    Envelope]

    Code:
    {
    QAxWidget word("Word.Application");
    word.setProperty("Visible", true);
    QAxObject * documents = word.querySubObject("Documents");
    QAxObject * document=documents->querySubObject("Open(QString)",QDir::currentPath()+ "/aaaa.doc");

    // vars for ExportAsFixedFormat
    
    QVariant OutputFileName(QDir::currentPath()+"/test.pdf");
    QVariant ExportFormat(17);
    QVariant OpenAfterExport(true);
    
    // converting to PDF facing problem here
    

    /* document->querySubObject("ExportAsFixedFormat(const QVariant&,const QVariant&,const QVariant&)",
    OutputFileName,
    ExportFormat,
    OpenAfterExport);*/
    document->dynamicCall("ExportAsFixedFormat(OutputFileName, ExportFormat, BOOL, BOOL, BOOL)", 0, OutputFileName, 0, false, false);

    //closing document
    
    document->dynamicCall("Close(boolean)",false);
    word.dynamicCall("Quit (void)");
    

    }



  • @harsha123
    Does this work:

            document->querySubObject("ExportAsFixedFormat(const QVariant&,const QVariant&,const QVariant&)",
    	OutputFileName,
    	ExportFormat,
    	OpenAfterExport);
    

    ?



  • No that is also not working tried in two ways as mentioned in the above code ,same error is coming.



  • @harsha123
    You seem to be using it in the correct way, as per e.g. https://www.codetd.com/en/article/9255551 and ExportAsFixedFormat().

    • Check all your return results, e.g. from QAxObject * document=documents->querySubObject("Open(QString)",QDir::currentPath()+ "/aaaa.doc"); etc.

    • Try some of the other examples in that thread, to see whether everything else works or whether it's only ExportAsFixedFormat() which fails.

    • Go try your call inside Word: write it in a VBA script there and/or try recording doing it manually and look at the generated code.



  • Ok, i try with that..
    thank you



  • @harsha123
    I think/hope the following is claimed to work from https://forum.qt.io/topic/128464/export-pdf-file-from-excel-template-with-qt-and-qaxobject/12

    workbook->dynamicCall("ExportAsFixedFormat(int,QString,int,int,int)",0,pdfFile,0,0,bIgnorePrintAreas);

    Note how that declares and passes the parameters. That is for an Ecel workbook, does that work same for a Word document?

    UPDATE
    Or for Word https://forum.qt.io/topic/127801/saving-from-word-to-pdf/2 claims

        document->querySubObject("ExportAsFixedFormat(const QVariant&,const QVariant&,const QVariant&)",
                                 OutputFileName,
                                 ExportFormat,
                                 OpenAfterExport);
    

    does work, compare yours to that. That's all I know!



  • when i try check the return value of document->querySubObject("ExportAsFixedFormat(const QVariant&,const QVariant&,const QVariant&)",
    OutputFileName,
    ExportFormat,
    OpenAfterExport);
    I am getting QObject(0x0).

    for this
    QVariant doc_op=document->dynamicCall("ExportAsFixedFormat(int,QString,int,int,int)",0,pdfFile,0,0,bIgnorePrintAreas);
    I am getting QVariant(Invalid)

    can any one suggest , I am trying but it is not working please suggest if am missing some thing.



  • Please suggest if i need to add anything regarding this https://stackoverflow.com/questions/42909112/saving-word-document-as-pdf
    in qt cpp , I dont understand how to include or how to proceed with this VBA script.



  • @harsha123
    You cannot use use the code in your last stackoverflow reference because it is VBA script and only for use from MS VB or within Word.

    I cannot see why your previous code should not work given that the thread I pointed you to says that it does.

    I would give a go at the Excel print-to-PDF code from https://forum.qt.io/topic/128464/export-pdf-file-from-excel-template-with-qt-and-qaxobject/12 to see whether that does or does not work for you for Excel instead of Word.



  • This ExportAsFixedFormat() is also failing for excel conversion also when checked the return type getting Like QVariant(Invalid).

    QAxBase::dynamicCallHelper: ExportAsFixedFormat(QVariant,QVariant,QVariant,QVariant,QVariant,QVariant,QVariant,QVariant,QVariant): No such property in [unknown]
    Candidates are:
    EnableAutoFilter
    EnableCalculation
    EnableOutlining
    EnablePivotTable
    EnableSelection

    I need to add anything in. pro other that please check and tell...

    QT += core gui
    QT += core gui axcontainer
    QT += widgets axcontainer
    QT +=printsupport
    QT += axcontainer
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

    TARGET = ms_Word_application
    TEMPLATE = app

    SOURCES += main.cpp
    mainwindow.cpp

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui

    Please suggest



  • @harsha123
    Looks OK, except no point keep adding the same QT +=module repeatedly.

    https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.exportasfixedformat says:

    An error occurs if the PDF add-in is not currently installed.

    I don't know that would cause your error message, but have you at least verified you can do the export to PDF what way from within your Excel/Word? Can you pick a different value for your ExportFormat to see whether non-PDF works? I think Word has a SaveAs method for PDF instead of Export, look up examples, does it work that way?

    You will have to figure how yourself, but if you take a VBA script example instead I think you can put it into a Word VBA script/macro inside Word and test running it from there. That needs to work....



  • That means i need to install any specific plugins for the conversion to be if so please mention the plugins to install.



  • @harsha123
    Like I told you, what I have typed is all I know. I don't even have Qt for Windows.



  • Thank u its working this code is working this problem is because of not having pulgings only finally it is resolved.


Log in to reply