private static void AnalysisXWPFFooter(XWPFFooter footer) { foreach (var paragraph in footer.GetListParagraph()) { AnalysisXWPFParagraphs(paragraph); } }
public void TestCreate() { XWPFDocument doc = new XWPFDocument(); Assert.AreEqual(null, doc.GetHeaderFooterPolicy()); Assert.AreEqual(0, doc.HeaderList.Count); Assert.AreEqual(0, doc.FooterList.Count); XWPFHeaderFooterPolicy policy = doc.CreateHeaderFooterPolicy(); Assert.IsNotNull(doc.GetHeaderFooterPolicy()); Assert.AreEqual(0, doc.HeaderList.Count); Assert.AreEqual(0, doc.FooterList.Count); // Create a header and a footer XWPFHeader header = policy.CreateHeader(XWPFHeaderFooterPolicy.DEFAULT); XWPFFooter footer = policy.CreateFooter(XWPFHeaderFooterPolicy.DEFAULT); header.CreateParagraph().CreateRun().SetText("Header Hello"); footer.CreateParagraph().CreateRun().SetText("Footer Bye"); // Save, re-load, and check doc = XWPFTestDataSamples.WriteOutAndReadBack(doc); Assert.IsNotNull(doc.GetHeaderFooterPolicy()); Assert.AreEqual(1, doc.HeaderList.Count); Assert.AreEqual(1, doc.FooterList.Count); Assert.AreEqual("Header Hello\n", doc.HeaderList[(0)].Text); Assert.AreEqual("Footer Bye\n", doc.FooterList[(0)].Text); }
public XWPFHeaderFooterPolicy(XWPFDocument doc, CT_SectPr sectPr) { this.doc = doc; for (int i = 0; i < sectPr.SizeOfHeaderReferenceArray(); ++i) { CT_HdrFtrRef headerReferenceArray = sectPr.GetHeaderReferenceArray(i); POIXMLDocumentPart relationById = doc.GetRelationById(headerReferenceArray.id); XWPFHeader hdr = (XWPFHeader)null; if (relationById != null && relationById is XWPFHeader) { hdr = (XWPFHeader)relationById; } ST_HdrFtr type = headerReferenceArray.type; this.assignHeader(hdr, type); } for (int i = 0; i < sectPr.SizeOfFooterReferenceArray(); ++i) { CT_HdrFtrRef footerReferenceArray = sectPr.GetFooterReferenceArray(i); POIXMLDocumentPart relationById = doc.GetRelationById(footerReferenceArray.id); XWPFFooter ftr = (XWPFFooter)null; if (relationById != null && relationById is XWPFFooter) { ftr = (XWPFFooter)relationById; } ST_HdrFtr type = footerReferenceArray.type; this.assignFooter(ftr, type); } }
public void TestSimpleHeader() { XWPFDocument sampleDoc = XWPFTestDataSamples.OpenSampleDocument("headerFooter.docx"); XWPFHeaderFooterPolicy policy = sampleDoc.GetHeaderFooterPolicy(); XWPFHeader header = policy.GetDefaultHeader(); XWPFFooter footer = policy.GetDefaultFooter(); Assert.IsNotNull(header); Assert.IsNotNull(footer); }
private void assignFooter(XWPFFooter ftr, ST_HdrFtr type) { if (type == ST_HdrFtr.first) { firstPageFooter = ftr; } else if (type == ST_HdrFtr.even) { evenPageFooter = ftr; } else { defaultFooter = ftr; } }
private void assignFooter(XWPFFooter ftr, ST_HdrFtr type) { if (type == ST_HdrFtr.FIRST) { this.firstPageFooter = ftr; } else if (type == ST_HdrFtr.EVEN) { this.evenPageFooter = ftr; } else { this.defaultFooter = ftr; } }
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); }
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 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); }
/** * Figures out the policy for the given document, * and Creates any header and footer objects * as required. */ public XWPFHeaderFooterPolicy(XWPFDocument doc, CT_SectPr sectPr) { // Grab what headers and footers have been defined // For now, we don't care about different ranges, as it // doesn't seem that .docx properly supports that // feature of the file format yet this.doc = doc; for (int i = 0; i < sectPr.SizeOfHeaderReferenceArray(); i++) { // Get the header CT_HdrFtrRef ref1 = sectPr.GetHeaderReferenceArray(i); POIXMLDocumentPart relatedPart = doc.GetRelationById(ref1.id); XWPFHeader hdr = null; if (relatedPart != null && relatedPart is XWPFHeader) { hdr = (XWPFHeader)relatedPart; } // Assign it ST_HdrFtr type = ref1.type; assignHeader(hdr, type); } for (int i = 0; i < sectPr.SizeOfFooterReferenceArray(); i++) { // Get the footer CT_HdrFtrRef ref1 = sectPr.GetFooterReferenceArray(i); POIXMLDocumentPart relatedPart = doc.GetRelationById(ref1.id); XWPFFooter ftr = null; if (relatedPart != null && relatedPart is XWPFFooter) { ftr = (XWPFFooter)relatedPart; } // Assign it ST_HdrFtr type = ref1.type; assignFooter(ftr, type); } }
/// <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 void TestSetHeader() { XWPFDocument sampleDoc = XWPFTestDataSamples.OpenSampleDocument("SampleDoc.docx"); // no header is Set (yet) XWPFHeaderFooterPolicy policy = sampleDoc.GetHeaderFooterPolicy(); Assert.IsNull(policy.GetDefaultHeader()); Assert.IsNull(policy.GetFirstPageHeader()); Assert.IsNull(policy.GetDefaultFooter()); CT_P ctP1 = new CT_P(); CT_R ctR1 = ctP1.AddNewR(); CT_Text t = ctR1.AddNewT(); String tText = "Paragraph in header"; t.Value = tText; // Commented MB 23 May 2010 //CTP ctP2 = CTP.Factory.NewInstance(); //CTR ctR2 = ctP2.AddNewR(); //CTText t2 = ctR2.AddNewT(); //t2.StringValue=("Second paragraph.. for footer"); // Create two paragraphs for insertion into the footer. // Previously only one was inserted MB 23 May 2010 CT_P ctP2 = new CT_P(); CT_R ctR2 = ctP2.AddNewR(); CT_Text t2 = ctR2.AddNewT(); t2.Value = ("First paragraph for the footer"); CT_P ctP3 = new CT_P(); CT_R ctR3 = ctP3.AddNewR(); CT_Text t3 = ctR3.AddNewT(); t3.Value = ("Second paragraph for the footer"); XWPFParagraph p1 = new XWPFParagraph(ctP1, sampleDoc); XWPFParagraph[] pars = new XWPFParagraph[1]; pars[0] = p1; XWPFParagraph p2 = new XWPFParagraph(ctP2, sampleDoc); XWPFParagraph p3 = new XWPFParagraph(ctP3, sampleDoc); XWPFParagraph[] pars2 = new XWPFParagraph[2]; pars2[0] = p2; pars2[1] = p3; // Set headers XWPFHeader headerD = policy.CreateHeader(XWPFHeaderFooterPolicy.DEFAULT, pars); XWPFHeader headerF = policy.CreateHeader(XWPFHeaderFooterPolicy.FIRST); // Set a default footer and capture the returned XWPFFooter object. XWPFFooter footer = policy.CreateFooter(XWPFHeaderFooterPolicy.DEFAULT, pars2); // Ensure the headers and footer were Set correctly.... Assert.IsNotNull(policy.GetDefaultHeader()); Assert.IsNotNull(policy.GetFirstPageHeader()); Assert.IsNotNull(policy.GetDefaultFooter()); // ....and that the footer object captured above Contains two // paragraphs of text. Assert.AreEqual(2, footer.Paragraphs.Count); // Check the header created with the paragraph got them, and the one // created without got an empty one Assert.AreEqual(1, headerD.Paragraphs.Count); Assert.AreEqual(1, headerF.Paragraphs.Count); Assert.AreEqual(tText, headerD.Paragraphs[0].Text); Assert.AreEqual("", headerF.Paragraphs[0].Text); // As an Additional Check, recover the defauls footer and // make sure that it Contains two paragraphs of text and that // both do hold what is expected. footer = policy.GetDefaultFooter(); XWPFParagraph[] paras = new List <XWPFParagraph>(footer.Paragraphs).ToArray(); Assert.AreEqual(2, paras.Length); Assert.AreEqual("First paragraph for the footer", paras[0].Text); Assert.AreEqual("Second paragraph for the footer", paras[1].Text); // Add some text to the empty header String fText1 = "New Text!"; headerF.Paragraphs[0].InsertNewRun(0).SetText(fText1); // TODO Add another paragraph and check // Check it Assert.AreEqual(tText, headerD.Paragraphs[0].Text); Assert.AreEqual(fText1, headerF.Paragraphs[0].Text); // Save, re-open, ensure it's all still there XWPFDocument reopened = XWPFTestDataSamples.WriteOutAndReadBack(sampleDoc); policy = reopened.GetHeaderFooterPolicy(); Assert.IsNotNull(policy.GetDefaultHeader()); Assert.IsNotNull(policy.GetFirstPageHeader()); Assert.IsNull(policy.GetEvenPageHeader()); Assert.IsNotNull(policy.GetDefaultFooter()); Assert.IsNull(policy.GetFirstPageFooter()); Assert.IsNull(policy.GetEvenPageFooter()); // Check the new headers still have their text headerD = policy.GetDefaultHeader(); headerF = policy.GetFirstPageHeader(); Assert.AreEqual(tText, headerD.Paragraphs[0].Text); Assert.AreEqual(fText1, headerF.Paragraphs[0].Text); // Check the new footers have their new text too footer = policy.GetDefaultFooter(); paras = new List <XWPFParagraph>(footer.Paragraphs).ToArray(); Assert.AreEqual(2, paras.Length); Assert.AreEqual("First paragraph for the footer", paras[0].Text); Assert.AreEqual("Second paragraph for the footer", paras[1].Text); }