Formatting a complex field

Questions and answers on how to use DOCXReadWrite
Post Reply
cniccolo
Posts: 5
Joined: Tue Nov 09, 2021 2:39 pm

Formatting a complex field

Post by cniccolo »

Dear Lars

I am trying to add a complex field with the page number. The code below produces the xml below. It formats the default value (xxxx) correctly but once the page number gets refreshed it uses the default style. If I add the font definition manually to the block "<w:instrText xml:space="preserve"> PAGE </w:instrText>" the page number is displayed correctly. It seems to me that the docx writer suppresses the formats for instrText. Is there a way to work around?

Code: Select all

  
  para := DOCX.Editor.Paras.AppendPara;
  para.AppendComplexField('xxxx','PAGE');

  for I := para.Runs.First.Index to para.Runs.Last.Index do
  begin
    CR := para.Runs[I];
    format2 := CR.AddCHPX;
    format2.Size := 8;
    format2.FontName := 'Arial';
    format2.CHP.hAnsi := 'Arial';
    format2.CHP.Ascii := 'Arial';
    format2.EastAsia := 'Arial';
  end;

Code: Select all

<w:p>
	<w:r>
		<w:fldChar w:fldCharType="begin"/>
	</w:r>
	<w:r>
		<w:instrText xml:space="preserve"> PAGE </w:instrText>
	</w:r>
	<w:r>
		<w:fldChar w:fldCharType="separate"/>
	</w:r>
	<w:r>
		<w:rPr>
			<w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:eastAsia="Arial" w:cs="Arial"/>
			<w:sz w:val="16"/>
		</w:rPr>
		<w:t>xxxx</w:t>
	</w:r>
	<w:r>
		<w:fldChar w:fldCharType="end"/>
	</w:r>
</w:p>

larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: Formatting a complex field

Post by larsa »

Hello

Yes format was not saved for instrText. I will post an update which fixes this.
Lars Arvidsson, Axolot Data
cniccolo
Posts: 5
Joined: Tue Nov 09, 2021 2:39 pm

Re: Formatting a complex field

Post by cniccolo »

Thanks very much
Post Reply