Symptom
How do I modify the text in a TextObject using Report Application Server (RAS) and Visual Basic .NET?
Cause
The TextObject contains a Paragraphs collection. Each Paragraph can be considered a new line in the TextObject. Each Paragraph has a ParagraphElements collection. Each ParagraphElement can be a string, or a type of field. A single string or word can have several elements.
For example, if "Hello World" is typed into a TextObject, there would be one ParagraphElement. If the font for the word "World" is made bold (World), there are now two ParagraphElements.
Resolution
Use the following sample code to modify the TextObject. This sample assumes that the text object has a single Paragraph with a single ParagraphElement.
Dim boTextObject As CrystalDecisions.ReportAppServer.ReportDefModel.TextObject
Dim boParagraph As New Paragraph
Dim boParagraphTextElement As ParagraphTextElement
boTextObject = CType(boReportClientDocument.ReportDefController.ReportDefinition.FindObjectByName("Title"), CrystalDecisions.ReportAppServer.ReportDefModel.TextObject)
'This way the font information is preserved.
'Set the text for the ParagraphElement.
boParagraphTextElement.Text = "New Title"
boParagraph.ParagraphElements.Add(boParagraphTextElement)
boTextObject.Paragraphs.RemoveAll()
boTextObject.Paragraphs.Add(boParagraph)
'The first parameter is the ReportObject to modify
'The second parameter is the modified ReportObject.
'In this case they can be the same object.
boReportClientDocument.ReportDefController.ReportObjectController.Modify(boTextObject, boTextObject)
See Also
If there is a newline\ line feed in the text object or text is spread across more than 1 line then eacxh line is treated as 1 paragraph. If you want to modify text over multiple lines you have to edit the text of the ParagraphTextElement for each Paragraph.
Here is a sample code snippet:
Dim obj As CrystalDecisions.ReportAppServer.ReportDefModel.TextObject = CType(rcd.ReportDefController.ReportObjectController.GetReportObjectsByKind(CrReportObjectKindEnum.crReportObjectKindText)(0), CrystalDecisions.ReportAppServer.ReportDefModel.TextObject)
Dim i As Integer
For i = 0 To obj.Paragraphs.Count - 1
Dim boParagraphTextElement As ParagraphTextElement = obj.Paragraphs(i).ParagraphElements(0)
boParagraphTextElement.Text = "new Text"
boParagraphTextElement.Kind = CrParagraphElementKindEnum.crPararaphElementKindText
Dim newParagraph As Paragraph = new Paragraph()
newParagraph.ParagraphElements.Add(boParagraphTextElement)
obj.Paragraphs.Remove(i)
obj.Paragraphs.Insert(i)
Next
rcd.ReportDefController.ReportObjectController.Modify(obj, obj)
Keywords
RAS, TextObject, Text, change, modify, set , 9481490 , KBA , BI-DEV-NET-CMC , Obsolete, not used anymore , How To