Esempio n. 1
0
        public void ReplaceText(string oldtext, string newtext, string mode = "PLAIN")
        {
            string docText = null;
            Body   body    = _document.MainDocumentPart.Document.Body;
            Text   item    = body.Descendants <Text>().Where(t => t.Text == oldtext).FirstOrDefault();

            if (item != null)
            {
                if (mode.Equals("PLAIN"))
                {
                    item.Text = newtext;
                }
                else
                {
                    /*
                     * Run currentRun = (Run) item.Parent;
                     * currentRun.RemoveAllChildren<Text>();
                     */

                    wp.Paragraph currentParagraph = (wp.Paragraph)item.Parent.Parent;
                    Run          currentRun       = (Run)item.Parent;
                    currentRun.RemoveAllChildren <Text>();

                    //Gestione a capo
                    string[] splitted = newtext.Split('\n');
                    if (splitted.Length > 1)
                    {
                        item.Text = splitted[0];
                        Text currentTextBlock = item;

                        /*
                         * Text newTextBlock = new Text(splitted[0]);
                         * currentRun.Append(newTextBlock);
                         */

                        Text newTextBlock = new Text(splitted[0]);
                        currentRun.Append(newTextBlock);
                        for (int idx = 1; idx < splitted.Length; idx++)
                        {
                            Run newRun = new Run();
                            newRun.RunProperties = (RunProperties)currentRun.RunProperties.CloneNode(true);
                            newTextBlock         = new Text(splitted[idx]);
                            newRun.Append(newTextBlock);
                            currentRun = newRun;
                            wp.Paragraph newParagraph = new wp.Paragraph(newRun);
                            newParagraph.ParagraphProperties = (ParagraphProperties)currentParagraph.ParagraphProperties.CloneNode(true);
                            currentParagraph.InsertAfterSelf(newParagraph);
                            currentParagraph = newParagraph;

                            /*
                             * wp.Break b = new wp.Break();
                             * currentTextBlock.InsertAfterSelf(b);
                             * Text newTextBlock = new Text(splitted[idx]);
                             * b.InsertAfterSelf(newTextBlock);
                             * currentTextBlock = newTextBlock;
                             */

                            /*
                             * Run newRun = new Run();
                             * newRun.RunProperties = (RunProperties) currentRun.RunProperties.CloneNode(true);
                             * newTextBlock = new Text(splitted[idx]);
                             * newRun.AppendChild(newTextBlock);
                             * currentRun.InsertAfterSelf(newRun);
                             * newTextBlock.InsertAfterSelf(new DocumentFormat.OpenXml.Wordprocessing.Break());
                             * currentRun = newRun;
                             */
                        }
                    }
                }
            }
            using (StreamWriter sw = new StreamWriter(Document.MainDocumentPart.GetStream(FileMode.Create)))
            {
                sw.Write(docText);
            }
        }