- [TOOL] Chia sẻ phần mềm thay đổi thông tin cấu hình máy tính
- [C#] Hướng dẫn Export dữ liệu ra file Microsoft Word Template
- [C#] Chia sẻ source code tool kiểm tra domain website
- [C#] Hướng dẫn tạo file PDF sử dụng thư viện QuestPDF
- [C#] Hướng dẫn tạo ứng dụng dock windows giống Taskbar
- [C#] Chia sẻ source code sử dụng Object Listview trên Winform
- [VB.NET] Chia sẻ source code quản lý thu chi mô hình 3 lớp Winform
- [DATABASE] Xóa lịch sử danh sách đăng nhập tài khoản trên SMSS Sqlserver Management Studio
- [C#] Sử dụng FolderBrowserDialog Vista trên Winform
- [DEVEXPRESS] Chia sẻ tool Winform UI Templates Early Access Preview (EAP)
- [C#] Chia sẻ source code Spin Content (Trộn nội dung văn bản theo từ đồng nghĩa) trên Winform
- [VB.NET] Chia sẻ source code lịch âm dương và hẹn lịch nhắc việc
- [C#] Hướng dẫn đọc thông số thiết bị Thiết bị kiểm tra Pin (HIOKI BATTERY HiTESTER BT3562)
- [VB.NET] Hướng dẫn giải captcha sử dụng dịch vụ AZCaptcha API trên winform
- [C#] Hướng dẫn chứng thực đăng nhập ứng dụng bằng vân tay (Finger Print) trên máy tính
- [C#] Color Thief cách xuất màu sắc thiết kế từ hình ảnh
- [C#] Cách tạo bản quyền và cho phép dùng thử ứng dụng Winform
- [C#] Hướng dẫn sử dụng trình duyệt web Chrome convert HTML sang tập tin file PDF
- [C#] Kết nôi điện thoại Android, IOS với App Winform via Bluetooth
- [DATABASE] Cách query cộng trừ dồn dần trong Sqlserver
[C#] Xuất tất cả hình ảnh từ file Microsoft Word
Xin chào các bạn, bài viết hôm nay mình tiếp tục hướng dẫn các bạn cách lấy tất cả hình ảnh trong File Document Microsoft Word bằng ngôn ngữ lập trình C#, Winform.
[C#] How to Extract all Image from Microsoft Word
Bình thường các bạn, có thể lấy hình ảnh trong file word bằng cách.
Bước 1: Đổi tên file *.docx
=> file nén *.zip
Bước 2: Giải nén file đó ra
Bước 3: Các bạn vào trong thư mục giải nén sẽ tất cả hình ảnh nằm trong thư mục media.
Tuy nhiên, đó là cách lấy hình ảnh bằng tay, nếu nhiều file word chúng ta muốn lấy hình ảnh, thì các bạn lập trình theo bài viết này.
Đầu tiên, các bạn cần cài thư viện Microsoft.Office.Interop.Word vào từ Nuget
PM> NuGet\Install-Package Microsoft.Office.Interop.Word -Version 15.0.4797.1004
Video demo ứng dụng:
Full source code C#:
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace ExtractImageFromMicrosoftWord
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnBrowse_Click(object sender, EventArgs e)
{
var dlg = new OpenFileDialog();
dlg.Title = "Open Word file";
dlg.Filter = "Document files (*.docx)|*.docx";
dlg.Multiselect = true;
if (dlg.ShowDialog() == DialogResult.OK)
{
flowLayoutPanel1.Controls.Clear();
FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
Body body = null;
MainDocumentPart mainPart = null;
using (WordprocessingDocument wdDoc = WordprocessingDocument.Open(fs, false))
{
mainPart = wdDoc.MainDocumentPart;
body = wdDoc.MainDocumentPart.Document.Body;
if (body != null)
{
var listImage = ExtractImages(body, mainPart);
const int margin = 20;
int x = margin;
int y = 4;
foreach (var item in listImage)
{
var pic = new DevExpress.XtraEditors.PictureEdit();
pic.Location = new System.Drawing.Point(x, y);
pic.Size = new Size(200, 150);
pic.Properties.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
pic.Image = item;
pic.BorderStyle = BorderStyles.Style3D;
pic.Parent = flowLayoutPanel1;
x += pic.Width;
}
}
}
fs.Flush();
fs.Close();
}
}
private List<Image> ExtractImages(Body content, MainDocumentPart wDoc)
{
var imageList = new List<Image>();
foreach (Paragraph par in content.Descendants<Paragraph>())
{
ParagraphProperties paragraphProperties = par.ParagraphProperties;
foreach (Run run in par.Descendants<Run>())
{
Drawing image = run.Descendants<Drawing>().FirstOrDefault();
if (image != null)
{
var imageFirst = image?.Inline?.Graphic?.GraphicData.Descendants<DocumentFormat.OpenXml.Drawing.Pictures.Picture>().FirstOrDefault();
if (imageFirst != null)
{
var blip = imageFirst.BlipFill.Blip.Embed.Value;
ImagePart img = (ImagePart)wDoc.Document.MainDocumentPart.GetPartById(blip);
var imgItem = Bitmap.FromStream(img.GetStream());
imageList.Add(imgItem);
}
}
}
}
return imageList;
}
}
}
Thanks for watching!