簡體   English   中英

在Word文檔的頁腳中添加圖片

[英]Adding picture to footer of a word document

我的程序有問題。 我需要在Word文檔的頁腳中添加圖片。

我有2個功能,可以替換書簽文本並添加圖像。 替換書簽文本起作用。 添加圖像不起作用,我在stackoverflow上搜索了幾天,但找不到任何解決方案。 我希望有人能幫助我。

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

    }

找到解決辦法

        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