在现有 docx 文档中动态添加合并字段

发布于 2024-11-26 06:20:29 字数 86 浏览 1 评论 0原文

是否可以在不使用互操作的情况下将合并字段添加到现有的 .docx 文档中,仅使用 CodeBehind 中的开放 SDK 进行处理?

Is it possible to add mergefields to an existing .docx document without using interop, only handling with open SDK from CodeBehind?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤独患者 2024-12-03 06:20:29

是的,这是可能的,我在下面创建了一个小方法,您只需传递要分配给合并字段的名称,它就会为您创建它。
下面的代码用于创建一个新文档,但使用该方法附加到现有文档应该很容易,希望这对您有帮助:

using System;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            using (WordprocessingDocument package = WordprocessingDocument.Create("D:\\ManualMergeFields.docx", WordprocessingDocumentType.Document))
            {
                package.AddMainDocumentPart();

                Paragraph nameMergeField = CreateMergeField("Name");
                Paragraph surnameMergeField = CreateMergeField("Surname");

                Body body = new Body();
                body.Append(nameMergeField);
                body.Append(surnameMergeField);
                package.MainDocumentPart.Document = new Document(new Body(body));
            }
        }

        static Paragraph CreateMergeField(string name)
        {
            if (!String.IsNullOrEmpty(name))
            {
                string instructionText = String.Format(" MERGEFIELD  {0}  \\* MERGEFORMAT", name);
                SimpleField simpleField1 = new SimpleField() { Instruction = instructionText };

                Run run1 = new Run();

                RunProperties runProperties1 = new RunProperties();
                NoProof noProof1 = new NoProof();

                runProperties1.Append(noProof1);
                Text text1 = new Text();
                text1.Text = String.Format("«{0}»", name);

                run1.Append(runProperties1);
                run1.Append(text1);

                simpleField1.Append(run1);

                Paragraph paragraph = new Paragraph();
                paragraph.Append(new OpenXmlElement[] { simpleField1 });
                return paragraph;
            }
            else return null;
        }
    }
}

您可以从此网址下载 Open Xml Productivity Tool(如果您还没有) )http://www.microsoft.com/download/en/details.aspx?id=5124
该工具具有“反映代码”功能。因此您可以在 MS Word 文档中手动创建合并字段,然后使用快捷会议工具打开该文档
并查看有关如何在代码中执行此操作的 C# 代码示例!它非常有效,我已经使用了这个确切的工具来创建上面的示例。祝你好运

Yes this is possible, I've created a little method below where you simply pass through the name you want to assign to the merge field and it creates it for you.
The code below is for creating a new document but it should be easy enough to use the method to append to an existing document, hope this helps you:

using System;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            using (WordprocessingDocument package = WordprocessingDocument.Create("D:\\ManualMergeFields.docx", WordprocessingDocumentType.Document))
            {
                package.AddMainDocumentPart();

                Paragraph nameMergeField = CreateMergeField("Name");
                Paragraph surnameMergeField = CreateMergeField("Surname");

                Body body = new Body();
                body.Append(nameMergeField);
                body.Append(surnameMergeField);
                package.MainDocumentPart.Document = new Document(new Body(body));
            }
        }

        static Paragraph CreateMergeField(string name)
        {
            if (!String.IsNullOrEmpty(name))
            {
                string instructionText = String.Format(" MERGEFIELD  {0}  \\* MERGEFORMAT", name);
                SimpleField simpleField1 = new SimpleField() { Instruction = instructionText };

                Run run1 = new Run();

                RunProperties runProperties1 = new RunProperties();
                NoProof noProof1 = new NoProof();

                runProperties1.Append(noProof1);
                Text text1 = new Text();
                text1.Text = String.Format("«{0}»", name);

                run1.Append(runProperties1);
                run1.Append(text1);

                simpleField1.Append(run1);

                Paragraph paragraph = new Paragraph();
                paragraph.Append(new OpenXmlElement[] { simpleField1 });
                return paragraph;
            }
            else return null;
        }
    }
}

You can download the Open Xml Productivity Tool from this url(if you do not already have it)http://www.microsoft.com/download/en/details.aspx?id=5124
This tool has a "Reflect Code" functionality.So you can manually create a merge field in an MS Word document and then open up the document with the Productivity Tool
and see a C# code sample on how to do this in code!It's very effective an I've used this exact tool to create the sample above.Good luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文