使用C#以编程方式将MS Word中的文本对齐的问题 [英] Problem with programmatically aligning text in MS Word with C#

查看:105
本文介绍了使用C#以编程方式将MS Word中的文本对齐的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using Microsoft.Office.Core;
using Microsoft.Office.Interop.Word;
using Word = Microsoft.Office.Interop.Word;
using System.Reflection;

object oMissing = System.Reflection.Missing.Value;
            object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
            
            //Start Word and create a new document.
            Word._Application oWord;
            Word._Document oDoc;
            oWord = new Word.Application();
            oWord.Visible = true;
            oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,
                ref oMissing, ref oMissing);





这是我遇到问题的地方:





This is where I run into the problem:

//Insert a paragraph at the beginning of the document.
            Word.Paragraph oPara1;
            oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);
            Word.Range rng = oPara1.Range;
            rng.Font.Size = 14;
            rng.Font.Name = "Arial";
            rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            oPara1.Range.Font.Bold = 1;
            oPara1.Range.Text = "SOCIAL ASSESSMENT";
            oPara1.Format.SpaceAfter = 24;    //24 pt spacing after paragraph.
            oPara1.Range.InsertParagraphAfter();
            
            
            //Insert another paragraph.
            object oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            Word.Paragraph oPara2;
            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara2 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara2.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
            oPara2.Range.Font.Underline = Word.WdUnderline.wdUnderlineSingle;
            oPara2.Range.Font.Bold = 1;
            oPara2.Range.Text = "Request for Services and Identifying Information";
            oPara2.Format.SpaceAfter = 24;
            oPara2.Range.InsertParagraphAfter();

            //Insert another paragraph.
            Word.Paragraph oPara3;
            oRng = oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range;
            oPara3 = oDoc.Content.Paragraphs.Add(ref oRng);
            oPara3.Range.Font.Bold = 0;
            oPara3.Range.Text = "Applicants Name: " + textBox1.Text + " " + textBox7.Text + "\r\n" + "Referred by: "
                + textBox2.Text + "\r\n" + "Request for Evaluation by: " + textBox3.Text + "\r\n" +
                "Reason for Referral: " + textBox4.Text + "\r\n" + "Service Request: " + textBox5.Text
                + "\r\n" + "Clients Primary Language: " + textBox6.Text + "\r\n" +
                "Language of Informant/Advocate: " + textBox8.Text;
            oPara3.Format.SpaceAfter = 24;
            oPara3.Range.InsertParagraphAfter();





整个文档最终都是左对齐的。我该如何解决这个问题?



谢谢,

Vlad



The entire document ends up being left-aligned. How can I fix this?

Thank you,
Vlad

推荐答案

我自己能够解决这个问题。我必须准确指定要对齐的范围:



I was able to solve this myself. I had to specify exactly which range to align:

Word.Range rng = oPara1.Range;
rng.Font.Size = 14;
rng.Font.Name = "Arial";
rng.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;





现在左对齐下一段:





And now left align the next paragraph:

Word.Range rng2 = oPara2.Range;
rng2.Font.Size = 12;
rng2.Font.Name = "Arial";
rng2.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;







这是第一行文本的中心,而左对齐其余的:)




This centers the first line of text, while left aligning the rest :)


我知道这是一个古老而封闭的问题,但这里提到的解决方案对我没有帮助。

所以我会在这里发布解决方案,如ppl一样我希望它能帮助他们。



你需要做的是在Text属性之后设置Alignment属性(参见下面的代码)。



I know this is old and closed question, but the solution mentioned here didn't help me.
So i'll post the solution here for ppl like me in hope it will help them.

The thing you need to do is set the Alignment property AFTER the Text property (see the code below).

// p1 is going to be right aligned
var p1 = document.Paragraphs.Add(System.Reflection.Missing.Value);
p1.Range.Font.Name = "Calibri";
p1.Range.Font.Size = 18;
p1.Range.Text = "right";
p1.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
p1.Range.InsertParagraphAfter();
// p2 is going to be center aligned
var p2 = document.Paragraphs.Add(System.Reflection.Missing.Value);
p2.Range.Font.Name = "Calibri";
p2.Range.Font.Size = 16;
p2.Range.Text = "center";
p2.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
p2.Range.InsertParagraphAfter();
// p3 is going to be left aligned
var p3 = document.Paragraphs.Add(System.Reflection.Missing.Value);
p3.Range.Font.Name = "Calibri";
p3.Range.Font.Size = 14;
p3.Range.Text = "left";
p3.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft;
p3.Range.InsertParagraphAfter();





这就是它对我有用的方式。



This is the way it worked for me.


这篇关于使用C#以编程方式将MS Word中的文本对齐的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆