简体   繁体   English

在Word文档的页脚中添加图片

[英]Adding picture to footer of a word document

I have a problem with my program. 我的程序有问题。 I need to add a picture in the footer of a word document. 我需要在Word文档的页脚中添加图片。

I have 2 functions, replacing bookmarked text and add images. 我有2个功能,可以替换书签文本并添加图像。 The replace bookmarked text works. 替换书签文本起作用。 add images doesnt work, i searched for days on stackoverflow, but i cant find any sollution. 添加图像不起作用,我在stackoverflow上搜索了几天,但找不到任何解决方案。 I hope someone can help me out. 我希望有人能帮助我。

        private static void AddImages(Document wordDoc, string imagePath){
        var sec = wordDoc.Application.Selection.Sections[1];
        var ft = sec.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
        var rngFooter = ft.Range;
        object oRange = rngFooter;

        var autoScaledInlineShape = ft.Shapes.AddPicture(imagePath);
        var scaledWidth = autoScaledInlineShape.Width;
        var scaledHeight = autoScaledInlineShape.Height;
        autoScaledInlineShape.Delete();

        // Create a new Shape and fill it with the picture
        var newShape = wordDoc.Shapes.AddShape(1, 0, 0, scaledWidth, scaledHeight);
        newShape.Fill.UserPicture(imagePath);

        // Convert the Shape to an InlineShape and optional disable Border
        var finalInlineShape = newShape.ConvertToInlineShape();
        finalInlineShape.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse;

        // Cut the range of the InlineShape to clipboard
        finalInlineShape.Range.Cut();

        // And paste it to the target Range
        ft.Paste();

    }

Found sollution 找到解决办法

        private static void FindAndReplaceImages(Document wordDoc, string imagePath){
        var sec = wordDoc.Application.Selection.Sections[1];


        foreach (Section wordSection in wordDoc.Sections)
        {
            var footer = sec.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
            var footerImage = footer.Shapes.AddShape(1, 0, 0, 594, 280);
            footerImage.Fill.UserPicture(imagePath);
            footerImage.WrapFormat.Type = WdWrapType.wdWrapThrough;
            footerImage.WrapFormat.AllowOverlap = -1;
            footerImage.WrapFormat.Side = WdWrapSideType.wdWrapBoth;
            footerImage.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;
            footerImage.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
            footerImage.Top = (float)561.2;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM