Page 1 of 1

Convert doc to HTML or PDF using OpenOffice with COMatePLUS

Posted: Fri Jun 01, 2012 5:07 pm
by zapman*

Code: Select all

;
; Convert a Word or OpenOffice writer document to an HTML document or PDF document
;
; This program uses OpenOffice as background application to make the conversion
; so, you need to have OpenOffice installed on your machine to use this.
;
; You also need COMatePLUS to be installed into your machine.
; it can be downloaded at http://www.nxsoftware.com/ (it's free)
;
;
;
; You'll be first asked to open a Word (.doc or .docx) or OpenOffice document (.odt)
; a directory named "essai" will be created à the same level as your original
; document and an HTML or PDF version of you document will be generated in this folder


;works with OpenOffice 2/3 and also with IBM Symphony (aka Open Office with less bugs)
;
; References :  http://www.forums.purebasic.com/english/viewtopic.php?f=13&t=42440&hilit=comate+plus+openoffice&sid=cf693c6912feba076ad6ba0fbc14333a
;               http://www.purebasic.fr/english/viewtopic.php?p=258125#p258125
;               http://www.kalitech.fr/clients/doc/VB_APIOOo_fr.html
;               http://www.oooforum.org/forum/viewtopic.phtml?t=66772
;
;
#GenerateHTML = 1 ; set this to zero to generate PDF instead of HTML
;
;
;  
XIncludeFile "COMatePLUS.pbi" ; it can be downloaded at http://www.nxsoftware.com/
XIncludeFile "VariantHelper_Include.pb" ; included with COMatePLUS

Procedure.S OOoConvertToUrl(strFile.S)
  strFile = ReplaceString(strFile, "\", "/")
  strFile = ReplaceString(strFile, ":", "|")
  strFile = ReplaceString(strFile, " ", "%20")
  strFile = "file:///" + strFile
  ProcedureReturn strFile
EndProcedure
;
;
Procedure OOoMakePropertyValue(cName.S, uValue.S, datatype.s="")
  Define.COMateObject oServiceManager,oStruct
  Define *ret.variant, iDisp.iDispatch
  oServiceManager = COMate_CreateObject("com.sun.star.ServiceManager")
  If oServiceManager
    *ret = oServiceManager\GetVariantProperty("Bridge_GetStruct('com.sun.star.beans.PropertyValue')")
    If *ret
      If *ret\vt <> #VT_DISPATCH
        VariantChangeType_(*ret, *ret, 0, #VT_DISPATCH)
      EndIf
      If *ret\vt = #VT_DISPATCH
        iDisp = *ret\pdispVal
        oStruct = COMate_WrapCOMObject(iDisp)
        If oStruct
          oStruct\SetProperty("Name = '" + cName+"'")
          If datatype = "boolean"
            oStruct\SetProperty("Value = " + uValue+" as boolean")
          Else
            oStruct\SetProperty("Value = '" + uValue+"'")
          EndIf

          oStruct\Release() ;Required to keep the reference count in check.
        EndIf
      Else
        VariantClear_(*ret)
      EndIf
      FreeMemory(*ret) ;We do not use VariantClear_() as we do not wish to release the iDisp object which we are returning from this function.
    EndIf
  EndIf
  ProcedureReturn iDisp
EndProcedure

Procedure OOoMakeBoolPropertyValue(cName.S, uValue.b)
  sbValue.s = Str(uValue)
  ProcedureReturn OOoMakePropertyValue(cName.S, sbValue, "boolean")
EndProcedure 

Define.COMateObject oSM, oDesk, oDoc, oCalc

Define.safearray *openpar
Define.safearray *openpar1
Define.variant openarray
Define.variant openarray1

document.s= OpenFileRequester("Désignez un document .doc ou .odt","","Fichier OpenOffice|*.odt|Fichier Word|*.doc;*.docx" ,0)
If document
        outputfname$ = GetPathPart(document)+"essai\"
        CreateDirectory(outputfname$)
        If #GenerateHTML = 1
          outputfname$+ReplaceString(GetFilePart(document),"."+GetExtensionPart(GetFilePart(document)),".html")
        Else
          outputfname$+ReplaceString(GetFilePart(document),"."+GetExtensionPart(GetFilePart(document)),".pdf")
        EndIf

  WNum=OpenWindow(#PB_Any,100,100,200, 50,"Converting the file")
  If IsWindow(WNum)
    GTxt = TextGadget(#PB_Any, 10, 8, (WindowWidth(WNum)-19), 40, "Opening the document...", #PB_Text_Center)
    While WindowEvent():Wend
  EndIf
        
  oSM = COMate_CreateObject("com.sun.star.ServiceManager")
  ; en visual basic : Set oSM = CreateObject("com.sun.star.ServiceManager")
  If oSM = 0
    Debug COMate_GetLastErrorDescription()
  Else
    ;Creating instance of Desktop
    oDesk = oSM\GetObjectProperty("CreateInstance('com.sun.star.frame.Desktop')")
    ; en visualbasic : Set oDesk = oSM.createInstance("com.sun.star.frame.Desktop")
    If oDesk = 0
      Debug COMate_GetLastErrorDescription()
    Else
      ; Creating a new document
      ; en visual basic : Set oDoc = oDesk.loadComponentFromURL("private:factory/swriter", "_blank", 0, arg())
      ; en PureBasic : oDoc = oDesk\GetObjectProperty("loadComponentFromURL('private:factory/swriter', '_blank', 0, " + Str(openarray) + " as variant)")
      ;
      ; Opening a existing Document
      
      *openpar = saCreateSafeArray(#VT_DISPATCH, 0, 1) 
      SA_DISPATCH(*openpar, 0) = OOoMakeBoolPropertyValue("Hidden", #True)
      V_ARRAY_DISP(openarray) = *openpar
      ;
      oDoc = oDesk\GetObjectProperty("loadComponentFromURL('"+OOoConvertToUrl(document)+"', '_blank', 0, " + Str(@openarray) + " As variant)")
      ;en visual basic : Set oDoc = oDesk.loadComponentFromURL("file:///c:/dev/ooo/test.doc", "_blank", 0, arg())     
      
      If oDoc = 0
        Debug COMate_GetLastErrorDescription()
      Else
        If IsWindow(WNum)
          SetGadgetText(GTxt,"Saving the document...")
          While WindowEvent():Wend
        EndIf
        *openpar1 = saCreateSafeArray(#VT_DISPATCH, 0, 1)
        ;
        If #GenerateHTML = 1
          SA_DISPATCH(*openpar1, 0) = OOoMakePropertyValue("FilterName","HTML (StarWriter)")
        Else
          SA_DISPATCH(*openpar1, 0) = OOoMakePropertyValue("FilterName","writer_pdf_Export")
        EndIf
        ;
        ;
        V_ARRAY_DISP(openarray1) = *openpar1
        ;
        oDoc\Invoke("storeToURL('"+OOoConvertToUrl(outputfname$)+"',"+ Str(@openarray1)+ " As Variant)");
        
        RunProgram(outputfname$) 

        oDoc\Invoke("dispose")
        oDoc\Release()
       
      EndIf
      oDesk\Release()
    EndIf
    oSM\Release()
  EndIf
  ;
  If IsWindow(WNum)
    CloseWindow(WNum)
  EndIf
EndIf

Re: Convert doc to HTML or PDF using OpenOffice with COMateP

Posted: Wed Dec 12, 2012 12:12 am
by tola
it's great !!!!!
Thanks a lot.