Skip to content

Commit 9d30797

Browse files
authored
Merge pull request #1723 from tonyqus/xwpfrun1
POI Bug 66187 - MS document is error by XWPFDocument to write when XWPFRun call method getTextHightlightColor() or getEmphasisMark
2 parents 63723f0 + 1fc512d commit 9d30797

File tree

3 files changed

+311
-90
lines changed

3 files changed

+311
-90
lines changed

OpenXmlFormats/Wordprocessing/Paragraph.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5021,6 +5021,39 @@ public bool IsSetHighlight()
50215021
{
50225022
return this.highlightField != null;
50235023
}
5024+
5025+
public bool IsSetVanish()
5026+
{
5027+
return this.vanishField != null;
5028+
}
5029+
5030+
public CT_OnOff AddNewVanish()
5031+
{
5032+
this.vanishField = new CT_OnOff();
5033+
return this.vanishField;
5034+
}
5035+
5036+
public bool IsSetW()
5037+
{
5038+
return this.w!=null;
5039+
}
5040+
5041+
public CT_TextScale AddNewW()
5042+
{
5043+
this.w=new CT_TextScale();
5044+
return this.w;
5045+
}
5046+
5047+
public bool IsSetEm()
5048+
{
5049+
return this.em!=null;
5050+
}
5051+
5052+
public CT_Em AddNewEm()
5053+
{
5054+
this.em = new CT_Em();
5055+
return this.em;
5056+
}
50245057
}
50255058

50265059

ooxml/XWPF/Usermodel/XWPFRun.cs

Lines changed: 147 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ limitations under the License.
1616
==================================================================== */
1717
namespace NPOI.XWPF.UserModel
1818
{
19-
using Cysharp.Text;
19+
using Cysharp.Text;
20+
using EnumsNET;
2021
using NPOI.OpenXmlFormats.Dml;
2122
using NPOI.OpenXmlFormats.Dml.WordProcessing;
2223
using NPOI.OpenXmlFormats.Wordprocessing;
@@ -891,7 +892,22 @@ public VerticalAlign Subscript
891892
ctValign.val = EnumConverter.ValueOf<ST_VerticalAlignRun, VerticalAlign>(value);
892893
}
893894
}
894-
895+
/// <summary>
896+
/// Get the vanish (hidden text) value
897+
/// </summary>
898+
public bool IsVanish
899+
{
900+
get
901+
{
902+
var pr = GetRunProperties(false);
903+
return pr != null && pr.IsSetVanish() && IsCTOnOff(pr.vanish);
904+
}
905+
set {
906+
var pr = GetRunProperties(true);
907+
CT_OnOff vanish = pr.IsSetVanish() ? pr.vanish : pr.AddNewVanish();
908+
vanish.val = value;
909+
}
910+
}
895911
public int Kerning
896912
{
897913
get
@@ -913,18 +929,123 @@ public bool IsHighlighted
913929
{
914930
get
915931
{
916-
CT_RPr pr = run.rPr;
932+
CT_RPr pr = GetRunProperties(false);
917933
if (pr == null || !pr.IsSetHighlight())
918934
return false;
919935
if (pr.highlight.val == ST_HighlightColor.none)
920936
return false;
921937
return true;
922938
}
923939
}
924-
// TODO Provide a wrapper round STHighlightColor, then expose getter/setter
925-
// for the highlight colour. Ideally also then add to CharacterRun interface
940+
public string TextHighlightColor
941+
{
942+
get
943+
{
944+
var pr = GetRunProperties(false);
945+
if (pr==null||!pr.IsSetHighlight())
946+
return null;
947+
if (pr.highlight.val == ST_HighlightColor.none)
948+
return null;
949+
return pr.highlight.val.ToString();
950+
}
951+
set
952+
{
953+
var pr = GetRunProperties(true);
954+
CT_Highlight highlight = pr.IsSetHighlight() ? pr.highlight : pr.AddNewHighlight();
955+
if (value == null)
956+
{
957+
highlight.val = ST_HighlightColor.none;
958+
}
959+
else
960+
{
961+
highlight.val = Enums.Parse<ST_HighlightColor>(value,true);
962+
}
963+
}
964+
}
965+
public string UnderlineColor
966+
{
967+
get {
968+
var underline = GetCTUnderline(false);
969+
if (underline?.color==null)
970+
return "auto";
971+
return underline.color;
972+
}
973+
set {
974+
var underline = GetCTUnderline(true);
975+
underline.color = value.ToLower();
976+
}
977+
}
978+
public ST_Em EmphasisMark
979+
{
980+
get {
981+
CT_RPr pr = GetRunProperties(false);
982+
if(pr==null || !pr.IsSetEm())
983+
return ST_Em.none;
984+
return pr.em.val;
985+
}
986+
set
987+
{
988+
CT_RPr pr = GetRunProperties(true);
989+
var em = pr.IsSetEm() ? pr.em : pr.AddNewEm();
990+
em.val=value;
991+
}
992+
}
993+
public int TextScale
994+
{
995+
get {
996+
CT_RPr pr = GetRunProperties(false);
997+
if(pr==null|| !pr.IsSetW())
998+
{
999+
return 100;
1000+
}
1001+
return Int32.Parse(pr.w.val);
1002+
}
1003+
set {
1004+
CT_RPr pr = GetRunProperties(true);
1005+
var scale = pr.IsSetW() ? pr.w : pr.AddNewW();
1006+
scale.val = value.ToString();
1007+
}
1008+
}
9261009

1010+
/// <summary>
1011+
/// Get or set the vertical alignment of the run.
1012+
/// </summary>
1013+
public ST_VerticalAlignRun VerticalAlignment
1014+
{
1015+
get
1016+
{
1017+
CT_RPr pr = GetRunProperties(false);
1018+
if(pr==null || !pr.IsSetVertAlign())
1019+
return ST_VerticalAlignRun.baseline;
1020+
return pr.vertAlign.val;
1021+
}
1022+
set
1023+
{
1024+
CT_RPr pr = GetRunProperties(true);
1025+
CT_VerticalAlignRun vertAlign = pr.IsSetVertAlign() ? pr.vertAlign : pr.AddNewVertAlign();
1026+
vertAlign.val =value;
1027+
}
1028+
}
9271029

1030+
/// <summary>
1031+
/// Set or get the highlight color for the run
1032+
/// </summary>
1033+
public ST_HighlightColor TextHightlightColor
1034+
{
1035+
get
1036+
{
1037+
var pr = GetRunProperties(false);
1038+
if(pr==null||!pr.IsSetHighlight())
1039+
return ST_HighlightColor.none;
1040+
return pr.highlight.val;
1041+
}
1042+
set
1043+
{
1044+
var pr = GetRunProperties(true);
1045+
CT_Highlight highlight = pr.IsSetHighlight() ? pr.highlight : pr.AddNewHighlight();
1046+
highlight.val = value;
1047+
}
1048+
}
9281049
public int CharacterSpacing
9291050
{
9301051
get
@@ -1174,9 +1295,9 @@ public void AddTab()
11741295
run.AddNewTab();
11751296
}
11761297

1177-
public void RemoveTab()
1298+
public void RemoveTab(int p)
11781299
{
1179-
//TODO
1300+
run.RemoveTab(p);
11801301
}
11811302

11821303
/**
@@ -1198,7 +1319,7 @@ public void AddCarriageReturn()
11981319

11991320
public void RemoveCarriageReturn(int i)
12001321
{
1201-
throw new NotImplementedException();
1322+
run.RemoveCr(i);
12021323
}
12031324

12041325
XWPFPicture AddPicture(Stream pictureData, int pictureType, String filename, int width, int height, Action<XWPFDocument, CT_Blip> extAct)
@@ -1439,6 +1560,24 @@ public String Lang
14391560
ctLang.val = value;
14401561
}
14411562
}
1563+
1564+
public string UnderlineThemeColor
1565+
{
1566+
get{
1567+
var underline = GetCTUnderline(false);
1568+
if(underline?.themeColor==null)
1569+
return "none";
1570+
return underline.themeColor.ToString();
1571+
}
1572+
set{
1573+
var underline = GetCTUnderline(true);
1574+
if (value!=null)
1575+
{
1576+
underline.themeColor = Enums.Parse<ST_ThemeColor>(value, true);
1577+
}
1578+
}
1579+
}
1580+
14421581
protected CT_RPr GetRunProperties(bool create)
14431582
{
14441583
CT_RPr pr = run.IsSetRPr() ? run.rPr : null;

0 commit comments

Comments
 (0)