【问题标题】:Get image from header and footer of .docx file in C#在 C# 中从 .docx 文件的页眉和页脚获取图像
【发布时间】:2011-12-29 13:00:13
【问题描述】:

我有一个 .docx 文件,它在页脚和页眉中有图像。如何获取图片,知道哪些在页脚,哪些在页眉?

我尝试使用:

Microsoft.Office.Interop.Word.Range range = section.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range;

但我看不到任何有用的属性。

【问题讨论】:

    标签: c# header ms-word footer


    【解决方案1】:

    查看 Range 对象的 InlineShape (http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.inlineshapes(v=office.11).aspx) 属性。它是 InlineShape (http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.inlineshape(v=office.11).aspx) 对象的集合。Inline 对象可以是多种类型的对象中的任何一种,您可以检查哪一种它是通过访问 Type 属性 (http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.inlineshape.type(v=office.11).aspx)

    (自从有人问这个问题已经快 2 年了,我希望作者找到了解决方案,我添加了这个以防其他人发现这有帮助)。

    【讨论】:

      【解决方案2】:

      方式 1:

       foreach(Microsoft.Office.Interop.Word.Shape Headershape in OHeader.Shapes)
                              {
                                 InlineShape inlineshape = Headershape.ConvertToInlineShape();
                                 Range PictureRange = inlineshape.Range;
                                 inlineshape.Delete();
                                 PictureRange.InlineShapes.AddPicture(m_sLogoPath);
                              }
      

      方式:2

       foreach (InlineShape shape in OHeader.Range.InlineShapes) 
                                  {
                                      if (shape.Type == Microsoft.Office.Interop.Word.WdInlineShapeType.wdInlineShapePicture)
                                      {
                                          shape.Delete();
                                          oSection.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.InlineShapes.AddPicture(m_sLogoPath);
                                      }
                                  }
      

      【讨论】:

      • var applicationWord = new Microsoft.Office.Interop.Word.Application(); adoc = applicationWord.Documents.Open(ref ofileName); foreach (Section oSection in adoc.Sections) foreach (HeaderFooter OHeader in oSection.Headers)
      • Range PictureRange= Headershape.Anchor; Headershape.Delete(); PictureRange.InlineShapes.AddPicture(m_sLogoPath);
      【解决方案3】:
      var applicationWord = new Microsoft.Office.Interop.Word.Application();
      adoc = applicationWord.Documents.Open(ref ofileName);
      foreach (Section oSection in adoc.Sections)
      {
          foreach (HeaderFooter OHeader in oSection.Headers)
          {
              foreach(Microsoft.Office.Interop.Word.Shape Headershape in OHeader.Shapes)
              {
                  Headershape.Delete();
                  OHeader.Shapes.AddPicture(m_sLogoPath);
              }
          }
      }
      

      参考:Word Automation Basics

      【讨论】:

        猜你喜欢
        • 2015-11-16
        • 2012-05-04
        • 2011-04-23
        • 2020-08-30
        • 2017-10-18
        • 2012-09-24
        • 2023-04-05
        • 2018-03-18
        • 2015-09-02
        相关资源
        最近更新 更多