NEWS

[C#] Xuất tất cả hình ảnh từ file Microsoft Word

[C#] Xuất tất cả hình ảnh từ file Microsoft Word
Đăng bởi: Thảo Meo - Lượt xem: 1839 15:32:04, 09/11/2022C#   In bài viết

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.

extract_image-csharp

Đầ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!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Xuất tất cả hình ảnh từ file Microsoft Word
Đăng bởi: Thảo Meo - Lượt xem: 1839 15:32:04, 09/11/2022C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.