Copy link to clipboard
Copied
Hi,
I am getting TextItems out of an element using:
currentElem.GetText(Constants.FTF_ALL);
I am then modifying the sdata for some of the items, and want to write this structure back into the element I got it from.
If I just use doc.Clear then doc.AddText, I have to pass in a string, and thus lose all the markers, anchors etc in the TextItems.
I can't find a documented way to pass in TextItems to a function, anyone got ideas/workarounds to preserve the non-string text items?
Or a way to modify the TextItems on an object directly?
Thanks in advance,
Dan
1 Correct answer
No, that is not how it works.
If you want to replace text in some of the text items, you need to make changes to the individual text strings, not to the entire contents of the element. Something like this:
1. Get all text items in the element
2. Loop through all text items
3. For each text item of type string that requires replacing:
Set TextLoc 1 to point to the start of that string ( offset property of your text item )
Set TextLoc 2 to point to the end of the string ( offset property plus
...Copy link to clipboard
Copied
No, that is not how it works.
If you want to replace text in some of the text items, you need to make changes to the individual text strings, not to the entire contents of the element. Something like this:
1. Get all text items in the element
2. Loop through all text items
3. For each text item of type string that requires replacing:
Set TextLoc 1 to point to the start of that string ( offset property of your text item )
Set TextLoc 2 to point to the end of the string ( offset property plus length of text string )
Create a text range from these two TextLoc objects
Set the TextSelection property of your Doc object to this text range
Use the Doc.Clear method
Add new text to the document, using TextLoc 1.
Note that after doing this you should get the text items again, as the offsets might have changed by replacing text. The way to get around this hassle is to work through the list of text items from the back to the front.
Good luck
4everJang
Copy link to clipboard
Copied
Perfect!
This makes so much sense now, I was already looping through the text items to modify them, so I feel silly for not putting 2 and 2 together.
Thanks for your help!
Copy link to clipboard
Copied
Just make sure to work from back to front - as shown in Rick's example - as the changes in the offsets will not affect the offsets of text items before it.
Copy link to clipboard
Copied
Hi,
I have a function that just deletes the strings from a text range. I am not sure if this will help you, but it may get you started.
Rick
function deleteStrings (textRange, doc) {
var textItems, i, tRange;
// Get a list of strings in the text range.
textItems = doc.GetTextForRange (textRange, Constants.FTI_String);
for (i = textItems.len - 1; i >= 0; i -= 1) {
// Make a text range from the string and delete it.
tRange = new TextRange (
new TextLoc (textRange.beg.obj, textItems.offset),
new TextLoc (textRange.beg.obj, textItems.offset + textItems.sdata.length));
doc.DeleteText (tRange);
}
}

