Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/MiniWord/Common/Enums/Extension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace MiniSoftware.Common.Enums
{
public enum Extension
{
Bmp,
Emf,
Icon,
Jpeg,
Pcx,
Png,
Svg,
Tiff,
Wmf
}
}
10 changes: 10 additions & 0 deletions src/MiniWord/Extensions/StringExtension.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
71 changes: 48 additions & 23 deletions src/MiniWord/MiniWordPicture.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -59,16 +80,20 @@ internal PartTypeInfo GetImagePartType
}

public byte[] Bytes { get; set; }

/// <summary>
/// Unit is Pixel
/// Unit is Pixel
/// </summary>
public Int64Value Width { get; set; } = 400;
internal Int64Value Cx { get { return Width * 9525; } }
public Int64Value Width { get; set; }

internal Int64Value Cx => Width * 9525;

/// <summary>
/// Unit is Pixel
/// Unit is Pixel
/// </summary>
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;
}
}
14 changes: 7 additions & 7 deletions tests/MiniWordTests/IssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>
{
{ "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)},
},
}
};
Expand Down Expand Up @@ -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<string, object>
{
{ "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)},
},
}
};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -595,7 +595,7 @@ public void TestIssue3()
var templatePath = PathHelper.GetFile("TestBasicImage.docx");
var value = new Dictionary<string, object>()
{
["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");
Expand Down
14 changes: 7 additions & 7 deletions tests/MiniWordTests/IssueTestsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, object>
{
{ "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)},
},
}
};
Expand Down Expand Up @@ -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<string, object>
{
{ "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)},
},
}
};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -596,7 +596,7 @@ public async Task TestIssue3()
var templatePath = PathHelper.GetFile("TestBasicImage.docx");
var value = new Dictionary<string, object>()
{
["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");
Expand Down
Loading