0

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

Example:

(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;
}
Borislav Ivanov
  • 4,684
  • 3
  • 31
  • 55
MrM
  • 21,709
  • 30
  • 113
  • 139

1 Answers1

0

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

Jens Kloster
  • 11,099
  • 5
  • 40
  • 54