如题,代码如下:
using System;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using Microsoft.Office.Core;
namespace WindowsFormsApp1654
{
public class WordTool
{
#region 公文内容格式设置
/// <summary>
/// 公文内容格式设置,传入Document对象
/// </summary>
/// <param name="doc">Document对象</param>
public void docFormatSetting(Word.Document doc)
{
/*
属性
Alignment 返回或设置一个 WdParagraphAlignment 常量,该常量代表指定段落的对齐方式。
AutoAdjustRightIndent 设置为自动调整指定段落的右缩进(如果您指定了每行的字符数)。
CharacterUnitFirstLineIndent 返回或设置首行或悬挂缩进的值 (以字符为单位)。 用正值设置首行缩进,并使用一个负值设置悬挂缩进。
CharacterUnitLeftIndent 返回或设置指定段落的左缩进值 (以字符为单位)。
CharacterUnitRightIndent 该属性返回或设置指定段落的右缩进量(以字符为单位)。
FirstLineIndent 返回或设置首行的行或悬挂缩进的值 (以磅为单位)。 用正数设置首行缩进的尺寸,用负数设置悬挂缩进的尺寸。
Hyphenation 如果指定段落的段包括在自动断字功能。 假 如果指定的段落不进行自动断字。 可以为 真 ,或者 wdUndefined 则 为 False 。
LineSpacing 返回或设置指定段落的行距 (以磅为单位)。
LineUnitAfter 返回或设置指定段落的段后间距 (以网格线)。
LineUnitBefore 返回或设置指定段落的段前间距 (以网格线) 的数量。
OutlineLevel 返回或设置指定段落的大纲级别。
SpaceAfter 返回或设置指定段落的段后间距(以磅为单位)的量。
SpaceBefore 返回或设置指定段落的段前间距 (以磅为单位)。
方法
CloseUp() 清除指定段落前的段落间距。
IndentFirstLineCharWidth(Int16) 将一个或多个段落的首行缩进指定的字符数。
Space15() 为指定段落设置 1.5 倍行距。 精确间距为各段内最大字符的字号加上 6 磅。
Space2() 为指定段落设置 2 倍行距。 精确间距为各段内最大字符的字号加上 12 磅。
*/
doc.Application.Selection.WholeStory();//全选内容
doc.Application.Selection.ClearFormatting();//清除格式
doc.Application.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphJustifyLow;//设置对齐方式为两端对齐
doc.Application.Selection.ParagraphFormat.LineSpacing = 28.9f;//设置行间距为28.9磅
doc.Application.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = 2;//首行缩进2字符
doc.Application.Selection.Font.Name = "仿宋_GB2312";//设置字体为"仿宋_GB2312"
doc.Application.Selection.Font.Size =16;//设置字体为3号
}
#endregion
#region 版面设置
/// <summary>
/// 文档版面设置
/// </summary>
/// <param name="doc">document对象</param>
public void docPageSetup(Word.Document doc)
{
doc.PageSetup.TopMargin = doc.Application.CentimetersToPoints(float.Parse("3.8"));//上页边距
doc.PageSetup.BottomMargin = doc.Application.CentimetersToPoints(float.Parse("3.6"));//下页边距
doc.PageSetup.LeftMargin = doc.Application.CentimetersToPoints(float.Parse("2.6"));//左页边距
doc.PageSetup.RightMargin = doc.Application.CentimetersToPoints(float.Parse("2.6"));//右页边距
}
#endregion
#region 图片设置
/// <summary>
/// 设置图片大小和对齐方式
/// </summary>
/// <param name="doc">传入document对象</param>
/// <param name="h">要设置的图片高,单位为厘米</param>
/// <param name="w">要设置的图片宽,单位为厘米</param>
public void pictureSet(Word.Document doc,float h,float w)
{
foreach (Word.Shape shape in doc.Shapes)
{
Word.InlineShape inlineshape = shape.ConvertToInlineShape();
}
foreach (Word.InlineShape inlineshape in doc.InlineShapes)
{
inlineshape.LockAspectRatio =Microsoft.Office.Core.MsoTriState.msoFalse;//改变大小时不保持纵横比
inlineshape.Height = doc.Application.CentimetersToPoints(h);
inlineshape.Width = doc.Application.CentimetersToPoints(w);
inlineshape.Select();
doc.Application.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;//设置图片居中对其
}
}
#endregion
}
}
参考:
https://www.cnblogs.com/arxive/p/7426588.html;
https://www.cnblogs.com/xh6300/p/5915717.html;
https://www.cnblogs.com/tianma3798/p/3558140.html
https://blog.csdn.net/lu930124/article/details/84743752
https://www.cnblogs.com/tianma3798/p/3558140.html
https://www.cnblogs.com/xh6300/p/5915717.html
https://www.php.cn/csharp-article-395632.html