How to add text on desired coordinates on new word file using openxml

前端 未结 1 1732
孤街浪徒
孤街浪徒 2021-01-27 01:20

I want to create .docx file using openxml and add text on desired coordinates(location) on each page of the file. Is there any way in openxml to adjust the text. I am using the

1条回答
  •  南方客
    南方客 (楼主)
    2021-01-27 01:59

    I found a way to add text to a coordinate on the page of a Word file. I started with your generated Word file and using Word, I added a simple TextBox (Insert->Text->TextBox). I generated the code for the added TextBox using the Productivity Tool. (Note: as of this writing, the latest version of the SDK is now 2.5, which is recommended for this to work).

    Add the following method to your class above:

    private static void PlaceTextAtCoordinate(Paragraph para, string text, double xCoordinate, double uCoordinate)
        {
            var picRun = para.AppendChild(new Run());
    
            Picture picture1 = picRun.AppendChild(new Picture());
    
            Shapetype shapetype1 = new  Shapetype() { Id = "_x0000_t202", CoordinateSize = "21600,21600", OptionalNumber = 202, EdgePath = "m,l,21600r21600,l21600,xe" };
            Stroke stroke1 = new Stroke() { JoinStyle = StrokeJoinStyleValues.Miter };
            Path path1 = new Path() { AllowGradientShape = true, ConnectionPointType = ConnectValues.Rectangle };
    
            shapetype1.Append(stroke1);
            shapetype1.Append(path1);
    
            Shape shape1 = new Shape() { Id = "Text Box 2", Style = string.Format("position:absolute;margin-left:{0:F1}pt;margin-top:{1:F1}pt;width:187.1pt;height:29.7pt;z-index:251657216;visibility:visible;mso-wrap-style:square;mso-width-percent:400;mso-height-percent:200;mso-wrap-distance-left:9pt;mso-wrap-distance-top:3.6pt;mso-wrap-distance-right:9pt;mso-wrap-distance-bottom:3.6pt;mso-position-horizontal-relative:text;mso-position-vertical-relative:text;mso-width-percent:400;mso-height-percent:200;mso-width-relative:margin;mso-height-relative:margin;v-text-anchor:top", xCoordinate, uCoordinate), Stroked = false };
    
            TextBox textBox1 = new TextBox() { Style = "mso-fit-shape-to-text:t" };
    
            TextBoxContent textBoxContent1 = new TextBoxContent();
    
            Paragraph paragraph2 = new Paragraph();
    
            Run run2 = new Run();
            Text text2 = new Text();
            text2.Text = text;
    
            run2.Append(text2);
    
            paragraph2.Append(run2);
    
            textBoxContent1.Append(paragraph2);
    
            textBox1.Append(textBoxContent1);
            TextWrap textWrap1 = new TextWrap() { Type = WrapValues.Square };
    
            shape1.Append(textBox1);
            shape1.Append(textWrap1);
    
            picture1.Append(shapetype1);
            picture1.Append(shape1);
        }
    

    The following usings were found in my class - your list may be different - but I wanted to detail them here just in case.

    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Vml;
    using DocumentFormat.OpenXml.Vml.Office;
    using DocumentFormat.OpenXml.Vml.Wordprocessing;
    using DocumentFormat.OpenXml.Wordprocessing;
    

    Finally, add the following 2 calls to the very end of your method above:

    PlaceTextAtCoordinate(para, "Text at 90.1,90.1", 90.1, 90.1);
    PlaceTextAtCoordinate(para, "Text at 120.5,120.5", 120.1, 120.1);
    

    and your Word Doc will look like the following:

    0 讨论(0)
提交回复
热议问题