Skip to content

Commit f9cc064

Browse files
Added the sample for change the image size during markdown to word conversion
1 parent 99a31d7 commit f9cc064

File tree

7 files changed

+118
-0
lines changed

7 files changed

+118
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35309.182
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Modify_image_size", "Modify_image_size\Modify_image_size.csproj", "{280B67DF-9185-42E5-9498-D12BAC8BAE2F}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{280B67DF-9185-42E5-9498-D12BAC8BAE2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{280B67DF-9185-42E5-9498-D12BAC8BAE2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{280B67DF-9185-42E5-9498-D12BAC8BAE2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{280B67DF-9185-42E5-9498-D12BAC8BAE2F}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {BF05181A-BA9E-449A-9395-3A211A69123B}
24+
EndGlobalSection
25+
EndGlobal
10.4 KB
Loading
42.5 KB
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Image from file:
2+
3+
![File](Image_2.png)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.DocIO.NET" Version="*" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="Data\Adventure-work-cycles.png">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Data\Cycle.png">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
<None Update="Data\Image_1.png">
22+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
23+
</None>
24+
<None Update="Data\Image_2.png">
25+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
26+
</None>
27+
<None Update="Data\Input.md">
28+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
29+
</None>
30+
<None Update="Output\.gitkeep">
31+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
32+
</None>
33+
</ItemGroup>
34+
35+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
using System.Net;
4+
5+
ResizeImageUsingAltText();
6+
7+
static void ResizeImageUsingAltText()
8+
{
9+
using (WordDocument document = new WordDocument())
10+
{
11+
// Register the event to customize images while importing Markdown.
12+
document.MdImportSettings.ImageNodeVisited += MdImportSettings_ImageNodeVisited;
13+
// Open the input Markdown file for reading.
14+
using (FileStream inputFileStream = new FileStream(@"Data/Input.md", FileMode.Open, FileAccess.Read))
15+
{
16+
document.Open(inputFileStream, FormatType.Markdown);
17+
#region ImageResize
18+
// Find all images with the alternative text "File" and resize them to 300x300.
19+
List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, "AlternativeText", "File");
20+
foreach (WPicture picture in pictures)
21+
{
22+
picture.Height = 284;
23+
picture.Width = 442;
24+
}
25+
#endregion
26+
// Save the modified document.
27+
using (FileStream outputFileStream = new FileStream(@"Output/Result.docx", FileMode.Create, FileAccess.Write))
28+
{
29+
document.Save(outputFileStream, FormatType.Docx);
30+
}
31+
}
32+
}
33+
}
34+
35+
static void MdImportSettings_ImageNodeVisited(object sender, Syncfusion.Office.Markdown.MdImageNodeVisitedEventArgs args)
36+
{
37+
// Set the image stream based on the image name from the Markdown input.
38+
if (args.Uri == "Image_1.png")
39+
args.ImageStream = new FileStream(@"Data/Image_1.png", FileMode.Open);
40+
else if (args.Uri == "Image_2.png")
41+
args.ImageStream = new FileStream(@"Data/Image_2.png", FileMode.Open);
42+
// If the image is from a URL, download and set it as a stream.
43+
else if (args.Uri.StartsWith("https://"))
44+
{
45+
// Create a WebClient instance.
46+
WebClient client = new WebClient();
47+
// Download the image as byte data.
48+
byte[] image = client.DownloadData(args.Uri);
49+
// Convert byte data to a memory stream.
50+
Stream stream = new MemoryStream(image);
51+
// Set the stream for the image in Markdown.
52+
args.ImageStream = stream;
53+
}
54+
}

0 commit comments

Comments
 (0)