|
2 | 2 | using Syncfusion.DocIO; |
3 | 3 | using System.Net; |
4 | 4 |
|
| 5 | +//Register Syncfusion license |
| 6 | +Syncfusion.Licensing.SyncfusionLicenseProvider.RegisterLicense("Mgo+DSMBMAY9C3t2UlhhQlNHfV5DQmBWfFN0QXNYfVRwdF9GYEwgOX1dQl9nSXZTc0VlWndfcXNSQWc="); |
| 7 | + |
5 | 8 | using (WordDocument document = new WordDocument()) |
6 | 9 | { |
7 | | - // Register the event to customize images while importing Markdown. |
| 10 | + // Register an event to customize images while importing Markdown. |
8 | 11 | document.MdImportSettings.ImageNodeVisited += MdImportSettings_ImageNodeVisited; |
9 | | - // Open the input Markdown file for reading. |
10 | | - using (FileStream inputFileStream = new FileStream(@"Data/Input.md", FileMode.Open, FileAccess.Read)) |
| 12 | + // Open the input Markdown file. |
| 13 | + using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Input.md"), FileMode.Open, FileAccess.Read)) |
11 | 14 | { |
| 15 | + // Load the Markdown file into the Word document. |
12 | 16 | document.Open(inputFileStream, FormatType.Markdown); |
13 | | - #region ImageResize |
14 | | - // Find all images with the alternative text "File" and resize them to 300x300. |
15 | | - List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, "AlternativeText", "Adventure"); |
| 17 | + |
| 18 | + // Find all images with the alternative text "Mountain" and resize them. |
| 19 | + List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, "AlternativeText", "Mountain"); |
16 | 20 | foreach (WPicture picture in pictures) |
17 | 21 | { |
18 | | - picture.Height = 300; |
19 | | - picture.Width = 300; |
| 22 | + picture.Height = 250; |
| 23 | + picture.Width = 250; |
20 | 24 | } |
21 | | - #endregion |
| 25 | + |
22 | 26 | // Save the modified document. |
23 | | - using (FileStream outputFileStream = new FileStream(@"Output/Result.docx", FileMode.Create, FileAccess.Write)) |
| 27 | + using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write)) |
24 | 28 | { |
25 | 29 | document.Save(outputFileStream, FormatType.Docx); |
26 | 30 | } |
27 | 31 | } |
28 | 32 | } |
29 | 33 |
|
| 34 | +/// <summary> |
| 35 | +/// Customizes image loading from local and remote sources during Markdown import. |
| 36 | +/// </summary> |
30 | 37 | static void MdImportSettings_ImageNodeVisited(object sender, Syncfusion.Office.Markdown.MdImageNodeVisitedEventArgs args) |
31 | 38 | { |
32 | | - // Set the image stream based on the image name from the Markdown input. |
33 | | - if (args.Uri == "Image_1.png") |
34 | | - args.ImageStream = new FileStream(@"Data/Image_1.png", FileMode.Open); |
35 | | - else if (args.Uri == "Image_2.png") |
36 | | - args.ImageStream = new FileStream(@"Data/Image_2.png", FileMode.Open); |
37 | | - // If the image is from a URL, download and set it as a stream. |
| 39 | + // Load a specific image from a local path if the image URI matches. |
| 40 | + if (args.Uri == "Road-550.png") |
| 41 | + args.ImageStream = new FileStream(Path.GetFullPath(@"Data/Road-550.png"), FileMode.Open); |
| 42 | + // If the image URI starts with "https://", download and set the image from the URL. |
38 | 43 | else if (args.Uri.StartsWith("https://")) |
39 | 44 | { |
40 | | - // Create a WebClient instance. |
| 45 | + // Initialize a WebClient instance for downloading. |
41 | 46 | WebClient client = new WebClient(); |
42 | | - // Download the image as byte data. |
| 47 | + // Download the image data as a byte array. |
43 | 48 | byte[] image = client.DownloadData(args.Uri); |
44 | | - // Convert byte data to a memory stream. |
| 49 | + // Convert the byte array to a memory stream. |
45 | 50 | Stream stream = new MemoryStream(image); |
46 | | - // Set the stream for the image in Markdown. |
| 51 | + // Set the downloaded stream as the image in the Markdown. |
47 | 52 | args.ImageStream = stream; |
48 | 53 | } |
49 | 54 | } |
0 commit comments