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

c# - OpenXML generates top margin when adding image to non-main pages - Stack Overflow

programmeradmin4浏览0评论
    private void AddHeaderToAllPages(MainDocumentPart mainPart, byte[] headerImageBytes, long widthEmus, long heightEmus)
    {
        var headerPart = mainPart.AddNewPart<HeaderPart>();
        var header = new Header();
        var paragraph = new Paragraph();
        var run = new Run();

        var imagePart = headerPart.AddImagePart(ImagePartType.Jpeg);
        using (var stream = new MemoryStream(headerImageBytes))
        {
            imagePart.FeedData(stream);
        }

        var element =
            new Drawing(
                new WP.Inline(
                    new WP.Extent { Cx = widthEmus, Cy = heightEmus },
                    new WP.EffectExtent
                    {
                        LeftEdge = 0L, TopEdge = 0L,
                        RightEdge = 0L, BottomEdge = 0L
                    },
                    new WP.DocProperties { Id = 1U, Name = "headersImage" },
                    new WP.NonVisualGraphicFrameDrawingProperties(new A.GraphicFrameLocks { NoChangeAspect = true }),
                    new A.Graphic(
                        new A.GraphicData(
                            new Pic.Picture(
                                new Pic.NonVisualPictureProperties(
                                    new Pic.NonVisualDrawingProperties { Id = 0U, Name = "headersImage" },
                                    new Pic.NonVisualPictureDrawingProperties()
                                ),
                                new Pic.BlipFill(
                                    new A.Blip { Embed = headerPart.GetIdOfPart(imagePart) },
                                    new A.Stretch(new A.FillRectangle())
                                ),
                                new Pic.ShapeProperties(
                                    new A.Transform2D(
                                        new A.Offset { X = 0L, Y = 0L },
                                        new A.Extents { Cx = widthEmus, Cy = heightEmus }
                                    ),
                                    new A.PresetGeometry(new A.AdjustValueList()) { Preset = A.ShapeTypeValues.Rectangle }
                                )
                            )
                        ) { Uri = "; }
                    )
                ) { DistanceFromTop = 0U, DistanceFromBottom = 0U, DistanceFromLeft = 0U, DistanceFromRight = 0U }
            );

        run.Append(element);
        paragraph.Append(run);

        header.Append(paragraph);
        headerPart.Header = header;

        var sectionProperties = mainPart.Document.Body!.Elements<SectionProperties>().FirstOrDefault();
        if (sectionProperties == null)
        {
            sectionProperties = new SectionProperties();
            mainPart.Document.Body.Append(sectionProperties);
        }

        // Se a primeira página for diferente, adicione a tag TitlePage
        sectionProperties.PrependChild(new TitlePage());

        // Adiciona o cabeçalho apenas para as páginas subsequentes
        var headerReference = new HeaderReference { Type = HeaderFooterValues.Default, Id = mainPart.GetIdOfPart(headerPart) };
        sectionProperties.Append(headerReference);
    }

This code is part of others that create my Docx, in this part it adds a header to all pages that are not the main one, however, with a margin that is practically impossible to remove, I don't know exactly why it appears, I believe that openXML places it automatically because I also see it in the footer (another part of the code).

发布评论

评论列表(0)

  1. 暂无评论