Skip to content

Commit 4d2a59a

Browse files
committed
code refacturing implment
1 parent 15ebe81 commit 4d2a59a

2 files changed

Lines changed: 33 additions & 23 deletions

File tree

README.md

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ MiniWord.SaveAsByTemplate(path, templatePath, value);
9696

9797
### Image
9898

99-
tag value is `MiniWordPicture` type
99+
标签值为 `MiniWordPicture` 类别
100100

101101
##### Example
102102

@@ -193,31 +193,22 @@ var value = new { title = "Hello MiniWord" };
193193
MiniWord.SaveAsByTemplate(outputPath, templatePath, value);
194194
```
195195

196-
### HyperLink
197-
198-
We can try to use `MiniWorHyperLink` class which will replace template text with HyperLink.
199-
200-
`MiniWorHyperLink` provide two major parameters.
201-
202-
* Url: HyperLink URI target path
203-
* Text:HyperLink Text
204-
196+
### Font ForeColor and BackColor functions
205197
```csharp
206-
var value = new
207-
{
208-
["Name"] = new MiniWorHyperLink(){
209-
Url = "https://google.com",
210-
Text = "測試連結!!"
211-
},
212-
["Company_Name"] = "MiniSofteware",
213-
["CreateDate"] = new DateTime(2021, 01, 01),
214-
["VIP"] = true,
215-
["Points"] = 123,
216-
["APP"] = "Demo APP",
217-
};
218-
MiniWord.SaveAsByTemplate(path, templatePath, value);
198+
var value = new
199+
{
200+
Company_Name = new MiniWordColorText { Text = "MiniSofteware", ForeColor = "#eb70AB" },
201+
Name = new MiniWordColorText { Text = "Jack", BackColor = "#eb70AB" },
202+
CreateDate = new MiniWordColorText { Text = new DateTime(2021, 01, 01).ToString(), BackColor = "#eb70AB", ForeColor = "#ffffff" },
203+
204+
VIP = true,
205+
Points = 123,
206+
APP = "Demo APP",
207+
};
219208
```
220209

210+
211+
221212
## Examples
222213

223214

src/MiniWord/MiniWord.Implment.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen
211211
run.Append(hyperlink);
212212
t.Remove();
213213
}
214+
else if (tag.Value is MiniWordColorText)
215+
{
216+
var miniWordColorText = (MiniWordColorText)tag.Value;
217+
var colorText = AddColorText(miniWordColorText);
218+
run.Append(colorText);
219+
t.Remove();
220+
}
214221
else
215222
{
216223
var newText = string.Empty;
@@ -270,7 +277,19 @@ private static Hyperlink GetHyperLink(MainDocumentPart mainPart,MiniWorHyperLink
270277
};
271278
return xmlHyperLink;
272279
}
280+
private static RunProperties AddColorText(MiniWordColorText miniWordColorText)
281+
{
282+
283+
RunProperties runPro = new RunProperties();
284+
Text text = new Text(miniWordColorText.Text);
285+
Color color = new Color() { Val = miniWordColorText.ForeColor?.Replace("#", "") };
286+
Shading shading = new Shading() { Fill = miniWordColorText.BackColor?.Replace("#", "") };
287+
runPro.Append(shading);
288+
runPro.Append(color);
289+
runPro.Append(text);
273290

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

0 commit comments

Comments
 (0)