在 C# 中从 .docx 文件的页眉和页脚获取图像

发布于 2024-12-15 23:40:09 字数 259 浏览 0 评论 0 原文

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

我尝试使用:

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

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

I have a .docx file which has images in the footer and the header. How to get the images, knowing which is in the footer and which is in the header?

I tried using:

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

but I can't see any properties that would be useful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独陪着我 2024-12-22 23:40:09

看一下 InlineShapes (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) 对象。内联对象可以是多种类型对象中的任何一种,您可以通过访问 Type 属性来检查它是哪一种(http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.inlineshape.type(v=office.11​​).aspx

(它有自从有人问这个问题以来已经快两年了,我希望作者已经找到了解决方案,我添加了这个以防其他人可能会觉得这有帮助)。

Take a look at the InlineShapes (http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.inlineshapes(v=office.11).aspx) property of the Range object. It's a collection of InlineShape (http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.inlineshape(v=office.11).aspx) objects. An Inline object can be any one of several types of objects, and you could check which one it is by accessing the Type property (http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word.inlineshape.type(v=office.11).aspx)

(It has been a almost 2 years since this was asked and I hope the author has found the solution, I added this in case anyone else might find this helpful).

寄与心 2024-12-22 23:40:09

方式 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);
                                }
                            }

way 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);
                        }

Way : 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);
                                }
                            }
娇俏 2024-12-22 23:40:09
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自动化基础知识

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);
        }
    }
}

Reference : Word Automation Basics

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文