UNO: set default font in LibreOffice Writer

Hello!

When editing font options locally (Tools → Options → LibreOffice Writer → Basic Fonts (Western)) my opened document changes, but when I’m doing it via UNO nothing happens and fonts are not applied. What am I doing wrong?

Here’s listing:

      private void setTimesNewRomanByDefault()
        throws com.sun.star.uno.Exception {
      XMultiServiceFactory configProvider = UnoRuntime.queryInterface(XMultiServiceFactory.class,
            _context.getServiceManager().createInstanceWithContext("com.sun.star.configuration.ConfigurationProvider", _context));

      PropertyValue nodePath = new PropertyValue();
      nodePath.Name = "nodepath";
      nodePath.Value = "/org.openoffice.Office.Writer/DefaultFont";

      Object updater = configProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationUpdateAccess",
            new PropertyValue[] { nodePath });

      XNameReplace replacer = UnoRuntime.queryInterface(XNameReplace.class, updater);
      replacer.replaceByName("Document", false); // I tried without this one, didn't work either
      for (String name : new String[] { "List", "Index", "Caption", "Heading", "Standard" }) {
          replacer.replaceByName(name, "Times New Roman");
      }
      UnoRuntime.queryInterface(XChangesBatch.class, updater).commitChanges();
      UnoRuntime.queryInterface(XComponent.class, updater).dispose();
      }

Hello @Contego,

The following StarBasic code runs fine on my LibreOffice version 5.4.1.2:

Sub setBasicFonts_Western( Optional strFontName As String, Optional lFontHeight As Long )
REM Sets all categories of the Basic Fonts (Western) to the specified Font and Size.
REM Requires restarting LibreOffice for the changes to be visible in "Tools : Options... : LibreOffice Writer : Basic Fonts (Western)".
	If IsMissing( strFontName ) Or strFontName   = "" Then strFontName = "Times New Roman"
	If IsMissing( lFontHeight ) Then lFontHeight = 12 * 35.28	REM 12 points specified in mm/100

	Dim aProps(1)   As New com.sun.star.beans.PropertyValue
	aProps(0).Name	= "nodepath"
	aProps(0).Value	= "/org.openoffice.Office.Writer/DefaultFont/"
	aProps(1).Name	= "enableasync"
	aProps(1).Value	= False
	
	Dim oConfig As Object, oDefFont As Object
	oConfig		= createUnoService( "com.sun.star.configuration.ConfigurationProvider" )
	oDefFont	= oConfig.createInstanceWithArguments( "com.sun.star.configuration.ConfigurationUpdateAccess", aProps() )

	Dim strCategory as String
	For Each strCategory in Array( "Standard", "Heading", "List", "Caption", "Index" )
		oDefFont.replaceByName( strCategory, strFontName )
		oDefFont.replaceByName( strCategory & "Height", lFontHeight )
	Next
	
	oDefFont.commitChanges()
	oConfig.flush()
End Sub

i suggest adding the following elements to your code:

  1. the final slash in the nodepath;
  2. the enableasync prop;
  3. flush the configurationprovider;
  4. restart LibreOffice.

HTH, lib