[Example] How to delete all images in a document

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
Sergey Tkachenko
Site Admin
Posts: 14163
Joined: Sat Aug 27, 2005 10:28 am
Contact:

[Example] How to delete all images in a document

Post by Sergey Tkachenko » Thu Jul 26, 2018 7:28 pm

This example shows how to delete all images.
Background images are cleared. Items-images are deleted. Images-markers are converted to text.
This procedure is not undoable.

Code: Select all

// Deleting all images in the document (RVData), including nested documents
// (table cells, notes and text boxes)
procedure DeleteImagesFromRVData(RVData: TCustomRVData);
var
  i, r, c: Integer;
  Item, NextItem: TCustomRVItemInfo;
  Table: TRVTableItemInfo;
begin
  for i := RVData.ItemCount - 1 downto 0 do
  begin
    Item := RVData.GetItem(i);
    // if this is an image item, deleting it
    if (Item is TRVGraphicItemInfo) or (Item is TRVBulletItemInfo) then
    begin
       // if the image starts a line, make the next item start a line
       if RVData.IsFromNewLine(i) and (i + 1 <> RVData.ItemCount) then
       begin
         NextItem := RVData.GetItem(i + 1);
         if not RVData.IsFromNewLine(i + 1) then
         begin
           NextItem.SameAsPrev := False;
           NextItem.PageBreakBefore := Item.PageBreakBefore;
           NextItem.BR := Item.BR;
         end;
       end;
       // deleting
       RVData.DeleteItems(i, 1);
       // if it was the only item after a list marker, adding an empty text
       // item instead (possible improvement: using some smarter code to
       // calculate StyleNo for the added line; now we just use TextStyles[0])
       if (i > 0) and (RVData.GetItemStyle(i - 1) = rvsListMarker) and
         RVData.IsFromNewLine(i) then
         RVData.AddNL('', 0);
    end
    // if this is a note or a text box, deleting images from its document
    else if Item is TCustomRVNoteItemInfo then
      DeleteImagesFromRVData(TCustomRVNoteItemInfo(Item).Document)
    // if this is a table...
    else if Item is TRVTableItemInfo then
    begin
      Table := TRVTableItemInfo(Item);
      // deleting its background image
      Table.BackgroundImage := nil;
      Table.BackgroundStyle := rvbsColor;
      // processing cells
      for r := 0 to Table.RowCount - 1 do
        for c := 0 to Table.ColCount - 1 do
          if Table.Cells[r, c] <> nil then
          begin
            // deleting cell background image
            Table.Cells[r, c].BackgroundImage := nil;
            Table.Cells[r, c].BackgroundStyle := rvbsColor;
            // deleting images in cell content
            DeleteImagesFromRVData(Table.Cells[r, c].GetRVData);
          end;
    end;
  end;
end;

// Deleting all images from paragraph lists
procedure DeleteImagesFromListStyles(ListStyles: TRVListInfos);
var
  i, j: Integer;
  Level: TRVListLevel;
begin
  for i := 0 to ListStyles.Count - 1 do
    for j := 0 to ListStyles[i].Levels.Count - 1 do
    begin
      Level := ListStyles[i].Levels[j];
      case Level.ListType of
        rvlstPicture,
        rvlstImageList:
          begin
            // converting bullets from images to text
            Level.ListType := rvlstBullet;
            Level.Font.Name := 'Symbol';
            Level.Font.Charset := SYMBOL_CHARSET;
            Level.Font.Size := 12;
            Level.FormatString := UNI_MIDDLEDOT;
          end;
        rvlstImageListCounter:
          begin
            // converting numbering from images to text
            Level.ListType := rvlstDecimal;
            Level.Font.Name := 'Arial';
            Level.Font.Charset := DEFAULT_CHARSET;
            Level.Font.Size := 10;
            Level.FormatString := '%' + IntToStr(j) + ':s.';
          end;
      end;
    end;
end;

// Deleting all images in rv
procedure DeleteAllImages(rv: TCustomRichView);
begin
  rv.BackgroundBitmap := nil;
  rv.BackgroundStyle := bsNoBitmap;
  DeleteImagesFromListStyles(rv.Style.ListStyles);
  DeleteImagesFromRVData(rv.RVData);
  rv.RefreshListMarkers;
  if rv is TCustomRichViewEdit then
    TCustomRichViewEdit(rv).ClearUndo;
end;

How to call:

Code: Select all

procedure DeleteAllImages(RichViewEdit1);
RichViewEdit1.Format;

Sergey Tkachenko
Site Admin
Posts: 14163
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: [Example] How to delete all images in a document

Post by Sergey Tkachenko » Thu Jul 26, 2018 7:29 pm

Possible improvement: replacing images to their Alt text.
I can do it on request.

See also:
- How to remove formatting
- Is there any image?

Post Reply