Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. convert word doc to pdf using qt cpp

convert word doc to pdf using qt cpp

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 6 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    harsha123
    wrote on last edited by
    #1

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

    JonBJ 1 Reply Last reply
    0
    • H harsha123

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

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

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

      1 Reply Last reply
      3
      • H Offline
        H Offline
        harsha123
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • H Offline
            H Offline
            harsha123
            wrote on last edited by
            #5

            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)");
            

            }

            JonBJ 1 Reply Last reply
            0
            • H harsha123

              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)");
              

              }

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @harsha123
              Does this work:

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

              ?

              1 Reply Last reply
              0
              • H Offline
                H Offline
                harsha123
                wrote on last edited by
                #7

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

                JonBJ 1 Reply Last reply
                0
                • H harsha123

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

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

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

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    harsha123
                    wrote on last edited by
                    #9

                    Ok, i try with that..
                    thank you

                    JonBJ 1 Reply Last reply
                    0
                    • H harsha123

                      Ok, i try with that..
                      thank you

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @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!

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        harsha123
                        wrote on last edited by
                        #11

                        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.

                        1 Reply Last reply
                        0
                        • H Offline
                          H Offline
                          harsha123
                          wrote on last edited by
                          #12

                          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.

                          JonBJ 1 Reply Last reply
                          0
                          • H harsha123

                            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.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

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

                            1 Reply Last reply
                            0
                            • H Offline
                              H Offline
                              harsha123
                              wrote on last edited by
                              #14

                              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

                              JonBJ 1 Reply Last reply
                              0
                              • H harsha123

                                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

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #15

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

                                1 Reply Last reply
                                0
                                • H Offline
                                  H Offline
                                  harsha123
                                  wrote on last edited by
                                  #16

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

                                  JonBJ 1 Reply Last reply
                                  0
                                  • H harsha123

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

                                    JonBJ Offline
                                    JonBJ Offline
                                    JonB
                                    wrote on last edited by
                                    #17

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

                                    1 Reply Last reply
                                    0
                                    • H Offline
                                      H Offline
                                      harsha123
                                      wrote on last edited by
                                      #18

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

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved