Skip to content

Commit 308035f

Browse files
committed
- auto get image extension name #10
- Image custom width and height
1 parent 129de2b commit 308035f

4 files changed

Lines changed: 61 additions & 10 deletions

File tree

samples/docx/TestBasicImage.png

53.2 KB
Loading

src/MiniWord/MiniWord.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,14 @@ static void ReplaceTag(this OpenXmlElement xmlElement, WordprocessingDocument do
5151
}
5252

5353
var mainPart = docx.MainDocumentPart;
54-
var imagePart = mainPart.AddImagePart(ImagePartType.Png);//TODO: jpg..
54+
55+
var imagePart = mainPart.AddImagePart(pic.GetImagePartType);
5556
using (var stream = new MemoryStream(l_Data))
5657
{
5758
imagePart.FeedData(stream);
5859
AddPicture(run, mainPart.GetIdOfPart(imagePart), pic);
59-
}
60+
61+
}
6062
t.Remove();
6163
}
6264
else
@@ -76,7 +78,7 @@ private static void AddPicture(OpenXmlElement appendElement, string relationship
7678
var element =
7779
new Drawing(
7880
new DW.Inline(
79-
new DW.Extent() { Cx = pic.Width, Cy = pic.Height },
81+
new DW.Extent() { Cx = pic.Cx, Cy = pic.Cy },
8082
new DW.EffectExtent()
8183
{
8284
LeftEdge = 0L,
@@ -98,7 +100,7 @@ private static void AddPicture(OpenXmlElement appendElement, string relationship
98100
new PIC.NonVisualDrawingProperties()
99101
{
100102
Id = (UInt32Value)0U,
101-
Name = $"New Bitmap Image{Guid.NewGuid().ToString()}.png"
103+
Name = $"Image {Guid.NewGuid().ToString()}.{pic.Extension}"
102104
},
103105
new PIC.NonVisualPictureDrawingProperties()),
104106
new PIC.BlipFill(
@@ -120,7 +122,7 @@ private static void AddPicture(OpenXmlElement appendElement, string relationship
120122
new PIC.ShapeProperties(
121123
new A.Transform2D(
122124
new A.Offset() { X = 0L, Y = 0L },
123-
new A.Extents() { Cx = pic.Width, Cy = pic.Height }),
125+
new A.Extents() { Cx = pic.Cx, Cy = pic.Cy }),
124126
new A.PresetGeometry(
125127
new A.AdjustValueList()
126128
)

src/MiniWord/MiniWordPicture.cs

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,62 @@
11
namespace MiniSoftware
22
{
33
using DocumentFormat.OpenXml;
4+
using DocumentFormat.OpenXml.Packaging;
5+
using System;
6+
using System.Collections.Generic;
47

58
public class MiniWordPicture
69
{
10+
11+
712
public string Path { get; set; }
8-
public byte[] Bytes { get; set; }
9-
public Int64Value Width { get; set; } = 990000L;
13+
private string _extension;
14+
public string Extension
15+
{
16+
get
17+
{
18+
if (Path != null)
19+
return System.IO.Path.GetExtension(Path).ToUpperInvariant().Replace(".", "");
20+
else
21+
{
22+
return _extension.ToUpper();
23+
}
24+
}
25+
set { _extension = value; }
26+
}
27+
internal ImagePartType GetImagePartType
28+
{
29+
get
30+
{
31+
switch (Extension.ToLower())
32+
{
33+
case "bmp": return ImagePartType.Bmp;
34+
case "emf": return ImagePartType.Emf;
35+
case "ico": return ImagePartType.Icon;
36+
case "jpg": return ImagePartType.Jpeg;
37+
case "jpeg": return ImagePartType.Jpeg;
38+
case "pcx": return ImagePartType.Pcx;
39+
case "png": return ImagePartType.Png;
40+
case "svg": return ImagePartType.Svg;
41+
case "tiff": return ImagePartType.Tiff;
42+
case "wmf": return ImagePartType.Wmf;
43+
default:
44+
throw new NotSupportedException($"{_extension} is not supported");
45+
}
46+
}
47+
}
1048

11-
public Int64Value Height { get; set; } = 792000L;
12-
}
49+
public byte[] Bytes { get; set; }
50+
/// <summary>
51+
/// Unit is Pixel
52+
/// </summary>
53+
public Int64Value Width { get; set; } = 400;
54+
internal Int64Value Cx { get { return Width * 9525; } }
55+
/// <summary>
56+
/// Unit is Pixel
57+
/// </summary>
58+
public Int64Value Height { get; set; } = 400;
59+
//format resource from http://openxmltrix.blogspot.com/2011/04/updating-images-in-image-placeholde-and.html
60+
internal Int64Value Cy { get { return Height * 9525; } }
61+
}
1362
}

tests/MiniWordTests/IssueTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void TestIssue3()
2929
["VIP"] = true,
3030
["Points"] = 123,
3131
["APP"] = "Demo APP",
32-
["Logo"] = new MiniWordPicture() { Path= @"D:\git\MiniWord\src\MiniWord\icon.png" ,Width= 392000L, Height= 392000L }
32+
["Logo"] = new MiniWordPicture() { Path= PathHelper.GetFile("TestBasicImage.png"), Width= 400, Height= 400 }
3333
};
3434
MiniWord.SaveAsByTemplate(path, templatePath, value);
3535
//Console.WriteLine(path);

0 commit comments

Comments
 (0)