将图片添加到word文档的页脚

时间:2018-02-22 22:22:18

标签: c# ms-word vsto add-in office-addins

我的程序有问题。我需要在word文档的页脚中添加一张图片。

我有2个功能,替换书签文字和添加图像。替换书签文本有效。添加图像不起作用,我在stackoverflow上搜索了几天,但我找不到任何sollution。我希望有人可以帮助我。

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

    }

1 个答案:

答案 0 :(得分:0)

发现溶解

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