rvico TRichView Reference | How to...

...remove text formatting

Top  Previous  Next

The code below removes text formatting from the whole document.

It converts indices of all text styles and paragraph styles to 0s. Optionally, it can keep hyperlinks (they are converted to the hypertext style with the lowest index).

Optionally, it can remove list markers as well.

This code changes text items, tabulators and list markers. All other item types remains unchanged.

Note 1: if the document contains both Unicode and non-Unicode text, or non-Unicode text of different Charsets, some text information may be lost.

Note 2: this is not an editing operation. When called for TRichViewEdit, undo buffer must be cleared.

// this code uses some undocumented methods

procedure RemoveFormatting(RVData: TCustomRVData; RemoveMarkers, KeepLinks: Boolean); 

var i, HypertextStyleNo: Integer; 

    UnicodeTo, UnicodeHTo: Boolean; 

  {.............................................................} 

  procedure DoRemoveFormatting(RVData: TCustomRVData); 

  var i, r, c, StyleNo, StyleNoTo: Integer; 

      PB, UnicodeFrom, ThisUnicodeTo: Boolean; 

      table: TRVTableItemInfo; 

  begin 

    for i := RVData.ItemCount-1 downto 0 do begin 

      RVData.GetItem(i).ParaNo := 0

      StyleNo := RVData.GetItemStyle(i); 

      case StyleNo of 

        rvsTable

          begin 

            table := TRVTableItemInfo(RVData.GetItem(i)); 

            for r := 0 to table.RowCount-1 do 

              for c := 0 to table.ColCount-1 do 

                if table.Cells[r,c]<>nil then 

                  DoRemoveFormatting(table.Cells[r,c].GetRVData); 

          end

        rvsListMarker

          if RemoveMarkers then begin 

            PB := RVData.PageBreaksBeforeItems[i]; 

            RVData.DeleteItems(i, 1); 

            if i<RVData.ItemCount then begin 

              RVData.GetItem(i).SameAsPrev := False; 

              RVData.GetItem(i).PageBreakBefore := PB; 

            end

          end

        rvsTab

          if RVData.GetRVStyle.TextStyles

            TRVTabItemInfo(RVData.GetItem(i)).TextStyleNo].Jump and KeepLinks then 

            TRVTabItemInfo(RVData.GetItem(i)).TextStyleNo := HypertextStyleNo 

          else begin 

            TRVTabItemInfo(RVData.GetItem(i)).TextStyleNo := 0

            RVData.SetItemTag(i, ''); 

          end

        1..MaxInt: 

          begin 

            if KeepLinks and RVData.GetRVStyle.TextStyles[StyleNo].Jump then begin 

              ThisUnicodeTo := UnicodeHTo; 

              StyleNoTo := HypertextStyleNo; 

              end 

            else begin 

              ThisUnicodeTo := UnicodeTo; 

              StyleNoTo := 0

              RVData.SetItemTag(i, '');              

            end

            UnicodeFrom := RVData.GetRVStyle.TextStyles[StyleNo].Unicode

            RVData.GetItem(i).StyleNo := StyleNoTo; 

            if UnicodeFrom and not ThisUnicodeTo then begin 

              RVData.GetItem(i).ItemOptions := RVData.GetItem(i).ItemOptions-[rvioUnicode]; 

              RVData.SetItemTextR(i, 

                RVU_UnicodeToAnsi(RVData.GetStyleCodePage(0), RVData.GetItemTextR(i))); 

              end 

            else if not UnicodeFrom and ThisUnicodeTo then begin 

              RVData.GetItem(i).ItemOptions := RVData.GetItem(i).ItemOptions+[rvioUnicode]; 

              RVData.SetItemTextR(i, 

                RVU_AnsiToUnicode(RVData.GetStyleCodePage(0), RVData.GetItemTextR(i))); 

            end

          end

      end

    end

  end

  {.............................................................} 

begin 

  HypertextStyleNo := 0

  if KeepLinks then 

    for i := 0 to RVData.GetRVStyle.TextStyles.Count-1 do 

      if RVData.GetRVStyle.TextStyles[i].Jump then begin 

        HypertextStyleNo := i; 

        break; 

      end

  UnicodeTo  := RVData.GetRVStyle.TextStyles[0].Unicode

  UnicodeHTo := RVData.GetRVStyle.TextStyles[HypertextStyleNo].Unicode

  DoRemoveFormatting(RVData); 

end;

How to use:

  RemoveFormatting(RichViewEdit1.RVData, True, True); 

  NormalizeRichView(RichViewEdit1.RVData); // from RVNormalize.pas, from RichViewActions 

  RichViewEdit1.DeleteUnusedStyles(True, True, True); 

  RichViewEdit1.ClearUndo

  RichViewEdit1.Format

More information: http://www.trichview.com/forums/viewtopic.php?t=2392


TRichView © trichview.com