最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Add background image in word with OpenXml - Stack Overflow

programmeradmin4浏览0评论

I'm reading a Word template file that I process in my code.

I want to set the background image of said document. Here's how I'm doing it `

using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(filePath, WordprocessingDocumentType.Document))
{
    MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
    mainPart.Document = new Document();
    Body body = new Body();
    mainPart.Document.Append(body);

    SectionProperties sectionProperties = new SectionProperties(
        new PageSize()
        {
            Width = (UInt32Value)16838U,
            Height = (UInt32Value)11906U,
            Orient = PageOrientationValues.Landscape
        },
        new PageMargin()
        {
            Top = 1440,
            Right = 1440,
            Bottom = 1440,
            Left = 1440,
            Header = 720,
            Footer = 720,
            Gutter = 0
        }
    );
    body.AppendChild(sectionProperties);

    var MainPageImgUrl = tocsInfo.FirstOrDefault(x => x.id == 7)?.images["background1"];
    if (!string.IsNullOrEmpty(MainPageImgUrl))
    {
        byte[] imageBytes = await DownloadBackgroundImage(MainPageImgUrl);
        
        // Görseli ImagePart'e ekle
        ImagePart imagePart = mainPart.AddImagePart(ImagePartType.Png);
        using (MemoryStream stream = new MemoryStream(imageBytes))
        {
            imagePart.FeedData(stream);

            BinaryWriter bw = new BinaryWriter(stream);
            bw.Write(imageBytes);
            bw.Close();
        }
        
        string imageId = mainPart.GetIdOfPart(imagePart);
        DocumentBackground docBg = new DocumentBackground() 
            {
                Color = "FFFFFF",
            };

        V.Background bg = new V.Background()
        {
            Id = "_x0000_s1025",
            BlackWhiteMode = V.Office.BlackAndWhiteModeValues.White,
            TargetScreenSize = V.Office.ScreenSizeValues.Sz1024x768,
        };

        bg.AddNamespaceDeclaration("v", "urn:schemas-microsoft-com:vml");

        V.Fill fill = new V.Fill()
        {
            RelationshipId = imageId, // Doğru ID kullanımı
            Title = "background",
            Recolor = false,
            Type = V.FillTypeValues.Frame,
            Aspect = V.ImageAspectValues.Ignore,
        };

        var settingsPart = mainPart.DocumentSettingsPart;
        if (settingsPart == null)
        {
            settingsPart = mainPart.AddNewPart<DocumentSettingsPart>();
            settingsPart.Settings = new Settings();
        }

        settingsPart.Settings.Append(new DisplayBackgroundShape());
        settingsPart.Settings.Save();

        bg.Append(fill);
        docBg.Append(bg);

        mainPart.Document.InsertAt(docBg, 0);
    }
   

    body.Append(new Paragraph(new Run(new Break() { Type = BreakValues.Page })));

    mainPart.Document.Save();
}

When I do it this way, the image comes, but I can't fit the image on the page because of “DisplayBackgroundShape()”, it overflows. If I define another property and not Type = V.FillTypeValues.Frame, no image comes to the screen.

I want to fit and show my background image exactly on a horizontal word page.

发布评论

评论列表(0)

  1. 暂无评论