简体   繁体   中英

Edit a docx with open xml

I am trying to edit a word document, with open xml, but I don't have a satisfying result :

Here is the raw document :

在此处输入图片说明

Here, I just got a sentence to start the document. and after the : I must add several lines.

The result I want is something like this(for some reasons, I had to erase some datas on the screens, but this does not affect the question) :

在此处输入图片说明

But I only got a result like this :

在此处输入图片说明

So, in the third image, the text is not center and also does not have any tab. Plus, is is not in bold (if is is not underline, that is not a problem).

But the real problem is the fact that it is left align, with no tabs, while it should be center align with tabs.

Here is the code I have :

                                    Text text = sdtFils.Descendants<Text>().FirstOrDefault();
                                    string[] lst = variableCourrier.Tables[0].Rows[0][v_ligne["NOM_CHAMP"].ToString()].ToString().Split("$".ToCharArray());
                                    Paragraph existPar = sdtFils.Descendants<Paragraph>().FirstOrDefault();
                                    if (existPar == null)
                                    {
                                        existPar = sdtFils.Ancestors<Paragraph>().FirstOrDefault();
                                    }
                                    Run existRun = sdtFils.Descendants<Run>().FirstOrDefault();
                                    foreach (string str in lst)
                                    {
                                        Paragraph p = new Paragraph();

                                        ParagraphProperties pPr = new ParagraphProperties(new Tabs(new TabStop() { Val = TabStopValues.Center, Position = 7372 }),
                                            new Justification() { Val = JustificationValues.Left },
                                            new ParagraphStyleId() { Val = "Paragraphedeliste" },
                                            new Indentation() { Start = (int.Parse(existPar.ParagraphProperties.Indentation.Left.Value) + int.Parse(existPar.ParagraphProperties.Indentation.FirstLine.Value)).ToString() });

                                        string[] lstDetails = str.Split(";".ToCharArray());
                                        int j = 0;
                                        foreach (string strDetail in lstDetails)
                                        {
                                            if (!string.IsNullOrEmpty(strDetail))
                                            {
                                                TabChar tab = new TabChar();
                                                Text t = new Text();
                                                Run r = new Run();
                                                RunProperties runPr = new RunProperties(new FontSize() { Val = existRun.RunProperties.FontSize.Val });

                                                r.AppendChild(runPr);
                                                t.Text = j == 0 ? "" + strDetail : " " + strDetail;
                                                r.AppendChild(t);


                                                if (j != lstDetails.Length - 1)
                                                {
                                                    Break br = new Break();
                                                    r.AppendChild(br);
                                                }
                                                p.AppendChild(r);
                                                j++;
                                            }
                                        }

                                        text.Parent.Parent.Parent.Parent.InsertAfterSelf(p);
                                    }

With this code, I split each part of a string, so that I can put them into their lines.

I think the part that is not correct, is when I define the Paragraph element, but I don't see how to correct it, to obtain a reuslt that will look like the second image.

Anybody can help me with this?

If it is not clear enough, please tell me, I will edit the question.

Thank you.

You can use Open XML SDK 2.5 Productivity Tool (remember a .docx file is a collection of XML documents zipped into a single file), open your *.docx and click Reflect code to see the code needed to style the document properly.

在此处输入图片说明

Also you can use Open XML Package Editor for Visual Studio add-in that provides an easy way to parse and edit Open Packaging Conventions files, including Word, Excel, PowerPoint and Visio documents.

Everything is open source and available on Github.

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