Novacode.Paragraph.InsertText C# (CSharp) Method

InsertText() public method

Inserts a specified instance of System.String into a Novacode.DocX.Paragraph at a specified index position.
public InsertText ( int index, string value, bool trackChanges = false, Formatting formatting = null ) : void
index int The index position of the insertion.
value string The System.String to insert.
trackChanges bool Flag this insert as a change.
formatting Formatting The text formatting.
return void
        public void InsertText(int index, string value, bool trackChanges = false, Formatting formatting = null)
        {
            // Timestamp to mark the start of insert
            DateTime now = DateTime.Now;
            DateTime insert_datetime = new DateTime(now.Year, now.Month, now.Day, now.Hour, now.Minute, 0, DateTimeKind.Utc);

            // Get the first run effected by this Insert
            Run run = GetFirstRunEffectedByEdit(index);

            if (run == null)
            {
                object insert;
                if (formatting != null) //not sure how to get original formatting here when run == null
                    insert = HelperFunctions.FormatInput(value, formatting.Xml);
                else
                    insert = HelperFunctions.FormatInput(value, null);

                if (trackChanges)
                    insert = CreateEdit(EditType.ins, insert_datetime, insert);
                Xml.Add(insert);
            }

            else
            {
                object newRuns;
                var rprel = run.Xml.Element(XName.Get("rPr", DocX.w.NamespaceName));
                if (formatting != null)
                {
                    //merge 2 formattings properly
                    Formatting finfmt = null;
                    Formatting oldfmt = null;

                    if (rprel != null)
                    {
                        oldfmt = Formatting.Parse(rprel);
                    }

                    if (oldfmt != null)
                    {
                        finfmt = oldfmt.Clone();
                        if (formatting.Bold.HasValue) { finfmt.Bold = formatting.Bold; }
                        if (formatting.CapsStyle.HasValue) { finfmt.CapsStyle = formatting.CapsStyle; }
                        if (formatting.FontColor.HasValue) { finfmt.FontColor = formatting.FontColor; }
                        finfmt.FontFamily = formatting.FontFamily;
                        if (formatting.Hidden.HasValue) { finfmt.Hidden = formatting.Hidden; }
                        if (formatting.Highlight.HasValue) { finfmt.Highlight = formatting.Highlight; }
                        if (formatting.Italic.HasValue) { finfmt.Italic = formatting.Italic; }
                        if (formatting.Kerning.HasValue) { finfmt.Kerning = formatting.Kerning; }
                        finfmt.Language = formatting.Language;
                        if (formatting.Misc.HasValue) { finfmt.Misc = formatting.Misc; }
                        if (formatting.PercentageScale.HasValue) { finfmt.PercentageScale = formatting.PercentageScale; }
                        if (formatting.Position.HasValue) { finfmt.Position = formatting.Position; }
                        if (formatting.Script.HasValue) { finfmt.Script = formatting.Script; }
                        if (formatting.Size.HasValue) { finfmt.Size = formatting.Size; }
                        if (formatting.Spacing.HasValue) { finfmt.Spacing = formatting.Spacing; }
                        if (formatting.StrikeThrough.HasValue) { finfmt.StrikeThrough = formatting.StrikeThrough; }
                        if (formatting.UnderlineColor.HasValue) { finfmt.UnderlineColor = formatting.UnderlineColor; }
                        if (formatting.UnderlineStyle.HasValue) { finfmt.UnderlineStyle = formatting.UnderlineStyle; }
                    }
                    else
                    {
                        finfmt = formatting;
                    }

                    newRuns = HelperFunctions.FormatInput(value, finfmt.Xml);
                }
                else
                {
                    newRuns = HelperFunctions.FormatInput(value, rprel);
                }

                // The parent of this Run
                XElement parentElement = run.Xml.Parent;
                switch (parentElement.Name.LocalName)
                {
                    case "ins":
                        {
                            // The datetime that this ins was created
                            DateTime parent_ins_date = DateTime.Parse(parentElement.Attribute(XName.Get("date", DocX.w.NamespaceName)).Value);

                            /*
                             * Special case: You want to track changes,
                             * and the first Run effected by this insert
                             * has a datetime stamp equal to now.
                            */
                            if (trackChanges && parent_ins_date.CompareTo(insert_datetime) == 0)
                            {
                                /*
                                 * Inserting into a non edit and this special case, is the same procedure.
                                */
                                goto default;
                            }

                            /*
                             * If not the special case above,
                             * then inserting into an ins or a del, is the same procedure.
                            */
                            goto case "del";
                        }

                    case "del":
                        {
                            object insert = newRuns;
                            if (trackChanges)
                                insert = CreateEdit(EditType.ins, insert_datetime, newRuns);

                            // Split this Edit at the point you want to insert
                            XElement[] splitEdit = SplitEdit(parentElement, index, EditType.ins);

                            // Replace the origional run
                            parentElement.ReplaceWith
                            (
                                splitEdit[0],
                                insert,
                                splitEdit[1]
                            );

                            break;
                        }

                    default:
                        {
                            object insert = newRuns;
                            if (trackChanges && !parentElement.Name.LocalName.Equals("ins"))
                                insert = CreateEdit(EditType.ins, insert_datetime, newRuns);

                            // Special case to deal with Page Number elements.
                            //if (parentElement.Name.LocalName.Equals("fldSimple"))
                            //    parentElement.AddBeforeSelf(insert);

                            else
                            {
                                // Split this run at the point you want to insert
                                XElement[] splitRun = Run.SplitRun(run, index);

                                // Replace the origional run
                                run.Xml.ReplaceWith
                                (
                                    splitRun[0],
                                    insert,
                                    splitRun[1]
                                );
                            }

                            break;
                        }
                }
            }

            HelperFunctions.RenumberIDs(Document);
        }

Same methods

Paragraph::InsertText ( string value, bool trackChanges = false, Formatting formatting = null ) : void

Usage Example

Example #1
0
        public virtual Paragraph InsertParagraph(int index, string text, bool trackChanges, Formatting formatting)
        {
            Paragraph newParagraph = new Paragraph(Document, new XElement(DocX.w + "p"), index);
            newParagraph.InsertText(0, text, trackChanges, formatting);

            Paragraph firstPar = HelperFunctions.GetFirstParagraphEffectedByInsert(Document, index);

            if (firstPar != null)
            {
                XElement[] splitParagraph = HelperFunctions.SplitParagraph(firstPar, index - firstPar.startIndex);

                firstPar.Xml.ReplaceWith
                (
                    splitParagraph[0],
                    newParagraph.Xml,
                    splitParagraph[1]
                );
            }

            else
                Xml.Add(newParagraph);

            GetParent(newParagraph);

            return newParagraph;
        }