嫩草视频 两个人的免费视频完整版 午夜视频 好大哥dgdg 骚虎视频 嫩草视频 嫩草视频 骚虎视频 影视大全免费追剧 两个人的免费视频完整版 影视大全 嫩草官网 啦啦啦免费观看视频6 影视大全高清版 野花在线观看免费视频 嫩草视频 骚虎视频入口 免费无限资源 好大哥dgdg 啦啦啦免费观看视频6 啦啦啦免费观看视频6 两个人的免费视频完整版 啦啦啦免费观看视频6 骚虎视频入口 影视大全 啦啦啦免费观看视频6 嫩草视频 野花在线观看免费视频 啦啦啦免费观看视频6 影视大全 影视大全 骚虎视频入口 影视大全免费追剧 24小时日本在线视频 嫩草官网 影视大全免费追剧

Extract images from word document using OpenXML


This resource is about to extract images from word document using OpenXML. File extension should be .docx. DOCX means XML Documents which helps you to read files easily and perform any action on the system because it is light weight.

Purpose : To extract images from word document (.docx) without word installed on system

Requirements : Download Document.OpenXML from Nuget Packages. Find the below screen shot for reference.
Download Document.OpenXML from Nuget Packages

Step 1 : Create one console application or class library

Step 2 : Write the below code in class file
Namespaces

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System.IO;


Main method
static void Main(string[] args)
{
ExtractImages(@"E:\Nirav\Testing\abcd.docx", @"E:\Nirav\Testing\Output", "jpg");
}

Function to extract images
public static int ExtractImages(string InputFilePath, string OutputPath, string OutputImageFileExtension)
{
int SavedImageCount = 0;
try
{
if (OutputImageFileExtension.Length <= 1)
{
throw new Exception("Output image file extension cannot be blank.");
}
else
{
if (!OutputImageFileExtension.StartsWith("."))
OutputImageFileExtension = "." + OutputImageFileExtension;

if (!File.Exists(InputFilePath))
throw new Exception("Input file doesn't exists on the system.");
else if (!Directory.Exists(OutputPath))
throw new Exception("Output folder doesn't exists on the system.");
else
{
WordprocessingDocument doc = WordprocessingDocument.Open(InputFilePath, false);
Body body = doc.MainDocumentPart.Document.Body;
IEnumerable drawings = doc.MainDocumentPart.Document.Descendants().ToList();

foreach (Drawing draw in drawings)
{
foreach (DocumentFormat.OpenXml.Drawing.Blip b in draw.Descendants().ToList())
{
ImagePart imgPart = (ImagePart)doc.MainDocumentPart.GetPartById(b.Embed);
using (System.Drawing.Image saveImage = System.Drawing.Bitmap.FromStream(imgPart.GetStream()))
{
int Cnt = 1;
while (File.Exists(OutputPath + "\\" + "img_" + Cnt.ToString("D3") + OutputImageFileExtension))
Cnt++;

string ImgFileName = "img_" + Cnt.ToString("D3") + OutputImageFileExtension;
saveImage.Save(OutputPath + "\\" + ImgFileName, System.Drawing.Imaging.ImageFormat.Png);
SavedImageCount++;
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("An error occurred while extracting images from word file: " + ex.Message);
}
return SavedImageCount;
}


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email:
    好大哥影视-好大哥视频-好大哥官网-好大哥电影 骚虎视频官方-房事视频免费看 四虎视频官方-女人本色高清视频 午夜视频官方-24小时日本在线视频 好大哥视频-好大哥dgdg 野花在线官方-影视大全 久草视频入口-嫩草视频官网 小草视频入口-两个人观看野花免费视频 嫩草视频入口-大片免费视频