Bug 66405 - FILEOPEN: Extra spaces around a formula added when importing a Word-document
Summary: FILEOPEN: Extra spaces around a formula added when importing a Word-document
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: Writer (show other bugs)
(earliest affected)
3.5.0 release
Hardware: All All
: medium minor
Assignee: Mike Kaganski
Whiteboard: BSA target:5.4.0
Keywords: filter:docx
Depends on:
Blocks: DOCX-Formula
  Show dependency treegraph
Reported: 2013-06-30 15:49 UTC by Sergey Pashinin
Modified: 2017-03-01 16:25 UTC (History)
5 users (show)

See Also:
Crash report or crash signature:

word document for testing (13.53 KB, application/vnd.openxmlformats-officedocument.wordprocessingml.document)
2013-06-30 15:49 UTC, Sergey Pashinin
Look quickly what is the problem (97.37 KB, image/jpeg)
2013-06-30 15:51 UTC, Sergey Pashinin

Note You need to log in before you can comment on or make changes to this bug.
Description Sergey Pashinin 2013-06-30 15:49:39 UTC
Created attachment 81749 [details]
word document for testing

Problem description: 
When you open a word-document with a formula - LO adds spaces (to sides and bottom usually) to the formula object.

It shifts all the lines a little bit down and breaks the paragraph style.

Steps to reproduce:
1. Open test-1.docx in MS Word
2. Open test-1.docx in LO
3. See that the formula object has spaces it shouldn't have

Current behavior:

Adds spaces to formula object when importing.

Expected behavior:

No spaces should be added
Operating System: Windows 7
Version: release
Comment 1 Sergey Pashinin 2013-06-30 15:51:18 UTC
Created attachment 81750 [details]
Look quickly what is the problem
Comment 2 Cor Nouws 2013-06-30 18:09:49 UTC
Hi Sergey,
Thank you for the report and screen print.
This looks (partly) the same as bug 64989 . Could you pls check if that is the case?
Comment 3 Sergey Pashinin 2013-06-30 19:09:03 UTC
(In reply to comment #2)
> This looks (partly) the same as bug 64989 . Could you pls check if that is
> the case?

It looks a bit different for me:

1. mine - about spacing around a formula object; 64989  - about adding new lines
2. mine - importing ".doc"; 64989 - saving .odt to .doc

Well, I can't tell if the same code is responsible for these 2 bugs.
Comment 4 Jorendc 2013-07-01 12:17:13 UTC
Thanks for your bug reporter and answer Sergey. I can reproduce this behavior using Mac OSX 10.8.4 with LibreOffice RC.

Kind regards,
Comment 5 Mike Kaganski 2013-07-02 13:41:54 UTC
This is Bug 65067. See also discussion in its dup Bug 65169.
Comment 6 Sergey Pashinin 2013-07-02 20:21:50 UTC
(In reply to comment #5)
> This is Bug 65067. See also discussion in its dup Bug 65169.

But not only horizonal! There is also a space at the bottom.
Comment 7 Mike Kaganski 2013-07-02 21:49:40 UTC
(In reply to comment #6)

My comment was intended not to question someone's priority and importance. It was mainly targeted towards developers who decide to fix this. Any bug report goal is primarily to get fixed! And I provide information with regard of relevant discussion about impact and related problems. Besides, dupes show importance of a problem - they inform developers, that a lot of users suffer from it. Still, anyway, I haven't marked your report as dup, as you see, and I simply add the abovementioned related bug to See Also.
Comment 8 Sergey Pashinin 2013-07-02 22:17:47 UTC
(In reply to comment #7)

Don't worry, wouldn't be a problem if it was even marked as a dup.
I agree with you. I just made a note - "not only horizonal".
Comment 9 Mike Kaganski 2013-07-06 12:57:45 UTC
This comment by no means doesn't pretend to be a "solution" to this bug; rather, it is a kludge for those who need to have their work done while this bug is being fixed.

You may use the following macro to remove all spacing around all formulas in a document at once:

Sub SetMathBorder
  On Error Resume Next ' Prevents deleted objects from causing errors.
  oCurrentController = ThisComponent.getCurrentController()
  oTextDocument = oCurrentController.Model
  oEmbeddedObjects = oTextDocument.EmbeddedObjects
  nEndIndex = oEmbeddedObjects.Count-1
  for nIndex=0 to nEndIndex
    oMathObject = oEmbeddedObjects.getByIndex(nIndex)
    oModel = oMathObject.Model
    if (not isNull(oModel)) then
      if(not isEmpty(oModel)) then
'        if oModel.supportsService("com.sun.star.formula.FormulaProperties") then
        if oMathObject.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997" then
          oMathObject.LeftMargin = 0
          oMathObject.RightMargin = 0
          oMathObject.TopMargin = 0
          oMathObject.BottomMargin = 0

          oModel.LeftMargin = 0
          oModel.RightMargin = 0
          oModel.TopMargin = 0
          oModel.BottomMargin = 0
          ' Update
          oXCOEO = oMathObject.ExtendedControlOverEmbeddedObject
        endif ' if formula
      endif ' if not empty
    endif ' if not null
  next nIndex
  ThisComponent.Reformat() ' Updates all elements in the document
End Sub

The macro removes both spacing accessible by Object->Wrap, and menu Format->Spacing->Category->Borders inside formula. Also, this will make formulas to "regenerate", i.e. stop showing cached image (this may be considered good or bad, depending on the correctness of formulas import).

Thanks to: LO Documentation team and their LibreOffice Math Guide & http://api.libreoffice.org;
AOO Wiki @ http://wiki.openoffice.org/wiki/Documentation/FAQ/Formula/How_do_I_change_the_spacing_around_my_formula%3f.
Comment 10 Sergey Pashinin 2013-07-08 16:40:31 UTC
(In reply to comment #9)

Great! I think this code might help someone even after the bug is fixed.
Comment 11 Mike Kaganski 2013-07-08 23:06:28 UTC
While I stated that this is similar to Bug 65067, this is quite different, though has much in common: this one is about wrong positioning of objects imported from another format (thus, distorting the document, making it look different than the original), while the mentioned issue is about defaults used to create new formulas in LO. This one should not be fixed by changing some defaults in a program or template; rather, the styles in the imported document (or even properties of individual objects, if they are different in different formulas in the original) should be set appropriately.

The import code seems to simply skip the spacing properties of formula objects, so it may be a (DOC(X) and RTF) filter issue? Miklos, could you please take a look at this, and maybe advise on who should be notified on this? Thank you.

And below is a slightly fixed version of the Basic macro posted above. The former code has one problem: if the document contain MathType objects not converted to Math, and MathType is installed, it will complain that "Too many windows are open". This is caused by string

oModel = oMathObject.Model

that, although returns null for non-LO objects, seems to trigger creating a (hidden?) window by MathType. (By the way, is there a way to force free the resources allocated by this?)

The new version avoids this problem by checking GUID before dereferencing Model. Thus, the alternative method of testing model for support of service com.sun.star.formula.FormulaProperties is impossible here.

Sub SetMathBorder
  On Error Resume Next ' Prevents deleted objects from causing errors.
  dim oEmbObject as Object
  for each oEmbObject in ThisComponent.getEmbeddedObjects()
    if oEmbObject.CLSID = "078B7ABA-54FC-457F-8551-6147e776a997" then ' Formula!
      dim oModel
      oModel = oEmbObject.Model
      if (not isNull(oModel)) then
        if (not isEmpty(oModel)) then
          with oEmbObject
            .LeftMargin = 0
            .RightMargin = 0
            .TopMargin = 0
            .BottomMargin = 0
          end with
          with oModel
            .LeftMargin = 0
            .RightMargin = 0
            .TopMargin = 0
            .BottomMargin = 0
          end with
          ' Update
        end if ' if not empty
      end if ' if not null
    end if ' if formula
  next ' oMathObject
  ThisComponent.Reformat() ' Updates all elements in the document
End Sub
Comment 12 QA Administrators 2015-04-01 14:40:04 UTC Comment hidden (obsolete)
Comment 13 Buovjaga 2015-04-19 14:47:51 UTC
3.5 already has extra space around the formula, even though the formula is invisible.

Win 7 Pro 64-bit Version: (x64)
Build ID: 211c12b9c64facd1c12f637a5229bd6a6feb032a
TinderBox: Win-x86_64@42, Branch:master, Time: 2015-04-18_01:51:17
Locale: fi_FI

Ubuntu 14.10 64-bit
LibreOffice 3.5.0rc3 
Build ID: 7e68ba2-a744ebf-1f241b7-c506db1-7d53735
Comment 14 QA Administrators 2016-09-20 09:32:04 UTC Comment hidden (obsolete)
Comment 15 Mike Kaganski 2016-12-22 11:02:39 UTC
A patch sent for review: https://gerrit.libreoffice.org/32334
Comment 16 Commit Notification 2016-12-23 03:56:57 UTC
Mike Kaganski committed a patch related to this issue.
It has been pushed to "master":


tdf#66405: imported formulas should have all margins set to 0

It will be available in 5.4.0.

The patch should be included in the daily builds available at
http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:

Affected users are encouraged to test the fix and report feedback.
Comment 17 Xisco Faulí 2017-03-01 11:08:53 UTC
Hello Mike,
Is this bug fixed?
If so, could you please close it as RESOLVED FIXED?