Word Interop中的C#行间距

时间:2018-02-13 14:57:50

标签: c# ms-word office-interop

我正在尝试创建一个word文档,其中行间距设置为“No Spacing”或单个间距。老实说,我不知道为什么这么难。

我尝试了所有这些例子但没有运气:

Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
para1.LineSpacingRule = WdLineSpacing.wdLineSpaceExactly ;
para1.LineSpacing = 1;
para1.Format.SpaceBefore = 5;
para1.Format.SpaceAfter = 0;
para1.Range.Font.Name = "Times New Roman";
para1.Range.Font.Size = 12;
para1.Range.Text = "Mr. John Doe" + Environment.NewLine + "Address" + Environment.NewLine + "Anytown, AnyCity  12345";

Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
para1.LineSpacingRule = WdLineSpacing.wdLineSpaceSingle ;
para1.Format.SpaceBefore = 5;
para1.Format.SpaceAfter = 0;
para1.Range.Font.Name = "Times New Roman";
para1.Range.Font.Size = 12;
para1.Range.Text = "Mr. John Doe" + Environment.NewLine + "Address" + Environment.NewLine + "Anytown, AnyCity  12345";

Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
para1.LineSpacingRule = <tried them all> ;
para1.Format.SpaceBefore = 5;
para1.Format.SpaceAfter = 0;
para1.Range.Font.Name = "Times New Roman";
para1.Range.Font.Size = 12;
para1.Range.Text = "Mr. John Doe\rAddress\rAnytown, AnyCity  12345";

Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
para1.LineSpacingRule = <tried them all> ;
para1.Format.SpaceBefore = 5;
para1.Format.SpaceAfter = 0;
para1.Range.Font.Name = "Times New Roman";
para1.Range.Font.Size = 12;
para1.Range.Text = "Mr. John Doe\rnAddress\rnAnytown, AnyCity  12345";

我显然错过了一些东西,因为每个例子都完全相同。地址的每一行之间有额外的间距。

任何人都知道如何使这项工作?

1 个答案:

答案 0 :(得分:0)

Danel A.怀特救援!

使用This question,答案是使用&#34;垂直标签&#34;。直到现在我才听说过它们。

所以,替换&#34; \ r \ n&#34;用&#34; \ v&#34;效果很好。

    Paragraph para1 = document.Content.Paragraphs.Add(ref missing);
para1.Range.Font.Name = "Times New Roman";
para1.Range.Font.Size = 12;
para1.Range.Text = "Mr. John Doe\vAddress\vAnytown, AnyCity  12345";