/** * Set text color. * @param rgbStr - the desired color, in the hex form "RRGGBB". */ public void SetColor(String rgbStr) { CT_RPr pr = run.IsSetRPr() ? run.rPr : run.AddNewRPr(); NPOI.OpenXmlFormats.Wordprocessing.CT_Color color = pr.IsSetColor() ? pr.color : pr.AddNewColor(); color.val = (rgbStr); }
/** * Whether the bold property shall be applied to all non-complex script * characters in the contents of this run.when displayed in a document. * <p> * This formatting property is a toggle property, which specifies that its * behavior differs between its use within a style defInition and its use as * direct formatting. When used as part of a style defInition, Setting this * property shall toggle the current state of that property as specified up * to this point in the hierarchy (i.e. applied to not applied, and vice * versa). Setting it to <code>false</code> (or an equivalent) shall * result in the current Setting remaining unChanged. However, when used as * direct formatting, Setting this property to true or false shall Set the * absolute state of the resulting property. * </p> * <p> * If this element is not present, the default value is to leave the * formatting applied at previous level in the style hierarchy. If this * element is never applied in the style hierarchy, then bold shall not be * applied to non-complex script characters. * </p> * * @param value <code>true</code> if the bold property is applied to * this run */ public void SetBold(bool value) { CT_RPr pr = run.IsSetRPr() ? run.rPr : run.AddNewRPr(); CT_OnOff bold = pr.IsSetB() ? pr.b : pr.AddNewB(); bold.val = value; }
public void SetStrike(bool value) { CT_RPr pr = run.IsSetRPr() ? run.rPr : run.AddNewRPr(); CT_OnOff strike = pr.IsSetStrike() ? pr.strike : pr.AddNewStrike(); strike.val = value; }
/** * Whether the bold property shall be applied to all non-complex script * characters in the contents of this run.when displayed in a document. * <p> * This formatting property is a toggle property, which specifies that its * behavior differs between its use within a style defInition and its use as * direct formatting. When used as part of a style defInition, Setting this * property shall toggle the current state of that property as specified up * to this point in the hierarchy (i.e. applied to not applied, and vice * versa). Setting it to <code>false</code> (or an equivalent) shall * result in the current Setting remaining unChanged. However, when used as * direct formatting, Setting this property to true or false shall Set the * absolute state of the resulting property. * </p> * <p> * If this element is not present, the default value is to leave the * formatting applied at previous level in the style hierarchy. If this * element is never applied in the style hierarchy, then bold shall not be * applied to non-complex script characters. * </p> * * @param value <code>true</code> if the bold property is applied to * this run */ public void SetBold(bool value) { CT_RPr pr = run.IsSetRPr() ? run.rPr : run.AddNewRPr(); CT_OnOff bold = pr.IsSetB() ? pr.b : pr.AddNewB(); bold.val = (value ? ST_OnOff.True : ST_OnOff.False); }
/** * Ensure that the specified paragraph has a reference marker for this * footnote by adding a footnote reference if one is not found. * <p>This method is for the first paragraph in the footnote, not * paragraphs that will refer to the footnote. For references to * the footnote, use {@link XWPFParagraph#addFootnoteReference(XWPFFootnote)}. * </p> * <p>The first run of the first paragraph in a footnote should * contain a {@link CTFtnEdnRef} object.</p> * * @param p The {@link XWPFParagraph} to ensure */ public void EnsureFootnoteRef(XWPFParagraph p) { XWPFRun r = null; if (p.Runs.Count > 0) { r = p.Runs[0]; } if (r == null) { r = p.CreateRun(); } CT_R ctr = r.GetCTR(); bool foundRef = false; foreach (CT_FtnEdnRef reference in ctr.GetFootnoteReferenceList()) { if (Id.ToString().Equals(reference.id)) { foundRef = true; break; } } if (!foundRef) { ctr.AddNewRPr().AddNewRStyle().val = "FootnoteReference"; ctr.AddNewFootnoteRef(); } }
/// <summary> /// 设置字体大小 /// </summary> /// <param name="run"></param> /// <param name="sizeType"></param> public static void SetFontSize(this XWPFRun run, FontSizeType sizeType) { ulong fontSize = GetFontSize(sizeType); CT_R r = run.GetCTR(); CT_RPr rpr = r.IsSetRPr() ? r.rPr : r.AddNewRPr(); CT_HpsMeasure ctSize = rpr.IsSetSz() ? rpr.sz : rpr.AddNewSz(); ctSize.val = fontSize; }
/// <summary> /// 设置颜色 /// </summary> /// <param name="run"></param> /// <param name="colorType"></param> public static void SetRunColor(this XWPFRun run, ColorType colorType) { Tuple <string, string> temp = GetColor(colorType); CT_R r = run.GetCTR(); CT_RPr rpr = r.IsSetRPr() ? r.rPr : r.AddNewRPr(); CT_Color color = rpr.IsSetColor() ? rpr.color : rpr.AddNewColor(); color.val = temp.Item1; }
public void TestSetGetBold() { CT_RPr rpr = ctRun.AddNewRPr(); rpr.AddNewB().val = (ST_OnOff.True); XWPFRun run = new XWPFRun(ctRun, p); Assert.AreEqual(true, run.IsBold()); run.SetBold(false); Assert.AreEqual(ST_OnOff.False, rpr.b.val); }
public void TestSetGetBold() { CT_RPr rpr = ctRun.AddNewRPr(); rpr.AddNewB().val = true; XWPFRun run = new XWPFRun(ctRun, p); Assert.AreEqual(true, run.IsBold); run.IsBold = (false); Assert.AreEqual(false, run.IsBold); Assert.AreEqual(false, rpr.b.val); }
public void TestSetGetBold() { CT_RPr rpr = ctRun.AddNewRPr(); rpr.AddNewB().val = true; XWPFRun run = new XWPFRun(ctRun, p); Assert.AreEqual(true, run.IsBold); run.IsBold = (false); // Implementation detail: POI natively prefers <w:b w:val="false"/>, // but should correctly read val="0" and val="off" Assert.AreEqual(false, run.IsBold); Assert.AreEqual(false, rpr.b.val); }
public TOC(CT_SdtBlock block) { this.block = block; CT_SdtPr sdtPr = block.AddNewSdtPr(); CT_DecimalNumber id = sdtPr.AddNewId(); id.val = ("4844945"); sdtPr.AddNewDocPartObj().AddNewDocPartGallery().val = ("Table of Contents"); CT_SdtEndPr sdtEndPr = block.AddNewSdtEndPr(); CT_RPr rPr = sdtEndPr.AddNewRPr(); CT_Fonts fonts = rPr.AddNewRFonts(); fonts.asciiTheme = (ST_Theme.minorHAnsi); fonts.eastAsiaTheme = (ST_Theme.minorHAnsi); fonts.hAnsiTheme = (ST_Theme.minorHAnsi); fonts.cstheme = (ST_Theme.minorBidi); CT_SdtContentBlock content = block.AddNewSdtContent(); CT_P p = content.AddNewP(); byte[] b = Encoding.Unicode.GetBytes("00EF7E24"); p.rsidR = b; p.rsidRDefault = b; CT_PPr pPr = p.AddNewPPr(); pPr.AddNewPStyle().val = ("TOCHeading"); pPr.AddNewJc().val = ST_Jc.center; CT_R run = p.AddNewR(); run.AddNewRPr().AddNewSz().val = 48; run.AddNewT().Value = ("Table of Contents"); run.AddNewBr().type = ST_BrType.textWrapping; // line break // TOC Field p = content.AddNewP(); pPr = p.AddNewPPr(); pPr.AddNewPStyle().val = "TOC1"; pPr.AddNewRPr().AddNewNoProof(); run = p.AddNewR(); run.AddNewFldChar().fldCharType = ST_FldCharType.begin; run = p.AddNewR(); CT_Text text = run.AddNewInstrText(); text.space = "preserve"; text.Value = (" TOC \\h \\z "); p.AddNewR().AddNewFldChar().fldCharType = ST_FldCharType.separate; }
public void AddRow(int level, string title, int page, string bookmarkRef) { CT_P ctP = this.block.sdtContent.AddNewP(); byte[] bytes = Encoding.Unicode.GetBytes("00EF7E24"); ctP.rsidR = bytes; ctP.rsidRDefault = bytes; CT_PPr ctPpr = ctP.AddNewPPr(); ctPpr.AddNewPStyle().val = nameof(TOC) + (object)level; CT_TabStop ctTabStop = ctPpr.AddNewTabs().AddNewTab(); ctTabStop.val = ST_TabJc.right; ctTabStop.leader = ST_TabTlc.dot; ctTabStop.pos = "8290"; ctPpr.AddNewRPr().AddNewNoProof(); CT_R ctR1 = ctP.AddNewR(); ctR1.AddNewRPr().AddNewNoProof(); ctR1.AddNewT().Value = title; CT_R ctR2 = ctP.AddNewR(); ctR2.AddNewRPr().AddNewNoProof(); ctR2.AddNewTab(); CT_R ctR3 = ctP.AddNewR(); ctR3.AddNewRPr().AddNewNoProof(); ctR3.AddNewFldChar().fldCharType = ST_FldCharType.begin; CT_R ctR4 = ctP.AddNewR(); ctR4.AddNewRPr().AddNewNoProof(); CT_Text ctText = ctR4.AddNewInstrText(); ctText.space = "preserve"; ctText.Value = " PAGEREF _Toc" + bookmarkRef + " \\h "; ctP.AddNewR().AddNewRPr().AddNewNoProof(); CT_R ctR5 = ctP.AddNewR(); ctR5.AddNewRPr().AddNewNoProof(); ctR5.AddNewFldChar().fldCharType = ST_FldCharType.separate; CT_R ctR6 = ctP.AddNewR(); ctR6.AddNewRPr().AddNewNoProof(); ctR6.AddNewT().Value = page.ToString(); CT_R ctR7 = ctP.AddNewR(); ctR7.AddNewRPr().AddNewNoProof(); ctR7.AddNewFldChar().fldCharType = ST_FldCharType.end; }
public CT_SdtBlock Build() { // append end field char for TOC - only once if (!isBuilt) { CT_SdtContentBlock contentBlock = block.sdtContent; CT_P p = contentBlock.AddNewP(); CT_R run = p.AddNewR(); run.AddNewRPr().AddNewNoProof(); run.AddNewFldChar().fldCharType = ST_FldCharType.end; isBuilt = true; } return(block); }
public void AddRow(int level, String title, int page, String bookmarkRef) { CT_SdtContentBlock contentBlock = this.block.sdtContent; CT_P p = contentBlock.AddNewP(); byte[] b = Encoding.Unicode.GetBytes("00EF7E24"); p.rsidR = b; p.rsidRDefault = b; CT_PPr pPr = p.AddNewPPr(); pPr.AddNewPStyle().val = ("TOC" + level); CT_Tabs tabs = pPr.AddNewTabs(); CT_TabStop tab = tabs.AddNewTab(); tab.val = (ST_TabJc.right); tab.leader = (ST_TabTlc.dot); tab.pos = "8290"; //(new BigInteger("8290")); pPr.AddNewRPr().AddNewNoProof(); CT_R Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); Run.AddNewT().Value = (title); Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); Run.AddNewTab(); Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); Run.AddNewFldChar().fldCharType = (ST_FldCharType.begin); // pageref run Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); CT_Text text = Run.AddNewInstrText(); text.space = "preserve";// (Space.PRESERVE); // bookmark reference text.Value = (" PAGEREF _Toc" + bookmarkRef + " \\h "); p.AddNewR().AddNewRPr().AddNewNoProof(); Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); Run.AddNewFldChar().fldCharType = (ST_FldCharType.separate); // page number run Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); Run.AddNewT().Value = page.ToString(); Run = p.AddNewR(); Run.AddNewRPr().AddNewNoProof(); Run.AddNewFldChar().fldCharType = (ST_FldCharType.end); }
/// <summary> /// 设置字体 /// </summary> /// <param name="run"></param> /// <param name="nameType"></param> public static void SetFontName(this XWPFRun run, FontNameType nameType) { string fontName = GetFontName(nameType); CT_R r = run.GetCTR(); CT_RPr rpr = r.IsSetRPr() ? r.rPr : r.AddNewRPr(); CT_Fonts fonts = rpr.IsSetRFonts() ? rpr.rFonts : rpr.AddNewRFonts(); fonts.ascii = fontName; if (string.IsNullOrEmpty(fonts.eastAsia)) { fonts.eastAsia = fontName; } if (string.IsNullOrEmpty(fonts.cs)) { fonts.cs = fontName; } if (string.IsNullOrEmpty(fonts.hAnsi)) { fonts.hAnsi = fontName; } }
/// <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; }