如何在 OpenXML (C# .Net core 3.1) 中用 Image / Table 替换文本

How to replace text with Image / Table in OpenXML (C# .Net core 3.1)

我只是使用 OpenXML 来处理 docx 文档。我有带有符号的模板 {{userFirstName}} {{tableOfUsers}} {{signatureImage}} 等

我需要用 table、图像等替换那些文本。如何在 Openxml 中执行此操作? 我正在使用 C# .Net 核心 3.1 和 OpenXML 2.5

我的代码是这样的,但是table还是没有插入正确的地方:

    var fileName = Path.Combine(_hostingEnv.WebRootPath, "test/test.docx");
    var fileNameResult = Path.Combine(_hostingEnv.WebRootPath, "test/result.docx");

    byte[] byteArray = System.IO.File.ReadAllBytes(fileName);

    using (MemoryStream stream = new MemoryStream())
    {
        stream.Write(byteArray, 0, (int)byteArray.Length);
        using (WordprocessingDocument wd = WordprocessingDocument.Open(stream, true))
        {
            Table table = new Table();

            // Create a TableProperties object and specify its border information.
            TableProperties tblProp = new TableProperties(
                new TableBorders(
                    new TopBorder()
                    {
                        Val =
                        new EnumValue<BorderValues>(BorderValues.Dashed),
                        Size = 24
                    },
                    new BottomBorder()
                    {
                        Val =
                        new EnumValue<BorderValues>(BorderValues.Dashed),
                        Size = 24
                    },
                    new LeftBorder()
                    {
                        Val =
                        new EnumValue<BorderValues>(BorderValues.Dashed),
                        Size = 24
                    },
                    new RightBorder()
                    {
                        Val =
                        new EnumValue<BorderValues>(BorderValues.Dashed),
                        Size = 24
                    },
                    new InsideHorizontalBorder()
                    {
                        Val =
                        new EnumValue<BorderValues>(BorderValues.Dashed),
                        Size = 24
                    },
                    new InsideVerticalBorder()
                    {
                        Val =
                        new EnumValue<BorderValues>(BorderValues.Dashed),
                        Size = 24
                    }
                )
            );

            // Append the TableProperties object to the empty table.
            table.AppendChild<TableProperties>(tblProp);

            // Create a row.
            TableRow tr = new TableRow();

            // Create a cell.
            TableCell tc1 = new TableCell();

            // Specify the width property of the table cell.
            tc1.Append(new TableCellProperties(
                new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));

            // Specify the table cell content.
            tc1.Append(new Paragraph(new Run(new Text("some text"))));

            // Append the table cell to the table row.
            tr.Append(tc1);

            // Create a second table cell by copying the OuterXml value of the first table cell.
            TableCell tc2 = new TableCell(tc1.OuterXml);

            // Append the table cell to the table row.
            tr.Append(tc2);

            // Append the table row to the table.
            table.Append(tr);  
            var tblStr = table.ToString();

            //wd.MainDocumentPart.Document.Body.Append(table);
            Text tablePl = wd.MainDocumentPart.Document.Body.Descendants<Text>().Where(x => x.Text == "{{tableOfUsers}}").First();
            if(tablePl != null)
            {
                var parent = tablePl.Parent;
                tablePl.Parent.InsertAfter<Table>(table, tablePl);
                tablePl.Remove();
            }

            string docText = null;
            using (StreamReader sr = new StreamReader(wd.MainDocumentPart.GetStream()))
            {
                docText = sr.ReadToEnd();
            }
            Regex regexText = new Regex("{{name}}");
            docText = regexText.Replace(docText, "Claire " + DateTime.Now.ToString());
            // Regex regextable = new Regex("{{tableOfUsers}}");
            // docText = regextable.Replace(docText, tblStr);


            using (StreamWriter sw = new StreamWriter(wd.MainDocumentPart.GetStream(FileMode.Create)))
            {
                sw.Write(docText);
            }
        }
        await System.IO.File.WriteAllBytesAsync(fileNameResult, stream.ToArray());
    }

    _fileRepo.SetPath("test");
    string fileName2 = "result.docx";
    var data = await _fileRepo.DownloadFile(fileName2);
    return File(data, "application/octet-stram", "filename.docx");
}

我的 docx 文件看起来像

Hello {{name}}

Here is the list of users : {{tableOfUsers}}

Sincerely,

{{signatureImage}}

使用下面的代码插入一个table:

............
string tablePlaceholder = "{{tableOfUsers}}";
Text tablePl = wd.MainDocumentPart.Document.Body
    .Descendants<Text>()
    .Where(x => x.Text.Contains(tablePlaceholder))
    .First();
if (tablePl != null)
{
    //Insert the table after the paragraph.
    var parent = tablePl.Parent.Parent.Parent;
    parent.InsertAfter(table, tablePl.Parent.Parent);
    tablePl.Text = tablePl.Text.Replace(tablePlaceholder, "");
    wd.MainDocumentPart.Document.Save();
}

string docText = wd.MainDocumentPart.Document.Body.OuterXml;
Regex regexText = new Regex("{{name}}");
docText = regexText.Replace(docText, "Claire " + DateTime.Now.ToString());
wd.MainDocumentPart.Document.Body = new Body(docText);
wd.MainDocumentPart.Document.Save();
.............

您在代码中查找 {{table}},但模板中的符号是 {{tableOfUsers}}