Horizontal and Vertical alignment of an image from C# to Word

36 Views Asked by At

I'm basically trying to horizontally center image relative to the page and vertically bottom align it again relative to the page. The image is in the footer. It appears, the width, height, etc... are all okay. Thanks in advance!

private static void AddImageToFooter(FooterPart footerPart, string imagePath)
{
ImagePart imagePart = footerPart.AddImagePart(ImagePartType.Png);
using (FileStream stream = new FileStream(imagePath, FileMode.Open))
{
imagePart.FeedData(stream);
}

    string relationshipId = footerPart.GetIdOfPart(imagePart);
    
    int imageWidthPx = 795;
    int imageHeightPx = 137;
    
    long imageWidthEmus = imageWidthPx * 9525;
    long imageHeightEmus = imageHeightPx * 9525;
    
    Drawing imageElement = new Drawing(
        new DW.Inline(
            new DW.Extent() { Cx = imageWidthEmus, Cy = imageHeightEmus },
            new DW.EffectExtent() { LeftEdge = 0, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
            new DW.HorizontalAlignment() { },
            new DW.VerticalAlignment() { },
            new DW.DocProperties() { Id = (UInt32Value)1U, Name = "Picture" },
            new DW.NonVisualGraphicFrameDrawingProperties(new A.GraphicFrameLocks() { NoChangeAspect = true }),
            new A.Graphic(
                new A.GraphicData(
                    new PIC.Picture(
                        new PIC.NonVisualPictureProperties(
                            new PIC.NonVisualDrawingProperties() { Id = (UInt32Value)0U, Name = "New Image" },
                            new PIC.NonVisualPictureDrawingProperties()),
                        new PIC.BlipFill(
                            new A.Blip() { Embed = relationshipId },
                            new A.Stretch(new A.FillRectangle())),
                        new PIC.ShapeProperties(
                            new A.Transform2D(
                                new A.Offset() { X = 0L, Y = 0L },
                                new A.Extents() { Cx = imageWidthEmus, Cy = imageHeightEmus }),
                            new A.PresetGeometry(new A.AdjustValueList()) { Preset = A.ShapeTypeValues.Rectangle })
                    )
                )
                { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" }
            )
        )
        { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U, EditId = "50D07946" }
    );
    
    Paragraph paragraph = new Paragraph(new Run(imageElement));
    footerPart.Footer.Append(paragraph);
    
    footerPart.Footer.Save();

}

I've tried several things like Anchor(), but this for some reason makes it so my Word document will not open, I'm assuming due to it not being supported or I'm just doing it wrong, I'll provide a snippet of what I tried previously:

long horizontalPositionEmus = (totalPageWidthEmus - imageWidthEmus) / 2; // Center horizontally
long verticalPositionEmus = totalPageHeightEmus - imageHeightEmus;

Drawing imageElement = new Drawing(
new DW.Anchor(
new DW.SimplePosition() { X = horizontalPositionEmus, Y = verticalPositionEmus },
new DW.HorizontalPosition() { RelativeFrom = DW.HorizontalRelativePositionValues.Page },
new DW.VerticalPosition() { RelativeFrom = DW.VerticalRelativePositionValues.Page },
new DW.Extent() { Cx = imageWidthEmus, Cy = imageHeightEmus },
// Other properties for wrapping, effects, etc.
// ...

Drawing imageElement = new Drawing(
new DW.Anchor(
new DW.SimplePosition() { X = 0L, Y = 0L },
new DW.HorizontalPosition() { RelativeFrom = DW.HorizontalRelativePositionValues.Page },
new DW.VerticalPosition() { RelativeFrom = DW.VerticalRelativePositionValues.Page },
new DW.Extent() { Cx = imageWidthEmus, Cy = imageHeightEmus },
new DW.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
new DW.WrapSquare(),
// ...

And this simply doesn't work or I'm just missing something:

Drawing imageElement = new Drawing(
new DW.Inline(
new DW.Extent() { Cx = imageWidthEmus, Cy = imageHeightEmus },
new DW.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
new DW.DocProperties() { Id = (UInt32Value)1U, Name = "Picture" },
new DW.NonVisualGraphicFrameDrawingProperties(new A.GraphicFrameLocks() { NoChangeAspect = true }),


0

There are 0 best solutions below