Ask Your Question

UNO: set default font in LibreOffice Writer

asked 2017-11-27 20:09:06 +0200

Contego gravatar image

updated 2017-11-27 20:10:57 +0200


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 {
      XMultiServiceFactory configProvider = UnoRuntime.queryInterface(XMultiServiceFactory.class,
            _context.getServiceManager().createInstanceWithContext("", _context));

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

      Object updater = configProvider.createInstanceWithArguments("",
            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();
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-11-28 03:35:53 +0200

librebel gravatar image

Hello @Contego,

The following StarBasic code runs fine on my LibreOffice version

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
    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( "" )
    oDefFont    = oConfig.createInstanceWithArguments( "", aProps() )

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

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

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-11-27 20:09:06 +0200

Seen: 144 times

Last updated: Nov 28 '17