none
Open XML SDK - Word: Image Vertical Alignment/Positioning in a Paragraph RRS feed

  • Question

  • Hi,

    I am trying to create a word document dynamically using OpenXML SDK2.0 In the document , I have a paragraph and have to place and image in the paragraph with text wrap . The image position is dynamic, but with set of values.  

    i.e Top Left, Top Right, Bottom Left and Bottom Right. I am able to get this working for Top Left/Right, However Bottom Left/Right does not seem to work.. I am trying to figureout how to get this workign without specifically setting any hard coded values as the size of paragraph may vary.

    The code which adds this paragraph with image is given below. Here I added a horizontal position with horizontal alignment value as "right". Also added a Veritical Position with Vertical alignment as "bottom", with RelativeFrom value as  "VerticalRelativePositionValues.Paragraph". But doesnt seem to be working.

    Please help me ...

    John

    private static Paragraph CreateParagraph2(string fillText1, long imageWidthEMU, long imageHeightEMU, string imageFileName)
            {
                string GraphicDataUri = "http://schemas.openxmlformats.org/drawingml/2006/picture";
                double imageWidthInInches = imageWidthEMU / 914400.0;
                double imageHeightInInches = imageHeightEMU / 914400.0;
                long horizontalWrapPolygonUnitsPerInch =
                  (long)(21600L / imageWidthInInches);
                long verticalWrapPolygonUnitsPerInch =
                  (long)(21600L / imageHeightInInches);
                wp.WrapPolygon wrapPolygon1 = new wp.WrapPolygon() { Edited = false };
                wp.StartPoint startPoint1 = new wp.StartPoint() { X = -150L, Y = 0L };
                wp.LineTo lineTo1 = new wp.LineTo() { X = -250L, Y = 21521L };
                wp.LineTo lineTo2 = new wp.LineTo() { X = 21600L, Y = 21521L };
                wp.LineTo lineTo3 = new wp.LineTo() { X = 21600L, Y = 0L };
                wp.LineTo lineTo4 = new wp.LineTo() { X = -250L, Y = 0L };
                wrapPolygon1.Append(startPoint1);
                wrapPolygon1.Append(lineTo1);
                wrapPolygon1.Append(lineTo2);
                wrapPolygon1.Append(lineTo3);
                wrapPolygon1.Append(lineTo4);
                wp.WrapTight wrapTight = new wp.WrapTight(wrapPolygon1) { WrapText = wp.WrapTextValues.BothSides };
                wp.VerticalPosition vertPostition = new wp.VerticalPosition
                    (new wp.VerticalAlignment("bottom")) { RelativeFrom = wp.VerticalRelativePositionValues.Paragraph};
                
                Paragraph p2 = new Paragraph(
                       new ParagraphProperties(
                         new Justification() { Val = JustificationValues.Both },
                       new Run(
                         new Drawing(
                           new wp.Anchor(
                             new wp.SimplePosition() { X = 0L, Y = 0L },
                             // Center the image horizontally on the page. 
                             new wp.HorizontalPosition(new wp.HorizontalAlignment("right"))
                             {
                                 RelativeFrom =wp.HorizontalRelativePositionValues.Margin 
                             },
                            vertPostition,
                             new wp.Extent()
                             {
                                 Cx = imageWidthEMU,
                                 Cy = imageHeightEMU
                             },
                             new wp.EffectExtent()
                             {
                                 LeftEdge = 0L,
                                 TopEdge = 0L,
                                 RightEdge = 0L,
                                 BottomEdge = 0L
                             },
                             wrapTight,
                             new wp.DocProperties()
                             {
                                 Id = (UInt32Value)2U,
                                 Name = "Tight Text Wrapping Picture",
                                 Description = "imageFileName"
                             },
                             new wp.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 = "imageFileName"
                                     },
                                     new pic.NonVisualPictureDrawingProperties()),
                                   new pic.BlipFill(
                                     new a.Blip() { Embed = "rId1" },
                                     new a.Stretch(
                                       new a.FillRectangle())),
                                   new pic.ShapeProperties(
                                     new a.Transform2D(
                                       new a.Offset() { X = 0L, Y = 0L },
                                       new a.Extents()
                                       {
                                           Cx = imageWidthEMU,
                                           Cy = imageHeightEMU
                                       }),
                                     new a.PresetGeometry(
                                       new a.AdjustValueList()
                                     ) { Preset = a.ShapeTypeValues.Rectangle }
                                   )
                                 )
                           ) { Uri = GraphicDataUri })
                         )
                           {
                               DistanceFromTop = (UInt32Value)0U,
                               DistanceFromBottom = (UInt32Value)0U,
                               DistanceFromLeft = (UInt32Value)114300U,
                               DistanceFromRight = (UInt32Value)114300U,
                               SimplePos = false,
                               RelativeHeight = (UInt32Value)251658240U,
                               BehindDoc = true,
                               Locked = false,
                               LayoutInCell = true,
                               AllowOverlap = true
                           })),
                           new Run(
                             new Text(fillText1))
                     )
                   );
                return p2;
            }

    Thursday, March 21, 2013 6:53 PM

All replies