Npio word文件创建和段落的各种设置
用代码创建一个word文件,下面代码主要说明创建一个word 文件后,页面大小设置、页面边距、段落之间距离
XWPFDocument doc = new XWPFDocument();
//定义页面大小
doc.Document.body.sectPr = new CT_SectPr();
CT_SectPr m_SectPr = doc.Document.body.sectPr;
m_SectPr.pgSz.h = (ulong)8618;
m_SectPr.pgSz.w = (ulong)5897;
//页面边距
m_SectPr.pgMar.left = (ulong)357;//左边距
m_SectPr.pgMar.right = (ulong)454;//右边距
m_SectPr.pgMar.top = "238";//上边距
m_SectPr.pgMar.bottom = "471";//下边距
//创建一个段落
var paragraph = doc.CreateParagraph();
//控制段落与其他元素的上下距离(段落前后距离)
paragraph.SpacingBeforeLines = 10;//上方距离0.1
paragraph.SpacingAfterLines = 10;//下方距离0.1
paragraph.Alignment = ParagraphAlignment.LEFT; //字体居中
var runHead = paragraph.CreateRun();
runHead.IsBold = false;//是否加粗
runHead.FontSize = 6;//字体大小
runHead.CharacterSpacing = -34;//字符间距(负数时紧缩、正数是加宽)
runHead.SetFontFamily("黑体", FontCharRange.None); //设置黑体
runHead.AppendText("张三");//添加文字
runHead.AddCarriageReturn();//换行
//保存
FileStream output = new FileStream(SavePath, FileMode.Create);
doc.Write(output);