Characters being dropped when doing textobj.Free

  • I have a WPRichText doc that I use as a template multiple times to build a single report from. It might be correct 9 times out of 10, but the tenth time it drops a character. For example, part of the template has the word "Comments:", and it comes out fine most of the time, but when it fails, it always drops the "o", and I get "Cmments:"

    I also have a second template that might say "Report as in need of repair", but comes out as "Repor as in need of repair", dropping the "t" in Report. If I delete the "R", I get "eportas in need of repair" where the "t" comes back but drops the space between "Report" and "as".

    So it appears to be a positional issue... perhaps an alignment correction that overlaps?

    I might have a problem coming up with an example for you, but I might be able to send you RTF of the templates and resulting output if you think that might help. I'll go ahead and send you image snapshots to you can "see" what I'm talking about ayway.

    Obviously this could have huge ramifications... so let me know how i can help!!

    WPTools 5.20.5
    Delphi 7

  • It has something to do with bookmarks. I went back through my code releases, and it started occuring when I added bookmarks to be able to build a Table Of Contents.

    If I remove the following three lines of code, the problem goes away:

    Code
    WPRichText1.BookmarkInput( '_Toc_' + TNavDocument(node.Data).ID, true );
    WPRichText1.ActiveParagraph.ASet(WPAT_ParIsOutline,2);
    WPRichText1.InputString(nodeDesc);


    Does that give you any clues???

  • Another clue for you all:

    When I go to append the mailmerged template to the report, I use:

    Code
    dest.SelectionAsString := src.AsANSIString('WPTOOLS');

    However, if I change it to:

    Code
    dest.SelectionAsString := src.AsString;


    The problem goes away, but the downside is that some other attributes get lost, like paragraph borders.

    Can't win!

  • The paragraph borders and center justify that append fine on the AsANSIString but not on an AsString is when the last paragraph in the source is the paragraph with those attributes. If I add a another paragraph at the end, AsString works and I get the paragraph border and the center justification. So I guess that's a bug?

    There are other issues with formatting on the last paragraph, like tabstops, etc that are not handled when using AsString to append. So if I use AsANSIString('WPTOOLS'), I lose characters when using bookmarks but I get correct formatting on ending paragraphs. But I don't lose chars when using AsString, but I lose last paragraph formatting.

    What to do???

  • More info:

    I traced it down to a particular function: DeleteMarkedChar

    What I do after generating the report is to delete various text objects like special hyperlinks that I don't want to be hyperlinks in a PDF doc for example (I use hyperlinks for checkboxes, so I don't want the PDF to indicate that checkboxes are hyperlinked).

    After freeing all the start and end objects, I call DeleteMarkedChar to delete the object placeholders themselves.

    But since I explicitly free the objects rather than simply doing the "unlink", do I really need to call DeleteMarkedChar? If I comment out the DeleteMarkedChar, I don't lose those chars that I mentioned at the start of this thread.

    Even so, I wouldn't think DeleteMarkedChar should do what it is doing. Apparently having bookmarks caused DeleteMarkedChar to delete something it wasn't supposed to be deleting. Perhaps the bookmarks were causing a cursor position issue?

    Sorry for the many updates I've added to this thread, but I couldn't let it go even though it is 2am my time!

    • Offizieller Beitrag

    Hi,

    If you free a TWPTextObject it calls Unlink which removes it from the parent paragraph. It does not delete the placeholder in this moment since this would change the position of other text and placeholders and would very likely disturb the code which originally freed the object.

    The placeholders are cleared and marked for later deletion which is done by DeleteMarkedChar. If you delete a pair of objects you need to call DeleteMarkedChar after start and end object was deleted (and not twice)

    Or, best, call wp.DeleteMarkedChar(true) at the end - when all paragraphs have been processed.


    Julian

  • Well, then that there lies the problem. I do the DeleteMarkedChar at the end, and this is where the chars get dropped. And they get dropped only when I have bookmarks at the beginning of that paragraph.

    So please look at how you deal with DeleteMarkedChars when a bookmark is in place. Now that I've narrowed it down, I might be able to create a sample that demos the issue if needed.

    Did you also see the issue about appending using AsString vs AsANSIString('WPTOOLS') as to AsString looses the last paragraph's formatting such as tabstops, justification, and paragraph borders?

    • Offizieller Beitrag

    Hi,

    Thanks for your demo project. I can see the problem and finally found the reason.

    I have the following solution for you which works without an update of the engine.

    place
    endObj.Free;
    obj.Free;

    with
    endObj.ParentPar.DeleteChar(endObj.ParentPosInPar);//1.
    obj.ParentPar.DeleteChar(obj.ParentPosInPar);//2.

    DeleteMarkedChar is not required then.

    ABout the text append problem, do <ou have a sample here. It depends a bit on the destination text if appended text looses its attribute in first paragraph - if the paragraph is not empty (it has a bookmark) the attributes of that paragrap override the append one, thats no bug.

    Julian