RadDocument CaretPosition Exception: inline does not belong to this document

1 Answer 21 Views
RichTextBox
Steve
Top achievements
Rank 2
Iron
Iron
Iron
Steve asked on 04 Oct 2024, 04:29 PM | edited on 04 Oct 2024, 04:31 PM

Hi,

We are getting an exception when trying to set the CaretPosition in a RadDocument.

The Document is created in code:


        public void PurchaseTemplate()
        {
            try
            {
                RadDocumentEditor editor = new RadDocumentEditor(doc_text.Document);
                string[] rowName = new string[] { "Make", "Model", "Technical Spec.", "Order No.", "Date Ordered", "Date Delivered", "Price", "Payment Method", "Supplier", "Tel.", "Other Details", "Manufacturer", "Tel.", "Other Details", "Guarantee", "Period", "Expiry Date", "Notes" };

                TableWidthUnit w1 = new TableWidthUnit(180);    // Date
                TableWidthUnit w2 = new TableWidthUnit(500);    // Day

                TableCell cell1 = new TableCell();
                TableCell cell2 = new TableCell();

                TableRow row = new TableRow();
                Paragraph p1 = new Paragraph();
                Span s1 = new Span();
                string subject = " ";
                if (_selelement != null)
                {
                    subject = _selelement.GetAttribute("title");
                }


                Table table = new Table();
                table.StyleName = RadDocumentDefaultStyles.DefaultTableGridStyleName;
                table.Tag = "5000";
                table.Rows.ElementAdded += doc_text_DiaryChanged;

                cell1.Background = Color.FromRgb(242, 219, 219);
                cell1.PreferredWidth = w1;
                cell1.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
                s1.FontWeight = FontWeights.Bold;
                s1.FontFamily = new FontFamily(CGlobals.docu_default_font);
                s1.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
                s1.Text = "Item";
                p1.Inlines.Add(s1);
                cell1.Blocks.Add(p1);

                p1 = new Paragraph();
                s1 = new Span();
                cell2.Background = Color.FromRgb(242, 219, 219);
                cell2.PreferredWidth = w2;
                cell2.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
                s1.FontWeight = FontWeights.Bold;
                s1.FontFamily = new FontFamily(CGlobals.docu_default_font);
                s1.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
                s1.Text = subject;
                p1.Inlines.Add(s1);
                cell2.Blocks.Add(p1);

                row.Cells.Add(cell1);
                row.Cells.Add(cell2);
                table.Rows.Add(row);
                int nRow = 0;
                foreach (string name in rowName)
                {
                    row = new TableRow();
                    cell1 = new TableCell();
                    cell2 = new TableCell();
                    switch (nRow)
                    {
                        default:
                            p1 = new Paragraph();
                            s1 = new Span();
                            cell1.Background = Color.FromRgb(255, 255, 255);
                            cell1.PreferredWidth = w1;
                            cell1.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
                            s1.FontWeight = FontWeights.Bold;
                            s1.FontFamily = new FontFamily(CGlobals.docu_default_font);
                            s1.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
                            s1.Text = name;
                            p1.Inlines.Add(s1);
                            cell1.Blocks.Add(p1);

                            p1 = new Paragraph();
                            s1 = new Span();
                            cell2.Background = Color.FromRgb(255, 255, 255);
                            cell2.PreferredWidth = w2;
                            cell2.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
                            s1.FontFamily = new FontFamily(CGlobals.docu_default_font);
                            s1.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
                            s1.Text = " ";
                            p1.Inlines.Add(s1);
                            cell2.Blocks.Add(p1);

                            row.Cells.Add(cell1);
                            row.Cells.Add(cell2);
                            table.Rows.Add(row);
                            break;
                        case 9:
                        case 10:
                        case 12:
                        case 13:
                        case 15:
                        case 16:
                            DocumentList bullets = new DocumentList(DefaultListStyles.Bulleted, doc_text.Document);
                            p1 = new Paragraph();
                            s1 = new Span();
                            cell1.Background = Color.FromRgb(255, 255, 255);
                            cell1.PreferredWidth = w1;
                            cell1.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
                            s1.FontFamily = new FontFamily(CGlobals.docu_default_font);
                            s1.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
                            s1.Text = name;
                            p1.Inlines.Add(s1);
                            bullets.AddParagraph(p1);
                            cell1.Blocks.Add(p1);

                            p1 = new Paragraph();
                            s1 = new Span();
                            cell2.Background = Color.FromRgb(255, 255, 255);
                            cell2.PreferredWidth = w2;
                            cell2.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
                            s1.FontFamily = new FontFamily(CGlobals.docu_default_font);
                            s1.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
                            s1.Text = " ";
                            p1.Inlines.Add(s1);
                            cell2.Blocks.Add(p1);

                            row.Cells.Add(cell1);
                            row.Cells.Add(cell2);
                            table.Rows.Add(row);
                            break;
                        case 17:
                            p1 = new Paragraph();
                            s1 = new Span();
                            cell1.Background = Color.FromRgb(255, 255, 255);
                            cell1.PreferredWidth = w1;
                            cell1.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
                            cell1.RowSpan = 7;
                            s1.FontWeight = FontWeights.Bold;
                            s1.FontFamily = new FontFamily(CGlobals.docu_default_font);
                            s1.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
                            s1.Text = name;
                            p1.Inlines.Add(s1);
                            cell1.Blocks.Add(p1);

                            p1 = new Paragraph();
                            s1 = new Span();
                            cell2.Background = Color.FromRgb(255, 255, 255);
                            cell2.PreferredWidth = w2;
                            cell2.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
                            s1.FontFamily = new FontFamily(CGlobals.docu_default_font);
                            s1.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
                            s1.Text = " ";
                            p1.Inlines.Add(s1);
                            cell2.Blocks.Add(p1);

                            row.Cells.Add(cell1);
                            row.Cells.Add(cell2);
                            table.Rows.Add(row);
                            for (int x = 0; x < 6; x++)
                            {
                                row = new TableRow();
                                cell2 = new TableCell();
                                p1 = new Paragraph();
                                s1 = new Span();
                                cell2.Background = Color.FromRgb(255, 255, 255);
                                cell2.PreferredWidth = w2;
                                cell2.Padding = new Telerik.Windows.Documents.Layout.Padding(3, 6, 3, 6);
                                s1.FontFamily = new FontFamily(CGlobals.docu_default_font);
                                s1.FontSize = Unit.PointToDip(CGlobals.docu_default_font_size);
                                s1.Text = " ";
                                p1.Inlines.Add(s1);
                                cell2.Blocks.Add(p1);
                                row.Cells.Add(cell2);
                                table.Rows.Add(row);
                            }
                            break;
                    }
                    nRow++;

                }
                editor.InsertTable(table);

                // Insert a field so we can identify the type of template
                //var cell = table.EnumerateChildrenOfType<TableCell>().FirstOrDefault();
                var cell = table.EnumerateChildrenOfType<TableCell>().FirstOrDefault();
                if (cell != null)
                {
                    DocumentPosition position = new DocumentPosition(doc_text.Document);
                    position.MoveToDocumentElementStart(cell.Blocks.First());
                    DocumentVariableField docVariable = new DocumentVariableField() { VariableName = "5000" };
                    editor.InsertField(docVariable);
                }
                doc_text.Document.DocumentVariables["5000"] = "";
                var field = doc_text.Document.EnumerateChildrenOfType<FieldRangeStart>().Where(x => x.Field.FieldTypeName == "DOCVARIABLE"
                && ((DocumentVariableField)x.Field).VariableName == "5000").FirstOrDefault();
                if (field != null)
                {
                    doc_text.UpdateField(field);
                }
                // Add the Datepickers
                var hrow5 = table.EnumerateChildrenOfType<TableRow>().ToList()[5];
                var hrow6 = table.EnumerateChildrenOfType<TableRow>().ToList()[6];
                var hrow17 = table.EnumerateChildrenOfType<TableRow>().ToList()[17];
                AddPurchaseDatePicker(table, hrow5);
                AddPurchaseDatePicker(table, hrow6);
                AddPurchaseDatePicker(table, hrow17);
            }
            catch (Exception e)
            {
                CGlobals cg = new CGlobals();
                cg.ShowError("TemplateHandler:PurchaseTemplate Error\n" + e.Message, 3);

            }
        }

We are then trying to insert a DateProperties into 3 cells in the table created using AddPurchaseDatePicker(Table, TableRow) :


        public void AddPurchaseDatePicker(Table table, TableRow row)
        {
            try
            {
                RadDocument document = doc_text.Document;
                RadDocumentEditor editor = new RadDocumentEditor(document);
                var dateCell = row.Cells.ToList()[1];
                DocumentPosition position = new DocumentPosition(document);
                string dateVal = string.Empty;

                var span = dateCell.EnumerateChildrenOfType<Span>().FirstOrDefault();
                if (span != null)
                {
                    dateVal = span.Text;
                    span.Text = " ";
                }
                position.MoveToDocumentElementStart(dateCell.Blocks.First());
                document.CaretPosition.MoveToPosition(position);
                editor.ChangeFontFamily(new FontFamily(CGlobals.docu_default_font));
                editor.ChangeFontSize(Unit.PointToDip(CGlobals.docu_default_font_size));
                DateProperties dataProperties = new DateProperties();
                try
                {
                    dataProperties.FullDate = DateTime.Parse(dateVal);
                }
                catch { }
               
                dataProperties.IsTemporary = false;
                dataProperties.StyleName = "TemplateFields";
                editor.InsertStructuredDocumentTag(dataProperties);
                doc_text.Document = document;
                table.Rows.ElementAdded += doc_text_NoteChanged;
            }
            catch { }
        }

When we try and set the CaretPosition with 

document.CaretPosition.MoveToPosition(position);

we recieve the following exception:

System.InvalidOperationException
  HResult=0x80131509
  Message=inline does not belong to this document.
  Source=Telerik.Windows.Controls.RichTextBox
  StackTrace:
   at Telerik.Windows.Documents.Model.RadDocument.GetContainingAnnotationRanges[TRangeStart](Inline inline, Predicate`1 filter, Boolean inclusive)
   at Telerik.Windows.Documents.Model.RadDocument.GetContainingAnnotationRanges[TRangeStart](Inline inline, Boolean inclusive)
   at Telerik.Windows.Documents.DocumentPosition.get_IsPositionInSkipPositionRange()
   at Telerik.Windows.Documents.DocumentPosition.get_IsValidPosition()
   at Telerik.Windows.Documents.DocumentPosition.Validator.EnsureValidPosition(DocumentPosition position, Func`1 shouldSearchForward)
   at Telerik.Windows.Documents.DocumentPosition.OnPositionChanged()
   at Telerik.Windows.Documents.Utils.SuspendChangingAndChangedActionCounter.InvokeChangedActionInternal()
   at Telerik.Windows.Documents.Utils.SuspendChangingAndChangedActionCounter.InvokeChanged()
   at Telerik.Windows.Documents.DocumentPosition.MoveToPosition(DocumentPosition newPosition)
   at DocuPraxis.MainWindow.AddPurchaseDatePicker(Table table, TableRow row) in E:\Source\Repos\Panarc\DocuPraxisTel\DocuPraxis\DocuPraxisTemplateHandler.cs:line 1634

We are using a similar method to add DateProperties in other generated documents without problem, e.g.:


        public void AddNotesDatePicker(Table table, TableRow row)
        {
            try
            {
                RadDocument document = doc_text.Document;
                RadDocumentEditor editor = new RadDocumentEditor(document);
                var dateCell = row.Cells.ToList()[0];
                DocumentPosition position = new DocumentPosition(document);
                string dateVal = string.Empty;
                
                var span = dateCell.EnumerateChildrenOfType<Span>().FirstOrDefault();
                if (span != null)
                {
                    dateVal = span.Text;
                    if(String.IsNullOrEmpty(dateVal) || dateVal == " " )
                    {
                        dateVal = DateTime.Now.ToString("dd/MM/yyyy");
                    }
                    span.Text = " ";
                }
                position.MoveToDocumentElementStart(dateCell.Blocks.First());
                document.CaretPosition.MoveToPosition(position);
                editor.ChangeFontFamily(new FontFamily(CGlobals.docu_default_font));
                editor.ChangeFontSize(Unit.PointToDip(CGlobals.docu_default_font_size));
                DateProperties dataProperties = new DateProperties();
                dataProperties.FullDate = DateTime.Parse(dateVal);
                dataProperties.IsTemporary = false;
                dataProperties.StyleName = "TemplateFields";
                editor.InsertStructuredDocumentTag(dataProperties);
                doc_text.Document = document;
                table.Rows.ElementAdded += doc_text_NoteChanged;
            }
            catch { }
        }
What are we doing wrong?

1 Answer, 1 is accepted

Sort by
1
Accepted
Dimitar
Telerik team
answered on 07 Oct 2024, 09:08 AM

Hello Steve,

The InsertTable method will create a deep copy of the table and its child elements. This is why you can no longer use the table instance after it is inserted. You should get the table instance from the document: 

editor.InsertTable(table);
table = doc_text.Document.EnumerateChildrenOfType<Table>().FirstOrDefault();

I hope this helps. Should you have any other questions do not hesitate to ask.

Regards,
Dimitar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Steve
Top achievements
Rank 2
Iron
Iron
Iron
commented on 10 Oct 2024, 06:58 AM

Hi Dimitar,

Thank you, yet again.

That has solved the problem. Should we always get the table instance from the document after using the InsertTable method?

Kind regards,

Steve.

Dimitar
Telerik team
commented on 10 Oct 2024, 07:02 AM

Hi Steve, 

Yes, this should be done each time. 

Let me know if you have additional questions.

Tags
RichTextBox
Asked by
Steve
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Dimitar
Telerik team
Share this question
or