赞
踩
本文章中NPOI版本为2.5.3.0。
- void main()
- {
- XWPFDocument document = new XWPFDocument();
- document.Document.body.sectPr = new CT_SectPr();
- var sectPr = document.Document.body.sectPr;
- SetPageSz(sectPr.pgSz,"A4", ST_PageOrientation.portrait);//A4纵向
- SetPageMar(sectPr.pgMar, "Medium");//中等边框
- }
-
- void SetPageSz(CT_PageSz size, string pageSz, ST_PageOrientation orientation)
- {
- //这里的单位是缇,1英寸=1440缇,1英寸=2.54cm,1cm=1440÷2.54≈567缇
- //A4纸的物理尺寸是210×297mm,
- //210mm=210×144÷2.54≈11905.51181102362
- //297mm=297×144÷2.54≈16837.79527559055
- size.orient = orientation;
- switch (pageSz)//默认A4
- {
- case "A3": size.w = 16838; size.h = 23811; break;//297x420mm
- case "A4": size.w = 11906; size.h = 16838; break;//210x297mm
- case "A5": size.w = 8391; size.h = 11906; break;//148x210mm
- }
- if (orientation != ST_PageOrientation.portrait)
- {
- (size.h, size.w) = (size.w, size.h);
- }
- }
-
- void SetPageMar(CT_PageMar margin, string pageMar)
- {
- switch (type)
- {
- case "Normal": m.left = m.right = 1800; m.top = m.bottom = "1440"; break;
- case "Narrow": m.left = m.right = 720; m.top = m.bottom = "720"; break;
- case "Medium": m.left = m.right = 1080; m.top = m.bottom = "1440"; break;
- case "Broad": m.left = m.right = 2880; m.top = m.bottom = "1440"; break;
- case "Symmetrical": m.left = 1800; m.right = 1440; m.top = m.bottom = "1440"; break;
- }
- m.header = 851;//页面设置-布局-页眉和页脚-页眉
- m.footer = 992;//页面设置-布局-页眉和页脚-页脚
- m.gutter = 0;//页面设置-页边距-页边距-装订线
- }
-
-
- //初号 42,小初 36
- //一号 26,小一 24
- //二号 22,小二 18
- //三号 16,小三 15
- //四号 14,小四 12
- //五号 10.5,小五9
- //六号 7.5,小六6.5
- //七号 5.5,八号 5
-
- XWPFDocument document = new XWPFDocument();
- var paragraph = document.CreateParagraph();
- var run = paragraph.CreateRun();
- run.IsItalic = true;
- run.IsBold = true;
- run.FontSize = 12;//小四
- run.FontFamily = “Arial”;
- run.SetColor(“#FFFFFF”);
- run.SetText(“Email:”);
- run.SetUnderline(UnderlinePatterns.Single);
-
- //FontSize这破玩意儿是int类型,如果要想使用五号、六号、小六、七号字体则必须用下面的办法
- decimal size = 10.5;
- if (size== decimal.Truncate(size))
- run.FontSize = (int)size;
- else
- {
- var ctr = run.GetCTR();
- CT_RPr cT_RPr = ctr.IsSetRPr() ? ctr.rPr : ctr.AddNewRPr();
- (cT_RPr.IsSetSz() ? cT_RPr.sz : cT_RPr.AddNewSz()).val = (ulong)(size * 2uL);
- }
- FileStream fs = new FileStream("1.docx", FileMode.Create);
- document.Write(fs);
- fs.Close();
- public static class WordColors
- {
- //标准色
- public const string DarkRed = "#C00000";
- public const string Red = "#FF0000";
- public const string Orange = "#FFC000";
- public const string Yellow = "#FFFF00";
- public const string LightGreen = "#90D050";
- public const string Green = "#00B050";
- public const string LightBlue = "#00B0F0";
- public const string Blue = "#0070C0";
- public const string DarkBlue = "#002060";
- public const string Purple = "#7030A0";
-
- //主题色-白色
- public const string Back1 = "#FFFFFF";
- public const string Back1Sub1 = "#F2F2F2";
- public const string Back1Sub2 = "#D9D9D9";
- public const string Back1Sub3 = "#BFBFBF";
- public const string Back1Sub4 = "#A6A6A6";
- public const string Back1Sub5 = "#808080";
-
- //主题色-黑色
- public const string Text1 = "#000000";
- public const string Text1Sub1 = "#7F7F7F";
- public const string Text1Sub2 = "#595959";
- public const string Text1Sub3 = "#404040";
- public const string Text1Sub4 = "#262626";
- public const string Text1Sub5 = "#0D0D0D";
-
- //主题色-浅灰
- public const string Back2 = "#E7E6E6";
- public const string Back2Sub1 = "#D0CECE";
- public const string Back2Sub2 = "#AEAAAA";
- public const string Back2Sub3 = "#767171";
- public const string Back2Sub4 = "#3B3838";
- public const string Back2Sub5 = "#171717";
-
- //主题色-蓝灰
- public const string Text2 = "#44546A";
- public const string Text2Sub1 = "#D5DCE4";
- public const string Text2Sub2 = "#ACB9CA";
- public const string Text2Sub3 = "#B496B0";
- public const string Text2Sub4 = "#323E4F";
- public const string Text2Sub5 = "#222A35";
-
- //主题色-蓝色
- public const string Accent1 = "#4472C4";
- public const string Accent1Sub1 = "#D9E2F3";
- public const string Accent1Sub2 = "#B4C6E7";
- public const string Accent1Sub3 = "#8EAADB";
- public const string Accent1Sub4 = "#2F5496";
- public const string Accent1Sub5 = "#1F3864";
-
- //主题色-橙色
- public const string Accent2 = "#ED7D31";
- public const string Accent2Sub1 = "#FBE4D5";
- public const string Accent2Sub2 = "#F7CAAC";
- public const string Accent2Sub3 = "#F4B083";
- public const string Accent2Sub4 = "#C45911";
- public const string Accent2Sub5 = "#833C0B";
-
- //主题色-灰色
- public const string Accent3 = "#A5A5A5";
- public const string Accent3Sub1 = "#EDEDED";
- public const string Accent3Sub2 = "#DBDBDB";
- public const string Accent3Sub3 = "#C9C9C9";
- public const string Accent3Sub4 = "#7B7B7B";
- public const string Accent3Sub5 = "#525252";
-
- //主题色-金色
- public const string Accent4 = "#FFC000";
- public const string Accent4Sub1 = "#FFF2CC";
- public const string Accent4Sub2 = "#FFE599";
- public const string Accent4Sub3 = "#FFD955";
- public const string Accent4Sub4 = "#BF8F00";
- public const string Accent4Sub5 = "#806000";
-
- //主题色-蓝色
- public const string Accent5 = "#5B9BD5";
- public const string Accent5Sub1 = "#DEEAF6";
- public const string Accent5Sub2 = "#BDD6EE";
- public const string Accent5Sub3 = "#9CC2E5";
- public const string Accent5Sub4 = "#2E74B5";
- public const string Accent5Sub5 = "#1F4E79";
-
- //主题色-绿色
- public const string Accent6 = "#70AD47";
- public const string Accent6Sub1 = "#E2EFD9";
- public const string Accent6Sub2 = "#C5E0B3";
- public const string Accent6Sub3 = "#A8D08D";
- public const string Accent6Sub4 = "#538135";
- public const string Accent6Sub5 = "#385623";
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。