1

我想使用 openxml 创建 .docx 文件,并在文件的每一页上的所需坐标(位置)上添加文本。openxml中有什么方法可以调整文本。我正在使用以下代码段:

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);
            }

使用它,我可以在 .docx 文件上添加文本,但不能在所需的坐标位置上添加文本。有人可以帮忙吗?

谢谢。

4

1 回答 1

3

我找到了一种将文本添加到 Word 文件页面上的坐标的方法。我从您生成的 Word 文件开始,使用 Word,添加了一个简单的文本框(插入->文本->文本框)。我使用Productivity Tool为添加的 TextBox 生成了代码。(注意:在撰写本文时,SDK 的最新版本现在是 2.5,建议使用此版本)。

将以下方法添加到上面的类中:

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);
    }

在我的课堂上发现了以下用法——你的列表可能不同——但我想在这里详细说明它们以防万一。

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;

最后,将以下 2 个调用添加到上述方法的最后:

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

您的 Word Doc 将如下所示:

在此处输入图像描述

于 2016-09-21T21:40:58.480 回答