rvico TRichView Reference | How to...

...remove text formatting

Top  Previous 

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.

procedure RemoveFormatting(RVData: TCustomRVData; RemoveMarkers, KeepLinks: Boolean);
var
  i, HypertextStyleNo: Integer;
  {.............................................................}
  procedure DoRemoveFormatting(RVData: TCustomRVData);
  var
    i, r, c, StyleNo, StyleNoTo: Integer;
    PB: 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
              StyleNoTo := HypertextStyleNo
            else
            begin
              StyleNoTo := 0;
              RVData.SetItemTag(i, '');
            end;
            RVData.GetItem(i).StyleNo := StyleNoTo;
          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;
  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: https://www.trichview.com/forums/viewtopic.php?t=2392


TRichView © trichview.com