如何使用Open XML SDK在C#中创建一个复选框 [英] How do I create a check box in C# using Open XML SDK

查看:126
本文介绍了如何使用Open XML SDK在C#中创建一个复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#中的Open XML SDK创建一定数量的复选框.我该怎么办?

I would like to create a certain number of checkbox using Open XML SDK in C#. How would I do that?

示例:

(Checkbox) - Shoes 

(Checkbox) - Shirt

复选框的数量也有所不同.我正在阅读模板文档,然后进行编辑以返回.到目前为止,我有这样的事情:

The checkbox count also varies. I am reading a template document, then make edits to return. I have something like this so far:

string documentText;
using (StreamReader reader ...)
{
    documentText = reader.ReadToEnd();
}

string addClothes = "";
Run newCheckBox = GenerateRun();
foreach(var item in ClothesList)
{
    addClothes = item.clothing;
    //MY DILEMMA
    documentText = documentText.Replace("##clothing##", newCheckBox + addClothes + "NewLine");
}


public Run GenerateRun()
{
    Run run1 = new Run() { RsidRunProperties = "004C0D9A", RsidRunAddition = "00850FA5" };
    FieldCode fieldCode1 = new FieldCode() { Space = SpaceProcessingModeValues.Preserve };
    fieldCode1.Text = " FORMCHECKBOX ";

    run1.Append(fieldCode1);
    return run1;
}

推荐答案

使用OpenXML SDK,我认为它是这样的: (原始复制/粘贴--1636166143的值可能不正确)

using the OpenXML SDK i think it goes something like this: (raw copy/paste - the value of -1636166143 might be way off)

using w14 = DocumentFormat.OpenXml.Office2010.Word

            SdtRun sdtRun1 = new SdtRun();

            SdtProperties sdtProperties1 = new SdtProperties();
            SdtId sdtId1 = new SdtId(){ Val = -1636166143 };

            W14.SdtContentCheckBox sdtContentCheckBox1 = new W14.SdtContentCheckBox();
            W14.Checked checked1 = new W14.Checked(){ Val = W14.OnOffValues.Zero };
            W14.CheckedState checkedState1 = new W14.CheckedState(){ Font = "MS Gothic", Val = "2612" };
            W14.UncheckedState uncheckedState1 = new W14.UncheckedState(){ Font = "MS Gothic", Val = "2610" };

            sdtContentCheckBox1.Append(checked1);
            sdtContentCheckBox1.Append(checkedState1);
            sdtContentCheckBox1.Append(uncheckedState1);

            sdtProperties1.Append(sdtId1);
            sdtProperties1.Append(sdtContentCheckBox1);

            SdtContentRun sdtContentRun1 = new SdtContentRun();

            Run run1 = new Run();

            RunProperties runProperties1 = new RunProperties();
            RunFonts runFonts1 = new RunFonts(){ Hint = FontTypeHintValues.EastAsia, Ascii = "MS Gothic", HighAnsi = "MS Gothic", EastAsia = "MS Gothic" };

            runProperties1.Append(runFonts1);
            Text text1 = new Text();
            text1.Text = "☐";

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

            sdtContentRun1.Append(run1);

复选框之后,您想要的任何文本都在上述代码之后

Any text you want after the checkbox, comes after the above code

这篇关于如何使用Open XML SDK在C#中创建一个复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆