有哪位大侠操作过NPOI生成word文档,如何设置页眉页脚距离顶部和底部距离?

#region 1.创建文档(页眉、页脚)
XWPFDocument doc = new XWPFDocument();
//页面设置 A4:w=11906 h=16838
doc.Document.body.sectPr = new CT_SectPr();
CT_SectPr m_SectPr = doc.Document.body.sectPr;
m_SectPr.pgSz.h = (ulong)16838;
m_SectPr.pgSz.w = (ulong)11906;
m_SectPr.pgMar.left = (ulong)1000;//左边距
m_SectPr.pgMar.right = (ulong)1000;//右边距
m_SectPr.pgMar.top = "150";//上边距
m_SectPr.pgMar.bottom = "150";

#region =页眉=
CT_Hdr m_hdr = new CT_Hdr();
m_hdr.AddNewP().AddNewPPr().AddNewSpacing().lineRule = "-600";
//创建页眉关系
XWPFRelation Hrelation = XWPFRelation.HEADER;
XWPFHeader m_h = (XWPFHeader)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), doc.HeaderList.Count);
//读取图片数据
FileStream picData_ym = null;
string filepath_ym = System.Windows.Forms.Application.StartupPath + @"/Images/ym.png";
picData_ym = new FileStream(filepath_ym, FileMode.Open, FileAccess.Read);
//设置页眉
m_h.SetHeaderFooter(m_hdr);
//页眉插入图片(inline)单位:EMUS,1CM=360000EMUS
m_h.AddPicture(picData_ym, (int)PictureType.PNG, "ym.png", 6200000, 200000);

CT_HdrFtrRef m_HdrFtr = m_SectPr.AddNewHeaderReference();
m_HdrFtr.type = ST_HdrFtr.@default;
m_HdrFtr.id = m_h.GetPackageRelationship().Id;
picData_ym.Close();
#endregion

#region =页脚=
//读取图片数据
FileStream picData_yj = null;
string filepath_yj = System.Windows.Forms.Application.StartupPath + @"/Images/yj.png";
picData_yj = new FileStream(filepath_yj, FileMode.Open, FileAccess.Read);
//设置页脚
CT_Ftr m_ftr = new CT_Ftr();
//m_ftr.AddNewP().AddNewR().AddNewT().Value = "fff";//页脚内容

//创建页脚关系(footern.xml)
XWPFRelation Frelation = XWPFRelation.FOOTER;
XWPFFooter m_f = (XWPFFooter)doc.CreateRelationship(Frelation, XWPFFactory.GetInstance(), doc.FooterList.Count + 1);
//设置页脚
m_f.SetHeaderFooter(m_ftr);
//页脚插入图片
m_f.AddPicture(picData_yj, (int)PictureType.PNG, "yj.png", 6200000, 200000);
CT_HdrFtrRef m_HdrFtr1 = m_SectPr.AddNewFooterReference();
m_HdrFtr1.type = ST_HdrFtr.@default;
m_HdrFtr1.id = m_f.GetPackageRelationship().Id;
picData_yj.Close();
#endregion

#endregion

posted @ 2018-05-10 17:41  ogre_zl  阅读(2288)  评论(0编辑  收藏  举报