C# 代码在图像上换行文本 - OpenXml

C# code to Wrap text on top of an image - OpenXml

我正在使用 OpenXml 创建一个 word 文档。该文档由 header 部分组成,其中包含一张图片。 现在我必须在图像顶部显示动态文本。文本将位于横幅图片的正中心,图片应该看起来像背景。

The header should look the attached image, the text is dynamic and it should be in the exact center of the image

 private static Header GeneratePicHeader(string imageWaterMark, string imageLogoHeader, string cmpHdr, string dateHdr)
{
    var bText = "The Boeing Company";
    var bdrCount = BorderValues.Single;
    var bdrColor = "auto";
    var bdrSize = (UInt32Value)4U;
    var bdrSpace = (UInt32Value)2U;
    var fntType = "Arial";
    var fntSize = (UInt32Value)22U;
    var txtAlign = JustificationValues.Center;

    var element =
         new Drawing(
             new DW.Inline(
                 new DW.Extent() { Cx = 5900000L, Cy = 900000L },
                 new DW.EffectExtent()
                 {
                     LeftEdge = 0L,
                     TopEdge = 0L,
                     RightEdge = 0L,
                     BottomEdge = 0L
                 },
                 new DW.DocProperties()
                 {
                     Id = (UInt32Value)1U,
                     Name = "Header Logo"
                 },
                 new DW.NonVisualGraphicFrameDrawingProperties(

                     new A.GraphicFrameLocks() { NoChangeAspect = true }),
                 new A.Graphic(
                     new A.GraphicData(
                         //  new Text(bText),
                         new PIC.Picture(
                             new PIC.NonVisualPictureProperties(
                             new PIC.NonVisualDrawingProperties()
                             {
                                 Id = (UInt32Value)0U,
                                 Name = "Heade1.jpg"
                             },
                                 new PIC.NonVisualPictureDrawingProperties()
                                 ),
                             new PIC.BlipFill(

                                 new A.Blip()

                                 {
                                     Embed = imageLogoHeader,
                                     CompressionState =
                                         A.BlipCompressionValues.Print
                                 },
                                 new A.Stretch(
                                     new A.FillRectangle())),

                             new PIC.ShapeProperties(
                                 new A.Transform2D(
                                     new A.Offset() { X = 0L, Y = 0L },
                                     new A.Extents() { Cx = 59000L, Cy = 90000L }
                                     ),
                                 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,
             });

    var header = new Header();
    var paragraph = new Paragraph();
    var run = new Run();
    run.Append(element);

这只适用于锚标记,所以替换内联代码,请找到下面的代码,它对我有用

    Drawing drawing = new Drawing();
    DW.Anchor anchor2 = new DW.Anchor() { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)114300U, DistanceFromRight = (UInt32Value)114300U, SimplePos = false, RelativeHeight = (UInt32Value)251662848U, BehindDoc = true, Locked = false, LayoutInCell = true, AllowOverlap = true };
    DW.SimplePosition simplePosition2 = new DW.SimplePosition() { X = 0L, Y = 0L };
    DW.HorizontalPosition horizontalPosition2 = new DW.HorizontalPosition() { RelativeFrom = DW.HorizontalRelativePositionValues.Column };
    DW.PositionOffset positionOffset3 = new DW.PositionOffset();
    positionOffset3.Text = "0";
    horizontalPosition2.Append(positionOffset3);
    DW.VerticalPosition verticalPosition2 = new DW.VerticalPosition() { RelativeFrom = DW.VerticalRelativePositionValues.Paragraph };
    DW.PositionOffset positionOffset4 = new DW.PositionOffset();
    positionOffset4.Text = "32385";
    verticalPosition2.Append(positionOffset4);
    DW.Extent extent = new DW.Extent() { Cx = 5950000L, Cy = 745000L };
    DW.EffectExtent effetctEx = new DW.EffectExtent() { LeftEdge = 0L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L, };
    DW.WrapTight wrapTight1 = new DW.WrapTight() { WrapText = DW.WrapTextValues.BothSides };
    DW.WrapPolygon wrapPolygon1 = new DW.WrapPolygon() { Edited = false };
    DW.StartPoint startPoint1 = new DW.StartPoint() { X = 0L, Y = 0L };
    DW.LineTo lineTo1 = new DW.LineTo() { X = 0L, Y = 21036L };
    DW.LineTo lineTo2 = new DW.LineTo() { X = 21551L, Y = 21036L };
    DW.LineTo lineTo3 = new DW.LineTo() { X = 21551L, Y = 0L };
    DW.LineTo lineTo4 = new DW.LineTo() { X = 0L, Y = 0L };
    wrapPolygon1.Append(startPoint1);
    wrapPolygon1.Append(lineTo1);
    wrapPolygon1.Append(lineTo2);
    wrapPolygon1.Append(lineTo3);
    wrapPolygon1.Append(lineTo4);
    wrapTight1.Append(wrapPolygon1);
    DW.DocProperties docProperties = new DW.DocProperties() { Id = (UInt32Value)1U, Name = "Header Logo" };
    DW.NonVisualGraphicFrameDrawingProperties nonVisualGraphicFrameDrawingProperties = new DW.NonVisualGraphicFrameDrawingProperties();
    A.GraphicFrameLocks graphicFrameLocks = new A.GraphicFrameLocks() { NoChangeAspect = true };
    A.Graphic graphic = new A.Graphic() ;
    A.GraphicData graphicData = new A.GraphicData() { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" };
    PIC.Picture picture = new PIC.Picture() ; 
    PIC.NonVisualPictureProperties nonVisualPictureProperties = new PIC.NonVisualPictureProperties() ;
    PIC.NonVisualDrawingProperties nonVisualDrawingProperties = new PIC.NonVisualDrawingProperties() { Id = (UInt32Value)0U, Name = "HeadeLogo.jpeg" };
    PIC.NonVisualPictureDrawingProperties nonVisualPictureDrawingProperties = new PIC.NonVisualPictureDrawingProperties() ;
    PIC.BlipFill blipFill = new PIC.BlipFill();
    A.Blip blip = new A.Blip() { Embed = imageLogoHeader, CompressionState = A.BlipCompressionValues.Print };
    A.Stretch stretch = new A.Stretch();
    A.FillRectangle fillRectangle = new A.FillRectangle();
    PIC.ShapeProperties shapeProperties = new PIC.ShapeProperties( 
   new A.Transform2D(new A.Offset() { X = 0L, Y = 0L }, new A.Extents() { Cx = 5950000L, Cy = 745000L }),
   new A.PresetGeometry(new A.AdjustValueList()) { Preset = A.ShapeTypeValues.Rectangle });
    nonVisualPictureProperties.Append(nonVisualDrawingProperties);
    nonVisualPictureProperties.Append(nonVisualPictureDrawingProperties);
    picture.Append(nonVisualPictureProperties);
    blipFill.Append(blip);       
    stretch.Append(fillRectangle);
    blipFill.Append(stretch);
    picture.Append(blipFill);
    picture.Append(shapeProperties);
    graphicData.Append(picture);
    graphic.Append(graphicData);
    anchor2.Append(simplePosition2);
    anchor2.Append(horizontalPosition2);
    anchor2.Append(verticalPosition2);
    anchor2.Append(extent);
    anchor2.Append(effetctEx);
    anchor2.Append(wrapTight1);
    anchor2.Append(docProperties);
    anchor2.Append(nonVisualGraphicFrameDrawingProperties);
    anchor2.Append(graphic);
    drawing.Append(anchor2);

    var header = new Header();
    var paragraph = new Paragraph();
    var run = new Run();
    run.Append(drawing);

    Paragraph paragraph2 = new Paragraph() ;
    Run run1 = new Run();
    Picture picture1 = new Picture();

     V.Shape shape1 = new V.Shape() { Id = "WordPictureWatermark75517470", Style = "position:absolute;mso-wrap-style:Tight", AllowInCell = false, Filled = false, Stroked = false, Type = "TextWrappingValues.Tight"} ;

    picture1.Append(shape1);
    run1.Append(picture1);
    paragraph2.Append(run1);
    header.Append(paragraph2);

    header.Append(
    new Paragraph(
              new ParagraphProperties(                  
                    new ParagraphBorders(
                    new TopBorder() { Val = bdrCount, Color = bdrColor, Size = bdrSize, Space = bdrSpace },
                    new LeftBorder() { Val = bdrCount, Color = bdrColor, Size = bdrSize, Space = bdrSpace },
                    new BottomBorder() { Val = bdrCount, Color = bdrColor, Size = bdrSize, Space = bdrSpace },
                    new RightBorder() { Val = bdrCount, Color = bdrColor, Size = bdrSize, Space = bdrSpace })), 
        new Run(run)
        ),
     new Paragraph(
        new ParagraphProperties(
            new ParagraphBorders(
                new TopBorder() { Val = bdrCount, Color = bdrColor, Size = bdrSize, Space = bdrSpace },
                new LeftBorder() { Val = bdrCount, Color = bdrColor, Size = bdrSize, Space = bdrSpace },
                new BottomBorder() { Val = bdrCount, Color = bdrColor, Size = bdrSize, Space = bdrSpace },
                new RightBorder() { Val = bdrCount, Color = bdrColor, Size = bdrSize, Space = bdrSpace }),
            new Justification() { Val = txtAlign },
            new ParagraphMarkRunProperties(
                new RunFonts() { Ascii = fntType, HighAnsi = fntType, ComplexScript = fntType },
                new Bold(),
                new FontSize() { Val = fntSize },
                new FontSizeComplexScript() { Val = fntSize })),
        new Run(
            new RunProperties(
                new RunFonts() { Ascii = fntType, HighAnsi = fntType, ComplexScript = fntType },
                new Bold(),
                new FontSize() { Val = fntSize },
                new FontSizeComplexScript() { Val = fntSize }),
             new Text(bText)
        )