使用NPOI操作word,在制作页眉页脚中有个特殊的需求,在页眉中插入图片,在网上查找许久,终于是有个可以实现在也没中插入图片,但是是以inline方式在页眉中,这种方式不能很好的满足图片位置的调整,需要修改源码,加上可以设置锚点的方式插入图片(还没有实现,实现了再来记录)
1,在页眉中插入图片所使用的NPOI版本是 https://download.csdn.net/download/dengxt/10824550
结果图如上:
2,非插入图片,页眉页脚都是文字
XWPFDocument m_Docx = new XWPFDocument();//创建文档
m_Docx.Document.body.sectPr = new CT_SectPr();
CT_SectPr m_SectPr = m_Docx.Document.body.sectPr;//创建页脚
CT_Ftr m_ftr = new CT_Ftr();m_ftr.AddNewP().AddNewR().AddNewT().Value = "我是页脚";
//创建页脚关系(footern.xml)
XWPFRelation Frelation = XWPFRelation.FOOTER;
XWPFFooter m_f = (XWPFFooter)m_Docx.CreateRelationship(Frelation, XWPFFactory.GetInstance(), m_Docx.FooterList.Count + 1);//设置页脚
m_f.SetHeaderFooter(m_ftr);
CT_HdrFtrRef m_HdrFtr1 = m_SectPr.AddNewFooterReference();m_HdrFtr1.type = ST_HdrFtr.@default;
m_HdrFtr1.id = m_f.GetPackageRelationship().Id;//创建页眉
CT_Hdr m_Hdr = new CT_Hdr();
m_Hdr.AddNewP().AddNewR().AddNewT().Value = "我是页眉";//页眉内容
//创建页眉关系(headern.xml)
XWPFRelation Hrelation = XWPFRelation.HEADER;
XWPFHeader m_h = (XWPFHeader)m_Docx.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), m_Docx.HeaderList.Count);
//设置页眉
m_h.SetHeaderFooter(m_Hdr);
CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewHeaderReference();
m_HdrFtr2.type = ST_HdrFtr.@default;
m_HdrFtr2.id = m_h.GetPackageRelationship().Id;//保存文件
string docPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "DocxWord");
if (!Directory.Exists(docPath)) { Directory.CreateDirectory(docPath); }
string fileName = string.Format("{0}.doc", DateTime.Now.ToString("yyyyMMddHHmmssfff"));
FileStream out1 = new FileStream(Path.Combine(docPath, fileName), FileMode.Create);
m_Docx.Write(out1);
out1.Close();
3,页眉插入图片,页脚文字
XWPFDocument m_Docx = new XWPFDocument();
m_Docx.Document.body.sectPr = new CT_SectPr();CT_SectPr m_SectPr = m_Docx.Document.body.sectPr;//创建页脚
CT_Ftr m_ftr = new CT_Ftr();
m_ftr.AddNewP().AddNewR().AddNewT().Value = "im yejiao";//页脚内容//创建页脚关系(footern.xml)
XWPFRelation Frelation = XWPFRelation.FOOTER;
XWPFFooter m_f = (XWPFFooter)m_Docx.CreateRelationship(Frelation, XWPFFactory.GetInstance(), m_Docx.FooterList.Count + 1);//设置页脚
m_f.SetHeaderFooter(m_ftr);
CT_HdrFtrRef m_HdrFtr1 = m_SectPr.AddNewFooterReference();
m_HdrFtr1.type = ST_HdrFtr.@default;
m_HdrFtr1.id = m_f.GetPackageRelationship().Id;//创建页眉
CT_Hdr m_Hdr = new CT_Hdr();
// m_Hdr.AddNewP().AddNewR().AddNewT().Value = "im yemei";//页眉内容
//创建页眉关系(headern.xml)
XWPFRelation Hrelation = XWPFRelation.HEADER;
XWPFHeader m_h = (XWPFHeader)m_Docx.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), m_Docx.HeaderList.Count);
//设置页眉
m_h.SetHeaderFooter(m_Hdr);
CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewHeaderReference();
m_HdrFtr2.type = ST_HdrFtr.@default;
m_HdrFtr2.id = m_h.GetPackageRelationship().Id;m_Docx.Document.body.sectPr = new CT_SectPr();
m_SectPr = m_Docx.Document.body.sectPr;
//页面设置A4横向
//m_SectPr.pgSz.w = (ulong)16838;
//m_SectPr.pgSz.h = (ulong)11906;// Add the picture + relationship
FileStream pictureData = null;
pictureData = new FileStream("../../image/3.jpeg", FileMode.Open, FileAccess.Read);//设置页眉
m_h.SetHeaderFooter(m_Hdr);
m_h.AddPicture(pictureData, (int)PictureType.JPEG, "3.jpeg", 7000000, 1000000);
CT_HdrFtrRef m_HdrFtr = m_SectPr.AddNewHeaderReference();
m_HdrFtr.type = ST_HdrFtr.@default;
m_HdrFtr.id = m_h.GetPackageRelationship().Id;//保存文件到磁盘WinForm
string docPath = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "DocxWord");
if (!Directory.Exists(docPath)) { Directory.CreateDirectory(docPath); }
string fileName = string.Format("{0}.doc", DateTime.Now.ToString("yyyyMMddHHmmssfff"));
FileStream out1 = new FileStream(Path.Combine(docPath, fileName), FileMode.Create);
m_Docx.Write(out1);
out1.Close();
收集的使用NPOI操作word的文档,还是比较适用的文档 https://download.csdn.net/download/dengxt/10823924