Skip to content

Commit 34a71a1

Browse files
committed
fix merge confict
2 parents f9e249b + 2a077b5 commit 34a71a1

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

README.zh-CN.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ var value = new { title = "Hello MiniWord" };
199199
MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
200200
```
201201

202+
### 字体ForeColor和BackColor功能
203+
```csharp
204+
var value = new
205+
{
206+
Company_Name = new MiniWordColorText { Text = "MiniSofteware", ForeColor = "#eb70AB" },
207+
Name = new MiniWordColorText { Text = "Jack", BackColor = "#eb70AB" },
208+
CreateDate = new MiniWordColorText { Text = new DateTime(2021, 01, 01).ToString(), BackColor = "#eb70AB", ForeColor = "#ffffff" },
209+
VIP = true,
210+
Points = 123,
211+
APP = "Demo APP",
212+
};
213+
```
202214

203215
### HyperLink
204216

README.zh-Hant.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ var value = new { title = "Hello MiniWord" };
197197
MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
198198
```
199199

200+
### 字体ForeColor和BackColor功能
201+
```csharp
202+
var value = new
203+
{
204+
Company_Name = new MiniWordColorText { Text = "MiniSofteware", ForeColor = "#eb70AB" },
205+
Name = new MiniWordColorText { Text = "Jack", BackColor = "#eb70AB" },
206+
CreateDate = new MiniWordColorText { Text = new DateTime(2021, 01, 01).ToString(), BackColor = "#eb70AB", ForeColor = "#ffffff" },
207+
208+
VIP = true,
209+
Points = 123,
210+
APP = "Demo APP",
211+
};
212+
```
200213

201214
### HyperLink
202215

src/MiniWord/MiniWord.Implment.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,13 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen
212212
run.Append(hyperlink);
213213
t.Remove();
214214
}
215+
else if (tag.Value is MiniWordColorText)
216+
{
217+
var miniWordColorText = (MiniWordColorText)tag.Value;
218+
var colorText = AddColorText(miniWordColorText);
219+
run.Append(colorText);
220+
t.Remove();
221+
}
215222
else
216223
{
217224
var newText = string.Empty;
@@ -271,7 +278,19 @@ private static Hyperlink GetHyperLink(MainDocumentPart mainPart, MiniWordHyperLi
271278
};
272279
return xmlHyperLink;
273280
}
281+
private static RunProperties AddColorText(MiniWordColorText miniWordColorText)
282+
{
283+
284+
RunProperties runPro = new RunProperties();
285+
Text text = new Text(miniWordColorText.Text);
286+
Color color = new Color() { Val = miniWordColorText.TextColor?.Replace("#", "") };
287+
Shading shading = new Shading() { Fill = miniWordColorText.BackgroundColor?.Replace("#", "") };
288+
runPro.Append(shading);
289+
runPro.Append(color);
290+
runPro.Append(text);
274291

292+
return runPro;
293+
}
275294
private static void AddPicture(OpenXmlElement appendElement, string relationshipId, MiniWordPicture pic)
276295
{
277296
// Define the reference of the image.

src/MiniWord/MiniWordColorText.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MiniSoftware
8+
{
9+
public class MiniWordColorText
10+
{
11+
public string TextColor { get; set; }
12+
public string Text { get; set; }
13+
public string BackgroundColor { get; set; }
14+
}
15+
}

tests/MiniWordTests/IssueTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,24 @@ public void TestIssue4_new()
598598
};
599599
MiniWord.SaveAsByTemplate(path, templatePath, value);
600600
}
601+
[Fact]
602+
public void TestColor()
603+
{
604+
var path = PathHelper.GetTempFilePath();
605+
var templatePath = PathHelper.GetFile("TestBasicFill.docx");
606+
var value = new
607+
{
608+
Company_Name = new MiniWordColorText { Text = "MiniSofteware", TextColor = "#eb70AB" },
609+
Name = new MiniWordColorText { Text = "Jack", BackgroundColor = "#eb70AB" },
610+
CreateDate = new MiniWordColorText { Text = new DateTime(2021, 01, 01).ToString(), BackgroundColor = "#eb70AB", TextColor = "#ffffff" },
611+
612+
VIP = true,
613+
Points = 123,
614+
APP = "Demo APP",
615+
};
616+
MiniWord.SaveAsByTemplate(path, templatePath, value);
617+
}
618+
601619

602620
#region Model:TestIssue18.docx
603621
public class Foo

0 commit comments

Comments
 (0)