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; }
private static void CreateHeader(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_Hdr header = new CT_Hdr(); //header.AddNewP().AddNewR().AddNewT().Value = headerText; var hp = header.AddNewP(); //byte[] data = Encoding.ASCII.GetBytes("00B80FAA"); hp.rsidR = new byte[] { 0, 184, 15, 170 }; hp.rsidRDefault = new byte[] { 0, 146, 87, 240 }; hp.rsidP = new byte[] { 0, 184, 15, 170 }; var ppr = hp.AddNewPPr(); var ctjc = new CT_Jc(); ctjc.val = ST_Jc.right; ppr.jc = ctjc; var hr = hp.AddNewR().AddNewT().Value = headerText; XWPFRelation Hrelation = XWPFRelation.HEADER; XWPFHeader docHeader = (XWPFHeader)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), doc.HeaderList.Count); docHeader.SetXWPFDocument(doc); XWPFFooter footer = new XWPFFooter(); //设置页眉 docHeader.SetHeaderFooter(header); if (!string.IsNullOrEmpty(imageUrl) && File.Exists(imageUrl)) { var pictureData = new FileStream(imageUrl, FileMode.Open, FileAccess.Read); docHeader.AddPictureData(pictureData, (int)PictureType.PNG); } CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewHeaderReference(); m_HdrFtr2.type = ST_HdrFtr.@default; m_HdrFtr2.id = docHeader.GetPackageRelationship().Id; }
public V5XWPFHeader(XWPFDocument doc) { if (doc.Document.body.sectPr == null) { doc.Document.body.sectPr = new CT_SectPr(); } CT_SectPr m_SectPr = doc.Document.body.sectPr; XWPFRelation Hrelation = XWPFRelation.HEADER; XWPFHeader = (XWPFHeader)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), doc.HeaderList.Count); XWPFHeader.SetXWPFDocument(doc); XWPFHeader.SetHeaderFooter(header); CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewHeaderReference(); m_HdrFtr2.type = ST_HdrFtr.@default; m_HdrFtr2.id = XWPFHeader.GetPackageRelationship().Id; }
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); }
public XWPFHeader CreateHeader(ST_HdrFtr type, XWPFParagraph[] pars) { XWPFRelation relation = XWPFRelation.HEADER; String pStyle = "Header"; int i = GetRelationIndex(relation); HdrDocument hdrDoc = new HdrDocument(); XWPFHeader wrapper = (XWPFHeader)doc.CreateRelationship(relation, XWPFFactory.GetInstance(), i); CT_HdrFtr hdr = buildHdr(type, pStyle, wrapper, pars); wrapper.SetHeaderFooter(hdr); hdrDoc.SetHdr((CT_Hdr)hdr); assignHeader(wrapper, type); using (Stream outputStream = wrapper.GetPackagePart().GetOutputStream()) { hdrDoc.Save(outputStream); } return(wrapper); }
private static void CreateWaterMark(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_Hdr header = new CT_Hdr(); //header.AddNewP().AddNewR().AddNewT().Value = headerText; var paragraph = header.AddNewP(); var ppr = paragraph.AddNewPPr(); ppr.AddNewPStyle().val = "a3"; ppr.AddNewJc().val = ST_Jc.right; var r = paragraph.AddNewR(); r.AddNewRPr().AddNewNoProof(); CT_Picture pict = r.AddNewPict(); CT_Shapetype shapetype = new CT_Shapetype(); shapetype.id = ("_x0000_t136"); shapetype.coordsize = ("21600,21600"); shapetype.spt = (136); shapetype.adj = ("10800"); shapetype.path2 = ("m@7,0l@8,0m@5,21600l@6,21600e"); CT_Formulas formulas = shapetype.AddNewFormulas(); formulas.AddNewF().eqn = ("sum #0 0 10800"); formulas.AddNewF().eqn = ("prod #0 2 1"); formulas.AddNewF().eqn = ("sum 21600 0 @1"); formulas.AddNewF().eqn = ("sum 0 0 @2"); formulas.AddNewF().eqn = ("sum 21600 0 @3"); formulas.AddNewF().eqn = ("if @0 @3 0"); formulas.AddNewF().eqn = ("if @0 21600 @1"); formulas.AddNewF().eqn = ("if @0 0 @2"); formulas.AddNewF().eqn = ("if @0 @4 21600"); formulas.AddNewF().eqn = ("mid @5 @6"); formulas.AddNewF().eqn = ("mid @8 @5"); formulas.AddNewF().eqn = ("mid @7 @8"); formulas.AddNewF().eqn = ("mid @6 @7"); formulas.AddNewF().eqn = ("sum @6 0 @5"); CT_Path path = shapetype.AddNewPath(); path.textpathok = (NPOI.OpenXmlFormats.Vml.ST_TrueFalse.t); path.connecttype = (NPOI.OpenXmlFormats.Vml.Office.ST_ConnectType.custom); path.connectlocs = ("@9,0;@10,10800;@11,21600;@12,10800"); path.connectangles = ("270,180,90,0"); CT_TextPath shapeTypeTextPath = shapetype.AddNewTextpath(); shapeTypeTextPath.on = (NPOI.OpenXmlFormats.Vml.ST_TrueFalse.t); shapeTypeTextPath.fitshape = (NPOI.OpenXmlFormats.Vml.ST_TrueFalse.t); CT_Handles handles = shapetype.AddNewHandles(); CT_H h = handles.AddNewH(); h.position = ("#0,bottomRight"); h.xrange = ("6629,14971"); NPOI.OpenXmlFormats.Vml.Office.CT_Lock lock1 = shapetype.AddNewLock(); lock1.ext = (ST_Ext.edit); lock1.text = NPOI.OpenXmlFormats.Vml.Office.ST_TrueFalse.t; lock1.shapetype = NPOI.OpenXmlFormats.Vml.Office.ST_TrueFalse.t; CT_Shape shape = new CT_Shape(); shape.id = ("PowerPlusWaterMarkObject" + "1109904797"); shape.spid = ("_x0000_s206" + (4 + "1")); shape.type = ("#_x0000_t136"); shape.style = ("position:absolute;margin-left:0;margin-top:0;width:415pt;height:207.5pt;z-index:-251654144;mso-wrap-edited:f;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin"); //shape.wrapcoords = ("616 5068 390 16297 39 16921 -39 17155 7265 17545 7186 17467 -39 17467 18904 17467 10507 17467 8710 17545 18904 17077 18787 16843 18358 16297 18279 12554 19178 12476 20701 11774 20779 11228 21131 10059 21248 8811 21248 7563 20975 6316 20935 5380 19490 5146 14022 5068 2616 5068"); shape.fillcolor = ("#ed7d31 [3205]"); shape.stroked = (NPOI.OpenXmlFormats.Vml.ST_TrueFalse.@false); shape.AddNewFill().opacity = ".5"; CT_TextPath shapeTextPath = shape.AddNewTextpath(); shapeTextPath.style = ("font-family:"Yu Gothic UI""); shapeTextPath.@string = ("吴润杰水印"); // pict.Items.Add(shapetype); //pict.Set(shapetype); pict.Set(shape); //pict.Set(group); XWPFRelation Hrelation = XWPFRelation.HEADER; XWPFHeader docHeader = (XWPFHeader)doc.CreateRelationship(Hrelation, XWPFFactory.GetInstance(), doc.HeaderList.Count); docHeader.SetXWPFDocument(doc); //设置页眉 docHeader.SetHeaderFooter(header); CT_HdrFtrRef m_HdrFtr2 = m_SectPr.AddNewHeaderReference(); m_HdrFtr2.type = ST_HdrFtr.@default; m_HdrFtr2.id = docHeader.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; }
/// <summary> /// Creates an empty header for front pages. /// The returned paragraph should be inserted in front of document /// </summary> /// <example>xwpfDoc.Document.body.Items.Insert(1, CreateFrontHeader());</example> /// <returns>CT_P paragraph that contains a blank header</returns> private CT_P CreateFrontHeader() { CT_Hdr header = new CT_Hdr(); CT_P headerParagraph = header.AddNewP(); headerParagraph.AddNewPPr(); XWPFHeader documentHeader = (XWPFHeader)newDoc.CreateRelationship(XWPFRelation.HEADER, XWPFFactory.GetInstance(), pageHeaderCount); documentHeader.SetHeaderFooter(header); // Create new section and set its header CT_P p = new CT_P(); CT_SectPr newSection = p.AddNewPPr().createSectPr(); newSection.type = new CT_SectType(); newSection.type.val = ST_SectionMark.continuous; CT_HdrFtrRef headerRef = newSection.AddNewHeaderReference(); headerRef.type = ST_HdrFtr.@default; headerRef.id = documentHeader.GetPackageRelationship().Id; // Increment page header count so each one gets a unique ID pageHeaderCount++; return(p); }
/// <summary> /// Creates a new section with the given page header. Must be /// called *after* the final paragraph of the section. In DOCX, a /// section definition is a child of the final paragraph of the /// section, except for the final section of the document, which /// is a direct child of the body. /// </summary> /// <param name="bookname"> The name of the book to display, usually from the \h marker </param> public void createBookHeaders(string bookname) { // Create page heading content for book CT_Hdr header = new CT_Hdr(); CT_P headerParagraph = header.AddNewP(); CT_PPr ppr = headerParagraph.AddNewPPr(); CT_Jc align = ppr.AddNewJc(); align.val = ST_Jc.center; CT_R run = headerParagraph.AddNewR(); // Show page numbers if requested if (configDocx.showPageNumbers) { // Page number run.AddNewFldChar().fldCharType = ST_FldCharType.begin; run.AddNewInstrText().Value = " PAGE "; run.AddNewFldChar().fldCharType = ST_FldCharType.separate; run.AddNewInstrText().Value = "1"; run.AddNewFldChar().fldCharType = ST_FldCharType.end; run.AddNewT().Value = " - "; } // Book name run.AddNewT().Value = bookname == null ? "" : bookname; // Chapter name if (currentChapterLabel.Length > 0) { run.AddNewT().Value = " - "; run.AddNewT().Value = currentChapterLabel; } // Create page header XWPFHeader documentHeader = (XWPFHeader)newDoc.CreateRelationship(XWPFRelation.HEADER, XWPFFactory.GetInstance(), pageHeaderCount); documentHeader.SetHeaderFooter(header); // Create new section and set its header CT_SectPr newSection = newDoc.Document.body.AddNewP().AddNewPPr().createSectPr(); newSection.type = new CT_SectType(); newSection.type.val = ST_SectionMark.continuous; CT_HdrFtrRef headerRef = newSection.AddNewHeaderReference(); headerRef.type = ST_HdrFtr.@default; headerRef.id = documentHeader.GetPackageRelationship().Id; // Set number of columns newSection.cols.num = configDocx.columnCount.ToString(); // Set page numbers CT_PageNumber pageNum = new CT_PageNumber { fmt = ST_NumberFormat.@decimal }; newSection.pgNumType = pageNum; // Increment page header count so each one gets a unique ID pageHeaderCount++; }
/// <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(); }
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); }