Skip to content

Instantly share code, notes, and snippets.

@fileformat-words-gists
Last active December 16, 2023 17:52
Show Gist options
  • Save fileformat-words-gists/ad5c621c8764eb69555f2ab339f3ea01 to your computer and use it in GitHub Desktop.
Save fileformat-words-gists/ad5c621c8764eb69555f2ab339f3ea01 to your computer and use it in GitHub Desktop.
C# Read Word Document Images
/// <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