【问题标题】:Find Pictures in a Word Document programmatically以编程方式在 Word 文档中查找图片
【发布时间】:2020-07-05 12:23:56
【问题描述】:

问题:-

在最近的一次品牌重塑工作中,所有的 word 文档(保存为 RTF 文件))

我有大小为 2、3 甚至 4MB 的 word 文档。

查看文档中的图片,我注意到有些缩小到 48%、70%、88% 等等。如果我从文档中剪切图像,将其粘贴到 Paint.NET,调整大小,将其粘贴回文档中并按照原件定位,我可以将文档的大小缩小到小于 1/10手动捏造的。

我想以编程方式处理 1150 个 Word 文档并在其中找到已缩放的图片。然后我想把图片拉出来,调整它们的大小,然后把它们放回去替换手动添加的图片。节省磁盘空间。

我在导航 Word 对象模型和以编程方式查找图片时遇到困难。

MSDN 上的这个网页说你可以添加它们,像这样

this.Application.Selection.InlineShapes.AddPicture(@"C:\SamplePicture.jpg");

所以我认为使用 InlineShapes 集合可以让我访问文档中的图片集合。

我已经声明了互操作。

using Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop;
using Microsoft.Office;

然后我打开 Word 应用程序,然后像这样记录(这可行)

private void OpenWordApplication()
{
    _WordApp = new Microsoft.Office.Interop.Word.Application();
    if (chkVisibleWord.CheckState == CheckState.Checked) {
        _WordApp.Visible = true;
    }
    else
    {
        _WordApp.Visible = false;
    }

}

private void OpenTheDocument(string DocumentPath)
{
    _WordDoc = _WordApp.Documents.Open(DocumentPath);
    changesMade = false;
}

当我尝试在 InlineShapes 中查找 eh 图片时,我似乎无法找到它们。

_WordApp.Selection.HomeKey(WdUnits.wdStory);

int picCount = _WordApp.ActiveDocument.InlineShapes.Count;
MessageBox.Show(string.Format("There are {0} images in this document", picCount));

我收到一个消息框说计数为零。

注意:应用程序可以在 Word 中正常打开文档。它根据我在表单上选中的复选框对文档执行其他操作,对我来说,问题似乎就在访问 InlineShapes 集合时。

任何指针。到目前为止,感谢您的关注?

提前致谢

【问题讨论】:

  • 你说你的代码 sn-p 没有给你你想要的。它给了你什么,它与你想要的有什么不同? Selectionstatements 与计数无关(内联形状集合不需要选择活动文档的文本)。如果你的Using 语句和建立_WordApp 的代码是正确的,你应该得到一个准确的计数。请发布其他代码,包括 Using 语句。
  • @joeschwa 我在我知道有图像的文档中得到零计数。我将修改我的问题并包含更广泛的代码。
  • SHApes 不一定是 InlineShapes。内联形状。每个 StoryRange 中也可能有一个 Shapes 集合(抱歉,现在没时间多说)。
  • @bibadia 提出了一个很好的观点。 InlineShapes 集合仅包含放置在文档文本层中的图片。 Shapes 集合包含位​​于文档中其他任何位置的图片(页眉、页脚、浮动在文本前面的图片、文本环绕的图片等)。int picCount = _WordApp.ActiveDocument.Shapes.Count; 是否返回文档中正确数量的图像?
  • @bibadia 和 joeschwa,你们都提出了正确的观点,在某些时候我可能会重新进入应用程序,看看这些知识是否会使我的应用程序更接近我的原始目标。谁知道呢,下次我需要处理大量文档时,它可能会对我有所帮助。

标签: .net ms-word


【解决方案1】:

我们最终采取的解决方案是找一个能熟练使用 Microsoft Word 的测试人员、一张纸、一支铅笔和 Paint.Net 应用程序。

测试人员使用铅笔和纸标记图像的尺寸和位置,并使用 Paint.Net 调整图像大小。

我知道它并没有像我最初打算的那样以编程方式处理文档/图像,但我们有一个截止日期来完成该过程,有时作为开发人员,当您认为手动执行操作时,您必须拨打电话是正确的做法。

【讨论】:

  • “有时作为开发人员,当您认为手动执行操作是正确的事情时,您必须拨打电话”完全同意!
【解决方案2】:

试试:

foreach (Microsoft.Office.Interop.Word.Shape s in wordApp.ActiveDocument.Shapes)
{
        ...
}

【讨论】:

    【解决方案3】:

    此代码将查找文档中的所有内联图片。这些确实是内联形状。所以不确定OP的代码有什么问题。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.Office.Interop.Word;
    
    namespace OfficeBench
    {
        class Program
        {
            static void Main(string[] args)
            {
                Application app = new Application();
                Document doc = app.Documents.Open(@"c:\scratch\so\4pictures.docx");
                if (doc != null)
                {
                    Console.WriteLine($"Number in in-line shapes = {doc.InlineShapes.Count}");
                    foreach (InlineShape shape in doc.InlineShapes)
                    {
                        Console.WriteLine($"Shape (width,height) = ({shape.Width},{shape.Height})");
                        Console.WriteLine($"Shape type = {shape.Type}");
                        Console.WriteLine();
                        if (shape.Type == WdInlineShapeType.wdInlineShapePicture)
                        {
                            // ...
                        }
                    }
                    doc.Close();
                }
                else
                {
                    Console.WriteLine("Error - Unable to open document.");
                }
            }
        }
    }
    

    这会产生输出...

    Number in in-line shapes = 4
    Shape (width,height) = (293.25,164.75)
    Shape type = wdInlineShapePicture
    
    Shape (width,height) = (412.5,231.75)
    Shape type = wdInlineShapePicture
    
    Shape (width,height) = (226.9,127.5)
    Shape type = wdInlineShapePicture
    
    Shape (width,height) = (222.75,125.1)
    Shape type = wdInlineShapePicture
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多