Problem pasting text in a document with styles

  • Hi,

    I'm having some trouble pasting text from another text editor in a TWPRichText with styles.
    Some info about my project first: my WPRichText has ClipboardOptions := [wpcoDontPasteFonts], ProtectedProp := [ppAllExceptForEditFields], and I created two styles with diferent font types, font sizes, linespacing, etc. within a TWPStyleCollection. All the editing is done inside a EditField obviously. I use version 5.43 of WPTools with Delphi 7.
    The style is applied to the document using the following code:


    wprich.ActiveStyleName := nmStyle;

    if wprich.IsSelected then
    wprich.SelectedTextAttr.ClearAttr(true, true);
    else
    wprich.ActiveParagraph.ClearCharAttr(true);

    wprich.ReformatAll(false, true);


    This works fine. The problem is as follows:

    1 - Apply a style to a paragraph (or all the document);
    2 - go to the end of the line and hit Enter to create a new line;
    3 - paste a text from another editor (or any other source in fact) into the new line;
    4 - the new text is pasted without the style. No matter the font type of the original text, it is pasted with Arial font size 11, while my styles uses Garamond and Verdana with sizes 13 and 12;


    I know that when a new line is created the style is not applied to this line (same behavior as Microsoft Word), so in the OnKeyPress event I set the style for the new line:

    if Ord(Key) = VK_RETURN then
    wprich.ActiveParagraph.SetStyle(wprich.ActiveParagraph.PrevPar.Style, false, true);


    As far I can tell this works fine too, but do not solve the problem. When a paste a text, it still has Arial font size 11.
    Before I start to use styles, the text was pasted using the document's font type (garamond size 13) wich is the behavior I espected from the ClipboardOptions := [wpcoDontPasteFonts]. Why is it pasted with a font other than the one defined in the style now?
    Any ideas on how to solve this?


    P.S.: Can I set a base style to all the document without looping through the paragraphs (ActivePar.ASetBaseStyle(0)) so I don't need the OnKeyPress code?

    • Offizieller Beitrag

    Hi,

    In Your case I recommend to use the BeforePasteText event.

    procedure TForm1.WPRichText1BeforePasteText(Sender: TObject;
    RTFData: TWPRTFDataCollection; par: TParagraph; Stream: TStream;
    Reader: TWPCustomTextReader; OnlyBodyText: Boolean;
    var LoadedText: TWPRTFDataBlock);

    You can loop the paragraphs and set you style. You can also call ClearCharAttr to remove any existing font information.

    You can make a style the deafult, it will be then applied to new paragraphs.

    {:: Used by paragraph styles. One style of the collection can
    be the default style. It will be used by new paragraphs created
    with the ENTER key. }
    property IsDefault: Boolean


    I hope this helps,
    Julian

  • Hi,

    Thanks for your help. I used the BeforePasteText event and did a loop on the paragraphs of LoadedText. It's working fine. The IsDefault tip was great too.

    A little question off-topic if I may: in my version of Wptools is not possible to merge tables lines and, if my memory serves me rigth, I read in another topic that there was no solution for this. There is a correction for this in Wptools version 6?