简体   繁体   中英

Get all content controls in .docx without word

I have to get all content controls in a Word document and insert information, but the PC where the program is run need not have Word installed.

I tried with:

List<SdtContentText> lista = parteDocumento.Document.Descendants<SdtContentText>().ToList();
 foreach (SdtContentText objeto in lista)

and changing SdtContentText to SdtBlock

EDIT:

Now i can obtain ContentControls but i can't edit their innerText

    List <OpenXmlElement> lista = parteDocumento.Document.Body.ToList().FirstOrDefault<OpenXmlElement>().ToList();

                        foreach (var objeto in lista)
                        {

                            if (objeto != null && objeto is SdtRun)
                            {
                                SdtRun objeto2 = (SdtRun)objeto; 
                    .....

to get all Text content controls and edit

 using (WordprocessingDocument doc = WordprocessingDocument.Open(document, true))
                    {

                        MainDocumentPart parteDocumento = doc.MainDocumentPart;

                        foreach (SdtElement objeto in parteDocumento.Document.Descendants<SdtElement>().ToList())
                        {

                        foreach (Text t in objeto.Descendants<Text>().ToList())
                        {

                            if (t.Text == "nombre")
                            {
                                t.Text = persona.nombre;
                            }
                            if (t.Text == "primerApellido")
                            {
                                t.Text = persona.primerApellido;
                            }
                            if (t.Text == "segundoApellido")
                            {
                                t.Text = persona.segundoApellido;
                            }
                            if (t.Text == "nacionalidad")
                            {
                                t.Text = persona.nacionalidad;
                            }

                        }

                        }
                    parteDocumento.Document.Save();
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM