0

だから私はこのリンクを使用して写真を単語のテキストに置き換える方法を理解しました

しかし今、私は写真を単語からフォルダにエクスポートする必要があります。そして、オブジェクトである画像を見つけるたびに推測しています(タイプs = Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)それを使って何かをする必要があります...しかし、オプションが見つかりません:s。 saveAsPicture(@ "C:\ pic.jpg");

4

1 に答える 1

5

あなたの質問は次のような重複の可能性があります:Wordファイルから画像を抽出する

ただし、 Wordで外部画像をインライン形状とプログラムで比較する方法に関する私の以前の回答に基づいて( Wordファイルとフォルダー内の画像の比較を参照してください)-いくつかの簡単な調整を行い、ほぼ正確に使用できます形状を別の画像と比較する代わりに、各インライン形状をフォルダーにエクスポートするための同じサンプルコード。

説明のために、必要な調整を行い、以下のソースコードを提供しました。この場合も、アプリケーションは.NET 4.5、Microsoft Office Object Libraryバージョン15.0、およびMicrosoft Word Object Libraryバージョン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 に答える