0

所以我想出了如何使用这个链接用word中的文字替换图片

但现在我需要将图片从 word 导出到文件夹。而且我猜每当我找到一个对象的图片(type s=Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)我应该用它做点什么......但我找不到选项:s。 saveAsPicture(@"C:\pic.jpg");

4

1 回答 1

5

您的问题可能与以下内容重复:extract image from word file

但是,根据我之前对您的问题的回答,即如何以编程方式将外部图像与 Word 中的内联形状进行比较(请参阅比较 Word 文件和文件夹中的图片?) - 您可以进行一些简单的调整并使用几乎完全相同的将每个内联形状导出到文件夹而不是将形状与另一个图像进行比较的相同示例代码。

为了说明,我为您做了必要的调整,并提供了下面的源代码。同样,该应用程序是基于 .NET 4.5、Microsoft Office 对象库版本 15.0 和 Microsoft Word 对象库版本 15.0 的 C# 控制台应用程序。

和以前一样,我在源代码中包含了引用,这样您就可以看到我的解决方案基于哪些来源(并且这些来源得到了他们应得的荣誉;))

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Application = Microsoft.Office.Interop.Word.Application;

namespace WordDocStats
{
    class Program
    {
        // General idea is based on: https://stackoverflow.com/a/7937590/700926
        static void Main()
        {
            // Open a doc file
            var wordApplication = new Application();
            var document = wordApplication.Documents.Open(@"C:\Users\Username\Documents\document.docx");

            // For each inline shape, export it to a file
            // By inspection you can see that the first inline shape have index 1 ( and not zero as one might expect )
            for (var i = 1; i <= wordApplication.ActiveDocument.InlineShapes.Count; i++)
            {
                // closure
                // http://confluence.jetbrains.net/display/ReSharper/Access+to+modified+closure
                var inlineShapeId = i; 

                // parameterized thread start
                // https://stackoverflow.com/a/1195915/700926
                var thread = new Thread(() => SaveInlineShapeToFile(inlineShapeId, wordApplication));

                // STA is needed in order to access the clipboard
                // https://stackoverflow.com/a/518724/700926
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
                thread.Join();
            }

            // Close word
            wordApplication.Quit();
            Console.ReadLine();
        }

        // General idea is based on: https://stackoverflow.com/a/7937590/700926
        protected static void SaveInlineShapeToFile(int inlineShapeId, Application wordApplication)
        {
            // Get the shape, select, and copy it to the clipboard
            var inlineShape = wordApplication.ActiveDocument.InlineShapes[inlineShapeId];
            inlineShape.Select();
            wordApplication.Selection.Copy();

            // Check data is in the clipboard
            if (Clipboard.GetDataObject() != null)
            {
                var data = Clipboard.GetDataObject();

                // Check if the data conforms to a bitmap format
                if (data != null && data.GetDataPresent(DataFormats.Bitmap))
                {
                    // Fetch the image and convert it to a Bitmap
                    var image = (Image) data.GetData(DataFormats.Bitmap, true);
                    var currentBitmap = new Bitmap(image);

                    // Save the bitmap to a file
                    currentBitmap.Save(@"C:\Users\Username\Documents\" + String.Format("img_{0}.png", inlineShapeId));
                }
            }
        }
    }
}
于 2012-09-16T18:22:37.850 回答