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