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

不问归期 提交于 2019-12-31 03:46:10

问题


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 following snippet:

WordprocessingDocument doc = WordprocessingDocument.Create("E:\\test11.docx", DocumentFormat.OpenXml.WordprocessingDocumentType.Document);
            {
                MainDocumentPart mainPart = doc.AddMainDocumentPart();
                mainPart.Document = new Document();
                Body body = mainPart.Document.AppendChild(new Body());
                Paragraph para = body.AppendChild(new Paragraph());
                ParagraphProperties oParagraphProperties = para.AppendChild(new ParagraphProperties());
                Run run = para.AppendChild(new Run());
                Text tt = new Text(str);
                run.AppendChild(tt);
                RunProperties runProp = new RunProperties(); // Create run properties.
                RunFonts runFont = new RunFonts() { Ascii = "Cambria(Headings)", HighAnsi = "Cambria(Headings)" };
                Bold bold = new Bold();
                DocumentFormat.OpenXml.Wordprocessing.Color Color1 = new DocumentFormat.OpenXml.Wordprocessing.Color() { Val = "0EBFE9" };
                Italic ita = new Italic();
                runProp.Append(bold);
                runProp.Append(Color1);
                runProp.Append(ita);
                FontSize size = new FontSize();
                size.Val = new StringValue((fontSize * 2).ToString());  // 48 half-point font size
                runProp.Append(runFont);
                runProp.Append(size);
                run.PrependChild<RunProperties>(runProp);
            }

Using this I was able to add text on .docx file, but not on desired coordinate location. Can someone help with this?

Thanks.


回答1:


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:



来源:https://stackoverflow.com/questions/39425646/how-to-add-text-on-desired-coordinates-on-new-word-file-using-openxml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!