private static void CreateFooter(XWPFDocument doc, string headerText, string imageUrl = null) { if (doc.Document.body.sectPr == null) { doc.Document.body.sectPr = new CT_SectPr(); } CT_SectPr m_SectPr = doc.Document.body.sectPr; CT_Ftr footer = new CT_Ftr(); CT_SdtBlock sdtBlock = new CT_SdtBlock(); CT_SdtPr sdtPr = sdtBlock.AddNewSdtPr(); CT_SdtDocPart sdtDocPart = sdtPr.AddNewDocPartObj(); sdtDocPart.AddNewDocPartGallery().val = "Page Numbers (Bottom of Page)"; sdtDocPart.docPartUnique = new CT_OnOff(); CT_SdtContentBlock sdtContentBlock = sdtBlock.AddNewSdtContent(); CT_P p = sdtContentBlock.AddNewP(); p.AddNewPPr().AddNewJc().val = ST_Jc.center; p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin; p.AddNewR().AddNewInstrText().Value = "PAGE"; p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate; p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end; p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.begin; p.AddNewR().AddNewInstrText().Value = "NUMPAGES"; p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate; p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.end; footer.Items.Add(sdtBlock); XWPFRelation Hrelation = XWPFRelation.FOOTER; XWPFFooter xwpfFooter = (XWPFFooter)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), doc.FooterList.Count); xwpfFooter.SetXWPFDocument(doc); xwpfFooter.SetHeaderFooter(footer); CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewFooterReference(); m_HdrFtr2.type = ST_HdrFtr.@default; m_HdrFtr2.id = xwpfFooter.GetPackageRelationship().Id; }
protected void AddFooter(XWPFDocument document, string text, int textFontSize) { CT_SectPr secPr = document.Document.body.sectPr; CT_Ftr footer = new CT_Ftr(); var run = footer.AddNewP().AddNewR(); run.AddNewT().Value = text; XWPFRelation relation2 = XWPFRelation.FOOTER; XWPFFooter myFooter = (XWPFFooter)document.CreateRelationship(relation2, XWPFFactory.GetInstance(), document.FooterList.Count + 1); myFooter.SetHeaderFooter(footer); CT_HdrFtrRef myFooterRef = secPr.AddNewFooterReference(); myFooterRef.type = ST_HdrFtr.@default; myFooterRef.id = myFooter.GetPackageRelationship().Id; myFooter.Paragraphs[0].Runs[0].FontSize = textFontSize != -1 ? textFontSize : 10; }
public XWPFFooter CreateFooter(ST_HdrFtr type, XWPFParagraph[] pars) { XWPFRelation footer = XWPFRelation.FOOTER; string pStyle = "Footer"; int relationIndex = this.GetRelationIndex(footer); FtrDocument ftrDocument = new FtrDocument(); XWPFFooter relationship = (XWPFFooter)this.doc.CreateRelationship((POIXMLRelation)footer, (POIXMLFactory)XWPFFactory.GetInstance(), relationIndex); CT_HdrFtr headerFooter = this.buildFtr(type, pStyle, (XWPFHeaderFooter)relationship, pars); relationship.SetHeaderFooter(headerFooter); Stream outputStream = relationship.GetPackagePart().GetOutputStream(); ftrDocument.SetFtr((CT_Ftr)headerFooter); this.assignFooter(relationship, type); ftrDocument.Save(outputStream, this.Commit((XWPFHeaderFooter)relationship)); outputStream.Close(); return(relationship); }
public XWPFFooter CreateFooter(ST_HdrFtr type, XWPFParagraph[] pars) { XWPFRelation relation = XWPFRelation.FOOTER; String pStyle = "Footer"; int i = GetRelationIndex(relation); FtrDocument ftrDoc = new FtrDocument(); XWPFFooter wrapper = (XWPFFooter)doc.CreateRelationship(relation, XWPFFactory.GetInstance(), i); CT_HdrFtr ftr = buildFtr(type, pStyle, wrapper, pars); wrapper.SetHeaderFooter(ftr); ftrDoc.SetFtr((CT_Ftr)ftr); assignFooter(wrapper, type); using (Stream outputStream = wrapper.GetPackagePart().GetOutputStream()) { ftrDoc.Save(outputStream); } return(wrapper); }
/// <summary> /// 创建页眉页脚 /// </summary> /// <param name="doc"></param> private void CreatePageHeaderFooter(XWPFDocument doc, CT_SectPr m_Sectpr) { XWPFParagraph p1 = doc.CreateParagraph(); p1.CreateRun().AddBreak();//新建页 //创建页眉 CT_Hdr m_hdr = new CT_Hdr(); m_hdr.Items = new System.Collections.ArrayList(); CT_P m_p = m_hdr.AddNewP(); CT_PPr cT_PPr = m_p.AddNewPPr(); cT_PPr.AddNewJc().val = ST_Jc.both;//两端对齐 cT_PPr.AddNewSpacing().beforeLines = "370"; CT_R cT_R = m_p.AddNewR(); cT_R.AddNewT().Value = "110kV濂泉(沙河)送电线路工程 施工图设计说明书 S123456S-D0101-01";//页眉内容 CT_RPr cT_RPr = cT_R.AddNewRPr(); cT_RPr.AddNewSz().val = (ulong)18; cT_RPr.AddNewSzCs().val = (ulong)18; cT_RPr.AddNewRFonts().ascii = "宋体"; //cT_RPr.AddNewU().val=ST_Underline.single;//下划线 //创建页眉关系(headern.xml) XWPFRelation Hrelation = XWPFRelation.HEADER; XWPFHeader m_h = (XWPFHeader)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), 3); //doc.CreateFootnotes(); //设置页眉 m_h.SetHeaderFooter(m_hdr); CT_HdrFtrRef m_HdrFtr = m_Sectpr.AddNewHeaderReference(); m_HdrFtr.type = ST_HdrFtr.@default; //m_h.GetRelationById(m_HdrFtr.id); m_HdrFtr.id = m_h.GetPackageRelationship().Id; //创建页脚 CT_Ftr m_ftr = new CT_Ftr(); m_ftr.Items = new System.Collections.ArrayList(); CT_SdtBlock m_Sdt = new CT_SdtBlock(); CT_SdtPr m_SdtPr = m_Sdt.AddNewSdtPr(); CT_SdtDocPart m_SdDocPartObj = m_SdtPr.AddNewDocPartObj(); m_SdDocPartObj.AddNewDocPartGallery().val = "PageNumbers (Bottom of Page)"; m_SdDocPartObj.docPartUnique = new CT_OnOff(); CT_SdtContentBlock m_SdtContent = m_Sdt.AddNewSdtContent(); CT_P m_SdtContentP = m_SdtContent.AddNewP(); CT_PPr m_SdtContentPpr = m_SdtContentP.AddNewPPr(); m_SdtContentPpr.AddNewJc().val = ST_Jc.center; m_SdtContentP.Items = new System.Collections.ArrayList(); CT_SimpleField m_fldSimple = new CT_SimpleField(); m_fldSimple.instr = " PAGE \\*MERGEFORMAT"; //页码字体大小 CT_R m_r = new CT_R(); CT_RPr m_Rpr = m_r.AddNewRPr(); m_Rpr.AddNewRFonts().ascii = "宋体"; m_Rpr.AddNewSz().val = (ulong)18; m_Rpr.AddNewSzCs().val = (ulong)18; m_r.AddNewT().Value = "1";//页数 m_fldSimple.Items.Add(m_r); m_SdtContentP.Items.Add(m_fldSimple); m_ftr.Items.Add(m_Sdt); //m_ftr.AddNewP().AddNewR().AddNewT().Value = "fff";//页脚内容 //m_ftr.AddNewP().AddNewPPr().AddNewJc().val = ST_Jc.center; //创建页脚关系(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_HdrFtr = m_Sectpr.AddNewFooterReference(); m_HdrFtr.type = ST_HdrFtr.@default; m_HdrFtr.id = m_f.GetPackageRelationship().Id; }
public static void ExportDocumentWithDataTables(DocumentSetting setting, List <WordTable> wordTables) { XWPFDocument docx = new XWPFDocument(); MemoryStream ms = new MemoryStream(); #region Page Setting //Set Document docx.Document.body.sectPr = new CT_SectPr(); CT_SectPr setPr = docx.Document.body.sectPr; //get page size Tuple <int, int> size = GetPaperSize(setting.PaperType); setPr.pgSz.w = (ulong)size.Item1; setPr.pgSz.h = (ulong)size.Item2; var mar = GetPaperMar(setting.PaperMarType); setPr.pgMar.left = (ulong)mar.Item3; setPr.pgMar.right = (ulong)mar.Item4; setPr.pgMar.top = mar.Item1.ToString(); setPr.pgMar.bottom = mar.Item2.ToString(); #region Footer CT_Ftr m_ftr = new CT_Ftr(); m_ftr.AddNewP().AddNewR().AddNewT().Value = setting.Footer; XWPFRelation Frelation = XWPFRelation.FOOTER; XWPFFooter m_f = (XWPFFooter)docx.CreateRelationship(Frelation, XWPFFactory.GetInstance(), docx.FooterList.Count + 1); m_f.SetHeaderFooter(m_ftr); CT_HdrFtrRef m_HdrFtr1 = setPr.AddNewFooterReference(); m_HdrFtr1.type = ST_HdrFtr.@default; m_HdrFtr1.id = m_f.GetPackageRelationship().Id; #endregion #endregion Page Setting var i = 0; foreach (var wordTable in wordTables) { i++; ExportDocumentWithDataTable(docx, setting, wordTable); if (i < wordTables.Count) { XWPFParagraph m_xp = docx.CreateParagraph(); m_xp.CreateRun().AddBreak(); } } docx.Write(ms); using (FileStream fs = new FileStream(setting.SavePath, FileMode.Create, FileAccess.Write)) { byte[] data = ms.ToArray(); fs.Write(data, 0, data.Length); fs.Flush(); } ms.Close(); }