3

I have a paragraph para1 and a run, before remove the run, I first keep the run font by

Font originFont = run1.getFont();

and I check debug mode, the originFont.getName() return Times New Roman. but then I remove the run by

para1.removeChild(run1);

then I check the originFont.getName(), it change to Calibri, why?

enter image description here

enter image description here

user1169587
  • 1,104
  • 2
  • 17
  • 34

1 Answers1

0

Font of the run is not specified directly in run only. This is complex value that is inherited from the run’s parent, document styles, themes and document defaults. When you remove run from the document it’s parent is set to null and the code that gets font name cannot get the inherited values and returns value that is directly specified in the run or if it is not directly specified returns default value.

Let’s consider the following three paragraphs. The first is simple bold text with Normal style that uses Calibri font. The second is paragraph with the same normal style, but with directly specified font for run and paragraph. The third is paragraph with a custom ArialStyle style, that has Arial style specified.

    <w:p w14:paraId="2787B8CC" w14:textId="607BD13B" w:rsidR="00C96F00" w:rsidRPr="00CA1A63" w:rsidRDefault="00CA1A63">
  <w:pPr>
    <w:rPr>
      <w:b />
      <w:bCs />
    </w:rPr>
  </w:pPr>
  <w:r w:rsidRPr="00CA1A63">
    <w:rPr>
      <w:b />
      <w:bCs />
    </w:rPr>
    <w:t>Bold Calibri</w:t>
  </w:r>
</w:p>
<w:p w14:paraId="7F64B104" w14:textId="7CCF8DB9" w:rsidR="00CA1A63" w:rsidRPr="00C75FFA" w:rsidRDefault="00CA1A63">
  <w:pPr>
    <w:rPr>
      <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman" />
      <w:i />
      <w:iCs />
    </w:rPr>
  </w:pPr>
  <w:r w:rsidRPr="00C75FFA">
    <w:rPr>
      <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman" w:cs="Times New Roman" />
      <w:i />
      <w:iCs />
    </w:rPr>
    <w:t>Italic Times New Roman</w:t>
  </w:r>
</w:p>
<w:p w14:paraId="0D91CAFA" w14:textId="21A94755" w:rsidR="00CA1A63" w:rsidRDefault="00CA1A63" w:rsidP="007A778A">
  <w:pPr>
    <w:pStyle w:val="ArialStyle" />
  </w:pPr>
  <w:r>
    <w:t xml:space="preserve">Normal arial style</w:t>
  </w:r>
</w:p>

If you remove the run from the first paragraph its font will not be changed because document default is Calibri.

If you remove the run from the second paragraph the font will not be changed because font name is directly specified.

If you remove the run from the third paragraph the font will be changed to Calibri – document default. Because when you remove a run it’s parent is null and code does not have access to the parent paragraph’s style.

Alexey Noskov
  • 1,722
  • 1
  • 7
  • 13
  • If run has its font, then remove run parent should have no change to run.getFont()? – user1169587 Mar 05 '20 at 12:18
  • one more question, if I want to change the text of a run but keep its font properties, any method? I try to delete the old run and add a new run, assign the new text, and keep the origin run font by originRun.getFont(), but now this method is not work. – user1169587 Mar 06 '20 at 02:47
  • If the font is specified directly in the run even if you remove it the font will be returned correctly. – Alexey Noskov Mar 06 '20 at 12:15
  • To change the text of the existing run you can simply use Run.setText(). But you should not that MS Word in most cases splits the text of paragraphs into multiple runs, even if these runs have the same formatting. You can decrease number of neighboring runs with the same formatting if call Document.joinRunsWithSameFormatting method. – Alexey Noskov Mar 06 '20 at 12:16