Skip to content

Commit d91acf6

Browse files
committed
Added support image bytes for generate
1 parent 725e36b commit d91acf6

File tree

5 files changed

+87
-37
lines changed

5 files changed

+87
-37
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace MiniSoftware.Common.Enums
2+
{
3+
public enum Extension
4+
{
5+
Bmp,
6+
Emf,
7+
Icon,
8+
Jpeg,
9+
Pcx,
10+
Png,
11+
Svg,
12+
Tiff,
13+
Wmf
14+
}
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace MiniSoftware.Extensions
2+
{
3+
public static class StringExtension
4+
{
5+
public static string FirstCharacterToLower(this string str)
6+
{
7+
return string.IsNullOrWhiteSpace(str) ? str : char.ToLowerInvariant(str[0]) + str.Substring(1);
8+
}
9+
}
10+
}

src/MiniWord/MiniWordPicture.cs

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,61 @@
1-
namespace MiniSoftware
2-
{
3-
using DocumentFormat.OpenXml;
4-
using DocumentFormat.OpenXml.Packaging;
5-
using System;
6-
using System.Collections.Generic;
1+
using System;
2+
using DocumentFormat.OpenXml;
3+
using DocumentFormat.OpenXml.Packaging;
4+
using MiniSoftware.Common.Enums;
5+
using MiniSoftware.Extensions;
76

7+
namespace MiniSoftware
8+
{
89
public enum MiniWordPictureWrappingType
910
{
1011
Inline,
1112
Anchor
1213
}
13-
14+
1415
public class MiniWordPicture
1516
{
16-
17+
public MiniWordPicture(string path, long width = 400, long height = 400)
18+
{
19+
Path = path;
20+
Width = width;
21+
Height= height;
22+
}
23+
24+
public MiniWordPicture(byte[] bytes, Extension extension, long width = 400, long height = 400)
25+
{
26+
Bytes = bytes;
27+
Extension = extension.ToString().FirstCharacterToLower();
28+
29+
Width = width;
30+
Height = height;
31+
}
32+
33+
private string _extension;
34+
1735
public MiniWordPictureWrappingType WrappingType { get; set; } = MiniWordPictureWrappingType.Inline;
1836

1937
public bool BehindDoc { get; set; } = false;
2038
public bool AllowOverlap { get; set; } = false;
2139
public long HorizontalPositionOffset { get; set; } = 0;
2240
public long VerticalPositionOffset { get; set; } = 0;
23-
41+
2442
public string Path { get; set; }
25-
private string _extension;
43+
2644
public string Extension
2745
{
2846
get
2947
{
48+
if (!string.IsNullOrWhiteSpace(_extension))
49+
return _extension;
50+
3051
if (Path != null)
3152
return System.IO.Path.GetExtension(Path).ToUpperInvariant().Replace(".", "");
32-
else
33-
{
34-
return _extension.ToUpper();
35-
}
53+
54+
return string.Empty;
3655
}
37-
set { _extension = value; }
56+
set => _extension = value;
3857
}
58+
3959
internal PartTypeInfo GetImagePartType
4060
{
4161
get
@@ -45,8 +65,9 @@ internal PartTypeInfo GetImagePartType
4565
case "bmp": return ImagePartType.Bmp;
4666
case "emf": return ImagePartType.Emf;
4767
case "ico": return ImagePartType.Icon;
48-
case "jpg": return ImagePartType.Jpeg;
49-
case "jpeg": return ImagePartType.Jpeg;
68+
case "jpg":
69+
case "jpeg":
70+
return ImagePartType.Jpeg;
5071
case "pcx": return ImagePartType.Pcx;
5172
case "png": return ImagePartType.Png;
5273
case "svg": return ImagePartType.Svg;
@@ -59,16 +80,20 @@ internal PartTypeInfo GetImagePartType
5980
}
6081

6182
public byte[] Bytes { get; set; }
83+
6284
/// <summary>
63-
/// Unit is Pixel
85+
/// Unit is Pixel
6486
/// </summary>
65-
public Int64Value Width { get; set; } = 400;
66-
internal Int64Value Cx { get { return Width * 9525; } }
87+
public Int64Value Width { get; set; }
88+
89+
internal Int64Value Cx => Width * 9525;
90+
6791
/// <summary>
68-
/// Unit is Pixel
92+
/// Unit is Pixel
6993
/// </summary>
70-
public Int64Value Height { get; set; } = 400;
94+
public Int64Value Height { get; set; }
95+
7196
//format resource from http://openxmltrix.blogspot.com/2011/04/updating-images-in-image-placeholde-and.html
72-
internal Int64Value Cy { get { return Height * 9525; } }
97+
internal Int64Value Cy => Height * 9525;
7398
}
7499
}

tests/MiniWordTests/IssueTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,14 @@ public void TestIssue13()
263263
{ "sDate",DateTime.Parse("2022-09-08 08:30:00")},
264264
{ "eDate",DateTime.Parse("2022-09-08 15:00:00")},
265265
{ "How","Discussion requirement part1"},
266-
{ "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting02.png"), Width = 160, Height = 90 }},
266+
{ "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting02.png"), 160, 90)},
267267
},
268268
new Dictionary<string, object>
269269
{
270270
{ "sDate",DateTime.Parse("2022-09-09 08:30:00")},
271271
{ "eDate",DateTime.Parse("2022-09-09 17:00:00")},
272272
{ "How","Discussion requirement part2 and development"},
273-
{ "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting01.png"), Width = 160, Height = 90 }},
273+
{ "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting01.png"), 160, 90)},
274274
},
275275
}
276276
};
@@ -299,14 +299,14 @@ public void TestIssue13_new()
299299
{ "sDate",DateTime.Parse("2022-09-08 08:30:00")},
300300
{ "eDate",DateTime.Parse("2022-09-08 15:00:00")},
301301
{ "How","Discussion requirement part1"},
302-
{ "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting02.png"), Width = 160, Height = 90 }},
302+
{ "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting02.png"), 160, 90)},
303303
},
304304
new Dictionary<string, object>
305305
{
306306
{ "sDate",DateTime.Parse("2022-09-09 08:30:00")},
307307
{ "eDate",DateTime.Parse("2022-09-09 17:00:00")},
308308
{ "How","Discussion requirement part2 and development"},
309-
{ "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting01.png"), Width = 160, Height = 90 }},
309+
{ "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting01.png"), 160, 90)},
310310
},
311311
}
312312
};
@@ -466,7 +466,7 @@ Apple OS Interface Limited | From Jan 2008 to Feb 2010
466466
Lorem Ipsum has been the industry's standard dummy text
467467
ever since the 1500s, when an unknown printer took.
468468
",
469-
["Image"] = new MiniWordPicture() { Path = PathHelper.GetFile("demo01.png"), Width = 160, Height = 90 },
469+
["Image"] = new MiniWordPicture(PathHelper.GetFile("demo01.png"), 160, 90),
470470
};
471471
MiniWord.SaveAsByTemplate(path, templatePath, value);
472472
//System.Diagnostics.Process.Start("explorer.exe", path);
@@ -513,7 +513,7 @@ Apple OS Interface Limited | From Jan 2008 to Feb 2010
513513
Lorem Ipsum has been the industry's standard dummy text
514514
ever since the 1500s, when an unknown printer took.
515515
";
516-
value.Image = new MiniWordPicture() { Path = PathHelper.GetFile("demo01.png"), Width = 160, Height = 90 };
516+
value.Image = new MiniWordPicture(PathHelper.GetFile("demo01.png"), 160, 90);
517517
MiniWord.SaveAsByTemplate(path, templatePath, value);
518518
//System.Diagnostics.Process.Start("explorer.exe", path);
519519
}
@@ -595,7 +595,7 @@ public void TestIssue3()
595595
var templatePath = PathHelper.GetFile("TestBasicImage.docx");
596596
var value = new Dictionary<string, object>()
597597
{
598-
["Logo"] = new MiniWordPicture() { Path = PathHelper.GetFile("DemoLogo.png"), Width = 180, Height = 180 }
598+
["Logo"] = new MiniWordPicture(PathHelper.GetFile("DemoLogo.png"), 180, 180)
599599
};
600600
MiniWord.SaveAsByTemplate(path, templatePath, value);
601601
var xml = Helpers.GetZipFileContent(path, "word/document.xml");

tests/MiniWordTests/IssueTestsAsync.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,14 @@ public async Task TestIssue13()
264264
{ "sDate",DateTime.Parse("2022-09-08 08:30:00")},
265265
{ "eDate",DateTime.Parse("2022-09-08 15:00:00")},
266266
{ "How","Discussion requirement part1"},
267-
{ "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting02.png"), Width = 160, Height = 90 }},
267+
{ "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting02.png"), 160, 90)},
268268
},
269269
new Dictionary<string, object>
270270
{
271271
{ "sDate",DateTime.Parse("2022-09-09 08:30:00")},
272272
{ "eDate",DateTime.Parse("2022-09-09 17:00:00")},
273273
{ "How","Discussion requirement part2 and development"},
274-
{ "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting01.png"), Width = 160, Height = 90 }},
274+
{ "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting01.png"), 160, 90)},
275275
},
276276
}
277277
};
@@ -300,14 +300,14 @@ public async Task TestIssue13_new()
300300
{ "sDate",DateTime.Parse("2022-09-08 08:30:00")},
301301
{ "eDate",DateTime.Parse("2022-09-08 15:00:00")},
302302
{ "How","Discussion requirement part1"},
303-
{ "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting02.png"), Width = 160, Height = 90 }},
303+
{ "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting02.png"), 160, 90)},
304304
},
305305
new Dictionary<string, object>
306306
{
307307
{ "sDate",DateTime.Parse("2022-09-09 08:30:00")},
308308
{ "eDate",DateTime.Parse("2022-09-09 17:00:00")},
309309
{ "How","Discussion requirement part2 and development"},
310-
{ "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting01.png"), Width = 160, Height = 90 }},
310+
{ "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting01.png"), 160, 90)},
311311
},
312312
}
313313
};
@@ -467,7 +467,7 @@ Apple OS Interface Limited | From Jan 2008 to Feb 2010
467467
Lorem Ipsum has been the industry's standard dummy text
468468
ever since the 1500s, when an unknown printer took.
469469
",
470-
["Image"] = new MiniWordPicture() { Path = PathHelper.GetFile("demo01.png"), Width = 160, Height = 90 },
470+
["Image"] = new MiniWordPicture(PathHelper.GetFile("demo01.png"), 160, 90),
471471
};
472472
await MiniWord.SaveAsByTemplateAsync(path, templatePath, value);
473473
//System.Diagnostics.Process.Start("explorer.exe", path);
@@ -514,7 +514,7 @@ Apple OS Interface Limited | From Jan 2008 to Feb 2010
514514
Lorem Ipsum has been the industry's standard dummy text
515515
ever since the 1500s, when an unknown printer took.
516516
";
517-
value.Image = new MiniWordPicture() { Path = PathHelper.GetFile("demo01.png"), Width = 160, Height = 90 };
517+
value.Image = new MiniWordPicture(PathHelper.GetFile("demo01.png"), 160, 90);
518518
await MiniWord.SaveAsByTemplateAsync(path, templatePath, value);
519519
//System.Diagnostics.Process.Start("explorer.exe", path);
520520
}
@@ -596,7 +596,7 @@ public async Task TestIssue3()
596596
var templatePath = PathHelper.GetFile("TestBasicImage.docx");
597597
var value = new Dictionary<string, object>()
598598
{
599-
["Logo"] = new MiniWordPicture() { Path = PathHelper.GetFile("DemoLogo.png"), Width = 180, Height = 180 }
599+
["Logo"] = new MiniWordPicture(PathHelper.GetFile("DemoLogo.png"), 180, 180)
600600
};
601601
await MiniWord.SaveAsByTemplateAsync(path, templatePath, value);
602602
var xml = Helpers.GetZipFileContent(path, "word/document.xml");

0 commit comments

Comments
 (0)