unit rtfPicResize;
interface
uses
JvRichEdit,StrUtils,Classes,SysUtils;
type
TrtfPic = record
Width_Start: Integer;
Width_End: Integer;
Height_Start: Integer;
Height_End: Integer;
end;
type TrtfPics = array of TrtfPic;
function getRtfStringFromRichEdit(re: TjvRichEdit): String;
procedure WriteRtfStringtoRichEdit(rtfStr: String;var re: TjvRichEdit);
function CountPics(re: TjvRichEdit): TrtfPics;
function getPicHeight(PicIndex: Integer; re: TjvRichEdit): Integer;
procedure setPicHeight(PicIndex: Integer; AHeight: Integer;var re: TjvRichEdit);
function getPicWidth(PicIndex: Integer; re: TjvRichEdit): Integer;
procedure setPicWidth(PicIndex: Integer; AWidth: Integer;var re: TjvRichEdit);
function getLastPicIndex(re: TjvRichEdit): Integer;
implementation
function getRtfStringFromRichEdit(re: TjvRichEdit): String;
var
ss: TStringStream;
begin
try
ss := TStringStream.Create;
re.Lines.SaveToStream(ss);
result := ss.DataString;
finally
ss.free;
end;
end;
procedure WriteRtfStringtoRichEdit(rtfStr: String;var re: TjvRichEdit);
var
ss: TStringStream;
begin
try
ss := TStringStream.Create(RtfStr);
re.Lines.LoadFromStream(ss);
finally
ss.free;
end;
end;
function CountPics(re: TjvRichEdit): TrtfPics;
var
p1,p2: Integer;
rtfStr: String;
begin
p1 := 1;
p2 := 1;
setlength(Result,0);
rtfStr := getRtfStringFromRichEdit(re);
while p1 > 0 do begin
p1 := posEx('picwgoal',rtfStr,p2);
if p1 > 0 then begin
p2 := posEx('\',rtfStr,p1);
setlength(Result,length(Result)+1);
Result[length(Result)-1].Width_Start := p1 +length('picwgoal');
Result[length(Result)-1].Width_End := p2;
p1 := posEx('pichgoal',rtfStr,p2);
p2 := posEx(' ',rtfStr,p1);
Result[length(Result)-1].Height_Start := p1 +length('pichgoal');
Result[length(Result)-1].Height_End := p2;
end;
end;
end;
function getPicHeight(PicIndex: Integer; re: TjvRichEdit): Integer;
var
rtfStr: String;
Pics: TRtfPics;
start,Ende: Integer;
begin
rtfStr := getRtfStringFromRichEdit(re);
Pics := CountPics(re);
result := 0;
if PicIndex <= length(Pics)-1 then begin
Start := Pics[PicIndex].Height_Start;
Ende := Pics[PicIndex].Height_End;
result := strToInt(midStr(rtfStr,Start,Ende-Start));
end;
end;
procedure setPicHeight(PicIndex: Integer; AHeight: Integer;var re: TjvRichEdit);
var
rtfStr,NewRtfStr: String;
Pics: TRtfPics;
start,Ende: Integer;
ss: TStringStream;
begin
rtfStr := getRtfStringFromRichEdit(re);
Pics := CountPics(re);
if PicIndex <= length(Pics)-1 then begin
Start := Pics[PicIndex].Height_Start;
Ende := Pics[PicIndex].Height_End;
NewRtfStr := LeftStr(rtfStr,Start-1)+IntToStr(AHeight)+RightStr(rtfStr,length(rtfStr)-ende+1);
try
ss := TStringStream.Create(NewRtfStr);
re.Lines.LoadFromStream(ss);
finally
ss.Free;
end;
end;
end;
function getPicWidth(PicIndex: Integer; re: TjvRichEdit): Integer;
var
rtfStr: String;
Pics: TRtfPics;
start,Ende: Integer;
begin
rtfStr := getRtfStringFromRichEdit(re);
Pics := CountPics(re);
result := 0;
if PicIndex <= length(Pics)-1 then begin
Start := Pics[PicIndex].Width_Start;
Ende := Pics[PicIndex].Width_End;
result := strToInt(midStr(rtfStr,Start,Ende-Start));
end;
end;
procedure setPicWidth(PicIndex: Integer; AWidth: Integer;var re: TjvRichEdit);
var
rtfStr,NewRtfStr: String;
Pics: TRtfPics;
start,Ende: Integer;
ss: TStringStream;
begin
rtfStr := getRtfStringFromRichEdit(re);
Pics := CountPics(re);
if PicIndex <= length(Pics)-1 then begin
Start := Pics[PicIndex].Width_Start;
Ende := Pics[PicIndex].Width_End;
NewRtfStr := LeftStr(rtfStr,Start-1)+IntToStr(AWidth)+RightStr(rtfStr,length(rtfStr)-ende+1);
try
ss := TStringStream.Create(NewRtfStr);
re.Lines.LoadFromStream(ss);
finally
ss.Free;
end;
end;
end;
function getLastPicIndex(re: TjvRichEdit): Integer;
var
Pics: TRtfPics;
begin
Pics := CountPics(re);
result := length(Pics)-1;
end;
end.