diff --git a/src/MiniWord/Common/Enums/Extension.cs b/src/MiniWord/Common/Enums/Extension.cs new file mode 100644 index 0000000..061d1b1 --- /dev/null +++ b/src/MiniWord/Common/Enums/Extension.cs @@ -0,0 +1,15 @@ +namespace MiniSoftware.Common.Enums +{ + public enum Extension + { + Bmp, + Emf, + Icon, + Jpeg, + Pcx, + Png, + Svg, + Tiff, + Wmf + } +} \ No newline at end of file diff --git a/src/MiniWord/Extensions/StringExtension.cs b/src/MiniWord/Extensions/StringExtension.cs new file mode 100644 index 0000000..0ece7cb --- /dev/null +++ b/src/MiniWord/Extensions/StringExtension.cs @@ -0,0 +1,10 @@ +namespace MiniSoftware.Extensions +{ + public static class StringExtension + { + public static string FirstCharacterToLower(this string str) + { + return string.IsNullOrWhiteSpace(str) ? str : char.ToLowerInvariant(str[0]) + str.Substring(1); + } + } +} \ No newline at end of file diff --git a/src/MiniWord/MiniWordPicture.cs b/src/MiniWord/MiniWordPicture.cs index bcc561d..5609968 100644 --- a/src/MiniWord/MiniWordPicture.cs +++ b/src/MiniWord/MiniWordPicture.cs @@ -1,41 +1,61 @@ -namespace MiniSoftware -{ - using DocumentFormat.OpenXml; - using DocumentFormat.OpenXml.Packaging; - using System; - using System.Collections.Generic; +using System; +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Packaging; +using MiniSoftware.Common.Enums; +using MiniSoftware.Extensions; +namespace MiniSoftware +{ public enum MiniWordPictureWrappingType { Inline, Anchor } - + public class MiniWordPicture { - + public MiniWordPicture(string path, long width = 400, long height = 400) + { + Path = path; + Width = width; + Height= height; + } + + public MiniWordPicture(byte[] bytes, Extension extension, long width = 400, long height = 400) + { + Bytes = bytes; + Extension = extension.ToString().FirstCharacterToLower(); + + Width = width; + Height = height; + } + + private string _extension; + public MiniWordPictureWrappingType WrappingType { get; set; } = MiniWordPictureWrappingType.Inline; public bool BehindDoc { get; set; } = false; public bool AllowOverlap { get; set; } = false; public long HorizontalPositionOffset { get; set; } = 0; public long VerticalPositionOffset { get; set; } = 0; - + public string Path { get; set; } - private string _extension; + public string Extension { get { + if (!string.IsNullOrWhiteSpace(_extension)) + return _extension; + if (Path != null) return System.IO.Path.GetExtension(Path).ToUpperInvariant().Replace(".", ""); - else - { - return _extension.ToUpper(); - } + + return string.Empty; } - set { _extension = value; } + set => _extension = value; } + internal PartTypeInfo GetImagePartType { get @@ -45,8 +65,9 @@ internal PartTypeInfo GetImagePartType case "bmp": return ImagePartType.Bmp; case "emf": return ImagePartType.Emf; case "ico": return ImagePartType.Icon; - case "jpg": return ImagePartType.Jpeg; - case "jpeg": return ImagePartType.Jpeg; + case "jpg": + case "jpeg": + return ImagePartType.Jpeg; case "pcx": return ImagePartType.Pcx; case "png": return ImagePartType.Png; case "svg": return ImagePartType.Svg; @@ -59,16 +80,20 @@ internal PartTypeInfo GetImagePartType } public byte[] Bytes { get; set; } + /// - /// Unit is Pixel + /// Unit is Pixel /// - public Int64Value Width { get; set; } = 400; - internal Int64Value Cx { get { return Width * 9525; } } + public Int64Value Width { get; set; } + + internal Int64Value Cx => Width * 9525; + /// - /// Unit is Pixel + /// Unit is Pixel /// - public Int64Value Height { get; set; } = 400; + public Int64Value Height { get; set; } + //format resource from http://openxmltrix.blogspot.com/2011/04/updating-images-in-image-placeholde-and.html - internal Int64Value Cy { get { return Height * 9525; } } + internal Int64Value Cy => Height * 9525; } } \ No newline at end of file diff --git a/tests/MiniWordTests/IssueTests.cs b/tests/MiniWordTests/IssueTests.cs index 64f0935..c4acfab 100644 --- a/tests/MiniWordTests/IssueTests.cs +++ b/tests/MiniWordTests/IssueTests.cs @@ -263,14 +263,14 @@ public void TestIssue13() { "sDate",DateTime.Parse("2022-09-08 08:30:00")}, { "eDate",DateTime.Parse("2022-09-08 15:00:00")}, { "How","Discussion requirement part1"}, - { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting02.png"), Width = 160, Height = 90 }}, + { "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting02.png"), 160, 90)}, }, new Dictionary { { "sDate",DateTime.Parse("2022-09-09 08:30:00")}, { "eDate",DateTime.Parse("2022-09-09 17:00:00")}, { "How","Discussion requirement part2 and development"}, - { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting01.png"), Width = 160, Height = 90 }}, + { "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting01.png"), 160, 90)}, }, } }; @@ -299,14 +299,14 @@ public void TestIssue13_new() { "sDate",DateTime.Parse("2022-09-08 08:30:00")}, { "eDate",DateTime.Parse("2022-09-08 15:00:00")}, { "How","Discussion requirement part1"}, - { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting02.png"), Width = 160, Height = 90 }}, + { "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting02.png"), 160, 90)}, }, new Dictionary { { "sDate",DateTime.Parse("2022-09-09 08:30:00")}, { "eDate",DateTime.Parse("2022-09-09 17:00:00")}, { "How","Discussion requirement part2 and development"}, - { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting01.png"), Width = 160, Height = 90 }}, + { "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting01.png"), 160, 90)}, }, } }; @@ -466,7 +466,7 @@ Apple OS Interface Limited | From Jan 2008 to Feb 2010 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took. ", - ["Image"] = new MiniWordPicture() { Path = PathHelper.GetFile("demo01.png"), Width = 160, Height = 90 }, + ["Image"] = new MiniWordPicture(PathHelper.GetFile("demo01.png"), 160, 90), }; MiniWord.SaveAsByTemplate(path, templatePath, value); //System.Diagnostics.Process.Start("explorer.exe", path); @@ -513,7 +513,7 @@ Apple OS Interface Limited | From Jan 2008 to Feb 2010 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took. "; - value.Image = new MiniWordPicture() { Path = PathHelper.GetFile("demo01.png"), Width = 160, Height = 90 }; + value.Image = new MiniWordPicture(PathHelper.GetFile("demo01.png"), 160, 90); MiniWord.SaveAsByTemplate(path, templatePath, value); //System.Diagnostics.Process.Start("explorer.exe", path); } @@ -595,7 +595,7 @@ public void TestIssue3() var templatePath = PathHelper.GetFile("TestBasicImage.docx"); var value = new Dictionary() { - ["Logo"] = new MiniWordPicture() { Path = PathHelper.GetFile("DemoLogo.png"), Width = 180, Height = 180 } + ["Logo"] = new MiniWordPicture(PathHelper.GetFile("DemoLogo.png"), 180, 180) }; MiniWord.SaveAsByTemplate(path, templatePath, value); var xml = Helpers.GetZipFileContent(path, "word/document.xml"); diff --git a/tests/MiniWordTests/IssueTestsAsync.cs b/tests/MiniWordTests/IssueTestsAsync.cs index 1667da6..bd08d5a 100644 --- a/tests/MiniWordTests/IssueTestsAsync.cs +++ b/tests/MiniWordTests/IssueTestsAsync.cs @@ -264,14 +264,14 @@ public async Task TestIssue13() { "sDate",DateTime.Parse("2022-09-08 08:30:00")}, { "eDate",DateTime.Parse("2022-09-08 15:00:00")}, { "How","Discussion requirement part1"}, - { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting02.png"), Width = 160, Height = 90 }}, + { "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting02.png"), 160, 90)}, }, new Dictionary { { "sDate",DateTime.Parse("2022-09-09 08:30:00")}, { "eDate",DateTime.Parse("2022-09-09 17:00:00")}, { "How","Discussion requirement part2 and development"}, - { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting01.png"), Width = 160, Height = 90 }}, + { "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting01.png"), 160, 90)}, }, } }; @@ -300,14 +300,14 @@ public async Task TestIssue13_new() { "sDate",DateTime.Parse("2022-09-08 08:30:00")}, { "eDate",DateTime.Parse("2022-09-08 15:00:00")}, { "How","Discussion requirement part1"}, - { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting02.png"), Width = 160, Height = 90 }}, + { "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting02.png"), 160, 90)}, }, new Dictionary { { "sDate",DateTime.Parse("2022-09-09 08:30:00")}, { "eDate",DateTime.Parse("2022-09-09 17:00:00")}, { "How","Discussion requirement part2 and development"}, - { "Photo",new MiniWordPicture() { Path = PathHelper.GetFile("DemoExpenseMeeting01.png"), Width = 160, Height = 90 }}, + { "Photo",new MiniWordPicture(PathHelper.GetFile("DemoExpenseMeeting01.png"), 160, 90)}, }, } }; @@ -467,7 +467,7 @@ Apple OS Interface Limited | From Jan 2008 to Feb 2010 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took. ", - ["Image"] = new MiniWordPicture() { Path = PathHelper.GetFile("demo01.png"), Width = 160, Height = 90 }, + ["Image"] = new MiniWordPicture(PathHelper.GetFile("demo01.png"), 160, 90), }; await MiniWord.SaveAsByTemplateAsync(path, templatePath, value); //System.Diagnostics.Process.Start("explorer.exe", path); @@ -514,7 +514,7 @@ Apple OS Interface Limited | From Jan 2008 to Feb 2010 Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took. "; - value.Image = new MiniWordPicture() { Path = PathHelper.GetFile("demo01.png"), Width = 160, Height = 90 }; + value.Image = new MiniWordPicture(PathHelper.GetFile("demo01.png"), 160, 90); await MiniWord.SaveAsByTemplateAsync(path, templatePath, value); //System.Diagnostics.Process.Start("explorer.exe", path); } @@ -596,7 +596,7 @@ public async Task TestIssue3() var templatePath = PathHelper.GetFile("TestBasicImage.docx"); var value = new Dictionary() { - ["Logo"] = new MiniWordPicture() { Path = PathHelper.GetFile("DemoLogo.png"), Width = 180, Height = 180 } + ["Logo"] = new MiniWordPicture(PathHelper.GetFile("DemoLogo.png"), 180, 180) }; await MiniWord.SaveAsByTemplateAsync(path, templatePath, value); var xml = Helpers.GetZipFileContent(path, "word/document.xml");