This example removes all formatting from the document by converting all text and paragraph styles to 0 and (optionally) removing bullets and numbering. If KeepLinks=True, hypertext will not be removed (all text links will be converted to the hypertext style with the lowest index).
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.
Code: Select all
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;
Code: Select all
procedure TForm3.ToolButton66Click(Sender: TObject);
begin
RemoveFormatting(RichViewEdit1.RVData, True, True);
NormalizeRichView(RichViewEdit1.RVData); // from RVNormalize.pas, from RichViewActions
RichViewEdit1.DeleteUnusedStyles(True, True, True);
RichViewEdit1.ClearUndo;
RichViewEdit1.Format;
end;
2008-Dec-11: for compatibility with TRichView 11 (using GetItemTextR and SetItemTextR instead of GetItemText and SetItemText)
2011-Oct-2: for compatibility with TRichView 13.4 (item tags became strings)
2018-Apr-9: new version of this code compatible with TRichView 17.3 is here: http://www.trichview.com/forums/viewtop ... 649#p34649