Last active
December 16, 2023 17:52
-
-
Save fileformat-words-gists/ad5c621c8764eb69555f2ab339f3ea01 to your computer and use it in GitHub Desktop.
C# Read Word Document Images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Loads a Word Document with structured content using | |
/// <a href="https://www.nuget.org/packages/FileFormat.Words">FileFormat.Words</a>. | |
/// Traverses images and displays image metadata. | |
/// </summary> | |
/// <param name="documentDirectory"> | |
/// The directory where the Word Document to load is present (default is root of your project). | |
/// </param> | |
/// <param name="filename"> | |
/// The name of the Word Document file to load (default is "WordImages.docx"). | |
/// </param> | |
public void ReadImagesInWordDocument(string documentDirectory = "../../../", string filename = "WordImages.docx") | |
{ | |
try | |
{ | |
// Load the Word Document | |
var doc = new FileFormat.Words.Document($"{documentDirectory}/{filename}"); | |
var body = new FileFormat.Words.Body(doc); | |
var num = 0; | |
// Traverse images and display metadata info | |
foreach (var img in body.Images) | |
{ | |
num++; | |
System.Console.WriteLine($" Image Number: {num}"); | |
System.Console.WriteLine($" Image Data Length: {img.ImageData.Length}"); | |
System.Console.WriteLine($" Image Height: {img.Height}"); | |
System.Console.WriteLine($" Image Width: {img.Width}"); | |
} | |
} | |
catch (System.Exception ex) | |
{ | |
throw new FileFormat.Words.FileFormatException("An error occurred.", ex); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment