Commits

Anonymous committed 0cd6d6d

contextmenu1: Added context menu item to export as PDF

Comments (0)

Files changed (2)

framework/workben/ctxtmnmerging/ole2graphic/Addons.xcu

                                 <value>vnd.sun.star.script:OLE2GraphicMenuMerging.oxt|python|SaveOLEAsGraphic.py$saveOLEAsGraphic?language=Python&amp;location=user:uno_packages</value>
                             </prop>
                         </node>
+                        <node oor:name="M002" oor:op="replace">
+                            <prop oor:name="Title" oor:type="xs:string">
+                                <value xml:lang="en-US">Save as PDF</value>
+                            </prop>
+                            <prop oor:name="URL" oor:type="xs:string">
+                                <value>vnd.sun.star.script:OLE2GraphicMenuMerging.oxt|python|SaveOLEAsGraphic.py$saveOLEAsPDF?language=Python&amp;location=user:uno_packages</value>
+                            </prop>
+                        </node>
                     </node>
                 </node>
                 <node oor:name="S2" oor:op="replace">
                                 <value>vnd.sun.star.script:OLE2GraphicMenuMerging.oxt|python|SaveOLEAsGraphic.py$saveOLEAsGraphic?language=Python&amp;location=user:uno_packages</value>
                             </prop>
                         </node>
+                        <node oor:name="M002" oor:op="replace">
+                            <prop oor:name="Title" oor:type="xs:string">
+                                <value xml:lang="en-US">Save as PDF</value>
+                            </prop>
+                            <prop oor:name="URL" oor:type="xs:string">
+                                <value>vnd.sun.star.script:OLE2GraphicMenuMerging.oxt|python|SaveOLEAsGraphic.py$saveOLEAsPDF?language=Python&amp;location=user:uno_packages</value>
+                            </prop>
+                        </node>
                     </node>
                 </node>
             </node>

framework/workben/ctxtmnmerging/ole2graphic/SaveOLEAsGraphic.py

 
 from com.sun.star.beans import PropertyValue
 from com.sun.star.beans.PropertyState import DIRECT_VALUE
-from com.sun.star.ui.dialogs.TemplateDescription import FILESAVE_SIMPLE
+from com.sun.star.ui.dialogs.TemplateDescription import FILESAVE_AUTOEXTENSION
 from com.sun.star.ui.dialogs.ExecutableDialogResults import OK
+from com.sun.star.ui.dialogs.ExtendedFilePickerElementIds import CHECKBOX_AUTOEXTENSION
 
 def storeGraphic( graph, context ):
     print 'Storing graphic'
-    filepicker = context.getServiceManager().createInstanceWithContext('com.sun.star.ui.dialogs.FilePicker', context)
-    filepicker.initialize( ( FILESAVE_SIMPLE, ) )
+    filepicker = context.getServiceManager().createInstanceWithArgumentsAndContext('com.sun.star.ui.dialogs.FilePicker', ( FILESAVE_AUTOEXTENSION, ), context )
     filepicker.setTitle('Save OLE as graphic')
+    filepicker.setMultiSelectionMode( False )
+    filepicker.setValue( CHECKBOX_AUTOEXTENSION, 0, True )
+
+    sFilterName = "png_Portable_Network_Graphic"
+
+    oTypeDetection = context.getServiceManager().createInstanceWithContext("com.sun.star.document.TypeDetection", context)
+
+    aFilter = None
+    sUIName = ""
+    sMimeType = ""
+    if oTypeDetection.hasByName(sFilterName):
+        aFilter = oTypeDetection.getByName(sFilterName)
+
+    if aFilter != None:
+        for aProp in aFilter:
+            if aProp.Name == "UIName":
+                sUIName = aProp.Value
+            elif aProp.Name == "MediaType":
+                sMimeType = aProp.Value
+
+    filepicker.appendFilter( sUIName, "*.png" )
+    filepicker.setCurrentFilter( sUIName )
 
     if ( filepicker.execute() == OK ):
         fileName = filepicker.Files[0]
         provider = context.getServiceManager().createInstanceWithContext('com.sun.star.graphic.GraphicProvider', context)
         MediaProperties =  (
                             PropertyValue( "URL", 0, fileName, DIRECT_VALUE ),
-                            PropertyValue( "MimeType", 0, "image/png", DIRECT_VALUE )
+                            PropertyValue( "MimeType", 0, sMimeType, DIRECT_VALUE )
                             )
         provider.storeGraphic( graph, MediaProperties )
 
             else:
                 exportDrawingChart( selection, ctx )
 
-g_exportedScripts = saveOLEAsGraphic,
+
+def getPDFFilterNameFromModel( oModel, context ):
+    #oModuleMngr = ctx.getServiceManager().createInstanceWithContext('com.sun.star.frame.ModuleManager', context)
+    #sModuleIdentifier = oModuleMngr.identify( oModel )
+    filterName = ""
+
+    if oModel.supportsService("com.sun.star.text.TextDocument"):
+        filterName = "writer_pdf_Export"
+    elif oModel.supportsService( "com.sun.star.sheet.SpreadsheetDocument" ):
+        filterName = "calc_pdf_Export"
+    elif oModel.supportsService( "com.sun.star.drawing.DrawingDocument" ):
+        filterName = "draw_pdf_Export"
+    elif oModel.supportsService( "com.sun.star.presentation.PresentationDocument" ):
+        filterName = "impress_pdf_Export"
+    elif oModel.supportsService("com.sun.star.text.WebDocument"):
+        filterName = "writer_web_pdf_Export"
+    elif oModel.supportsService("com.sun.star.text.GlobalDocument"):
+        filterName = "writer_globaldocument_pdf_Export"
+    elif oModel.supportsService( "com.sun.star.formulaFormulaProperties" ):
+        filterName = "math_pdf_Export"
+
+    return filterName
+
+def exportToPDF( aDoc, aSel, context ):
+    print 'Exporting to PDF'
+    filepicker = context.getServiceManager().createInstanceWithArgumentsAndContext('com.sun.star.ui.dialogs.FilePicker', ( FILESAVE_AUTOEXTENSION, ), context )
+    filepicker.setTitle('Save OLE as PDF')
+    filepicker.setMultiSelectionMode( False )
+    filepicker.setValue( CHECKBOX_AUTOEXTENSION, 0, True )
+
+    sFilterName = getPDFFilterNameFromModel( aDoc, context )
+
+    oFilterFactory = context.getServiceManager().createInstanceWithContext("com.sun.star.document.FilterFactory", context)
+
+    aFilter = None
+    sUIName = ""
+    if oFilterFactory.hasByName(sFilterName):
+        aFilter = oFilterFactory.getByName(sFilterName)
+
+    if aFilter != None:
+        for aProp in aFilter:
+            if aProp.Name == "UIName":
+                sUIName = aProp.Value
+                break
+
+    filepicker.appendFilter( sUIName, "*.pdf" )
+    filepicker.setCurrentFilter( sUIName )
+
+    if ( filepicker.execute() == OK ):
+        fileName = filepicker.Files[0]
+        print 'storing as ' + fileName
+
+        aFilterData = ( PropertyValue( "Selection", 0, aSel,  DIRECT_VALUE ), )
+        aMediaDescriptor = (
+                            PropertyValue( "FilterName", 0, sFilterName, DIRECT_VALUE ),
+                            PropertyValue( "FilterData", 0, uno.Any(  "[]com.sun.star.beans.PropertyValue",  aFilterData ), DIRECT_VALUE )
+                            )
+
+        aDoc.storeToURL( fileName, aMediaDescriptor )
+
+
+def saveOLEAsPDF():
+    """saves the selected OLE graphic as PDF"""
+    ctxt = uno.getComponentContext()
+
+    model = XSCRIPTCONTEXT.getDocument()
+    controller = model.getCurrentController()
+    if ( controller != None ):
+        selection = controller.getSelection()
+        if ( selection != None ):
+            services = selection.getSupportedServiceNames()
+            for service in services:
+                print service
+            exportToPDF( model, selection, ctxt )
+
+
+g_exportedScripts = saveOLEAsGraphic,saveOLEAsPDF