Skip to content
This repository was archived by the owner on May 21, 2022. It is now read-only.

Commit 7369133

Browse files
committed
feat: finish add image from md
1 parent 14ea158 commit 7369133

File tree

3 files changed

+94
-25
lines changed

3 files changed

+94
-25
lines changed

md2docx/GeneratedCode.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,7 +2059,7 @@ public static void GenerateFontTablePartContent(FontTablePart fontTablePart1)
20592059
fontTablePart1.Fonts = fonts1;
20602060
}
20612061

2062-
public static Run GenerateImageReference(int id)
2062+
public static Run GenerateImageReference(int id, long width, long height)
20632063
{
20642064
Run run1 = new Run();
20652065

@@ -2070,10 +2070,10 @@ public static Run GenerateImageReference(int id)
20702070

20712071
Drawing drawing1 = new Drawing();
20722072

2073-
Wp.Inline inline1 = new Wp.Inline() { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U, AnchorId = "06C249DF", EditId = "66989579" };
2074-
Wp.Extent extent1 = new Wp.Extent() { Cx = 1104900L, Cy = 1047750L };
2073+
Wp.Inline inline1 = new Wp.Inline() { DistanceFromTop = 0U, DistanceFromBottom = 0U, DistanceFromLeft = 0U, DistanceFromRight = 0U };
2074+
Wp.Extent extent1 = new Wp.Extent() { Cx = width, Cy = height };
20752075
Wp.EffectExtent effectExtent1 = new Wp.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L };
2076-
Wp.DocProperties docProperties1 = new Wp.DocProperties() { Id = (UInt32Value)1U, Name = $"图片 {id}" };
2076+
Wp.DocProperties docProperties1 = new Wp.DocProperties() { Id = 1U, Name = $"图片 {id}" };
20772077

20782078
Wp.NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties1 = new Wp.NonVisualGraphicFrameDrawingProperties();
20792079

@@ -2112,7 +2112,7 @@ public static Run GenerateImageReference(int id)
21122112

21132113
A.Transform2D transform2D1 = new A.Transform2D();
21142114
A.Offset offset1 = new A.Offset() { X = 0L, Y = 0L };
2115-
A.Extents extents1 = new A.Extents() { Cx = 1104900L, Cy = 1047750L };
2115+
A.Extents extents1 = new A.Extents() { Cx = width, Cy = height };
21162116

21172117
transform2D1.Append(offset1);
21182118
transform2D1.Append(extents1);

md2docx/ImageGetter.cs

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
4-
using System.Text;
53
using System.Drawing;
6-
using System.Threading.Tasks;
74
using System.IO;
85
using System.Net;
6+
using System.Drawing.Imaging;
7+
using System.Collections.Generic;
98

109
namespace md2docx
1110
{
@@ -15,8 +14,19 @@ namespace md2docx
1514
public class ImageGetter
1615
{
1716
public byte[] ImageData { get; set; }
18-
public int Height { get; set; }
19-
public int Width { get; set; }
17+
public long Height { get; set; }
18+
public long Width { get; set; }
19+
public string Type { get; set; }
20+
private readonly static string DefaultType = "jpeg";
21+
private readonly static long DefaultHeight = 846000L;
22+
private readonly static long DefaultWidth = 1724400L;
23+
private readonly static long ratio = 914400L;
24+
private readonly static Dictionary<ImageFormat, string> typeDictionary = new Dictionary<ImageFormat, string>
25+
{
26+
{ ImageFormat.Png, "png" },
27+
{ ImageFormat.Jpeg, "jpeg" },
28+
{ ImageFormat.Gif, "gif" }
29+
};
2030

2131
public ImageGetter() { }
2232

@@ -47,37 +57,55 @@ public bool Load(string origin)
4757
}
4858
}
4959

50-
private bool GetImageFromPath(string path)
60+
private void GetImageFromPath(string path)
5161
{
52-
Image img = Image.FromFile(path);
53-
ImageData = File.ReadAllBytes(path);
54-
Width = img.Width;
55-
Height = img.Height;
56-
return true;
62+
Image image = Image.FromFile(path);
63+
Width = (long)(image.Width / image.HorizontalResolution * ratio);
64+
Height = (long)(image.Height / image.HorizontalResolution * ratio);
65+
66+
DealFormat(ref image, File.ReadAllBytes(path));
5767
}
5868

59-
private bool GetImageFromUrl(string url)
69+
private void GetImageFromUrl(string url)
6070
{
6171
using (WebClient webClient = new WebClient())
6272
{
6373
byte[] data = webClient.DownloadData(url);
6474

6575
using (MemoryStream mem = new MemoryStream(data))
6676
{
67-
Image img = Image.FromStream(mem);
68-
Width = img.Width;
69-
Height = img.Height;
70-
ImageData = data.ToArray();
77+
Image image = Image.FromStream(mem);
78+
Width = (long)(image.Width / image.HorizontalResolution * ratio);
79+
Height = (long)(image.Height / image.HorizontalResolution * ratio);
80+
81+
DealFormat(ref image, data);
7182
}
7283
}
73-
return true;
84+
}
85+
86+
private void DealFormat(ref Image image, byte[] originalData)
87+
{
88+
89+
if (!typeDictionary.ContainsKey(image.RawFormat))
90+
{
91+
MemoryStream stream = new MemoryStream();
92+
image.Save(stream, ImageFormat.Png);
93+
Type = DefaultType;
94+
ImageData = stream.GetBuffer();
95+
}
96+
else
97+
{
98+
Type = typeDictionary[image.RawFormat];
99+
ImageData = originalData;
100+
}
74101
}
75102

76103
private void Fail()
77104
{
78105
ImageData = hexData;
79-
Width = 224;
80-
Height = 110;
106+
Width = DefaultWidth;
107+
Height = DefaultHeight;
108+
Type = DefaultType;
81109
}
82110

83111
#region

md2docx/md2docx.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using Microsoft.Toolkit.Parsers.Markdown;
34
using Microsoft.Toolkit.Parsers.Markdown.Blocks;
45
using DocumentFormat.OpenXml.Wordprocessing;
@@ -19,6 +20,11 @@ class Md2Docx
1920
static Dictionary<string, string> info = new Dictionary<string, string>();
2021
static Dictionary<string, string> correspondecs;
2122
static Dictionary<string, bool> optionalParts;
23+
static Dictionary<int, byte[]> imageDatas = new Dictionary<int, byte[]>();
24+
static Dictionary<int, string> imageType = new Dictionary<int, string>();
25+
static List<int> failImage = new List<int>();
26+
static int imageCount = 3;
27+
static bool hasFailImage = false;
2228

2329
/// <summary>
2430
/// Print usage without exit
@@ -111,6 +117,22 @@ private static void Main(string[] args)
111117
GeneratedCode.GenerateFontTablePartContent(fontTablePart1);
112118

113119
SetPackageProperties(document);
120+
121+
foreach(int index in imageType.Keys)
122+
{
123+
ImagePart imagePart = mainDocumentPart1.AddNewPart<ImagePart>($"image/{imageType[index]}", $"rId{index}");
124+
imagePart.FeedData(new MemoryStream(imageDatas[index]));
125+
}
126+
}
127+
128+
if (hasFailImage && !quiet)
129+
{
130+
Console.WriteLine("Some image failed to insert, please check and insert it manually. Their index are(count from 1):");
131+
foreach(int index in failImage)
132+
{
133+
Console.Write($"{index} ");
134+
}
135+
Console.WriteLine("\nThis warning can also be closed by -q.");
114136
}
115137
}
116138

@@ -248,7 +270,26 @@ private static void CovertMDInlines(RunProperties rp, IList<MarkdownInline> inli
248270
ParagraphStyleId = new ParagraphStyleId { Val = "Image Title" }
249271
}
250272
};
251-
Run run = new Run();
273+
ImageGetter image = new ImageGetter();
274+
if (!image.Load(img.Url))
275+
{
276+
failImage.Add(imageCount - 2);
277+
hasFailImage = true;
278+
}
279+
imageType.Add(imageCount, image.Type);
280+
imageDatas.Add(imageCount, image.ImageData);
281+
Run run = GeneratedCode.GenerateImageReference(imageCount, image.Width, image.Height);
282+
paragraph.Append(run);
283+
paragraphs.Add(paragraph);
284+
imageCount += 1;
285+
paragraph = new Paragraph
286+
{
287+
ParagraphProperties = new ParagraphProperties
288+
{
289+
ParagraphStyleId = new ParagraphStyleId { Val = "Image Title" }
290+
}
291+
};
292+
run = new Run();
252293
Text txt = new Text { Text = img.Tooltip, Space = SpaceProcessingModeValues.Preserve };
253294
run.Append(txt);
254295
paragraph.Append(run);

0 commit comments

Comments
 (0)