- Offizieller Beitrag
If you need to insert an image in the document which data is loaded from the web you have to use a component to access the internet. In our example we use Indy.
Please drop an INDY TIdHTTP component on the form:
object IdHTTP1: TIdHTTP MaxLineAction = maException ReadTimeout = 0 AllowCookies = True ProxyParams.BasicAuthentication = False ProxyParams.ProxyPort = 0 Request.ContentLength = -1 Request.ContentRangeEnd = 0 Request.ContentRangeStart = 0 Request.ContentType = 'text/html' Request.Accept = 'text/html, */*' Request.BasicAuthentication = False Request.UserAgent = 'Mozilla/3.0 (compatible; Indy Library)' HTTPOptions = [hoForceEncodeParams] Left = 554 Top = 35end
This code can be used to create the image:
procedure TForm1.InsertWEBPictureClick(Sender: TObject);var loadurl : String; obj : TWPTextObj; stream : TMemoryStream;begin loadurl := 'http://www.wpcubed.com/images/wpsynscreen_400.gif'; stream := TMemoryStream.Create; try IdHTTP1.Get(loadurl,stream); if stream.Size>0 then try obj := WPRichText1.TextObjects.InsertNewObject(wpobjImage,false, false); obj.LoadObjFromStream(loadurl,stream); obj.ObjRef.StreamName := loadurl; except obj.Free; end; finally stream.Free; end; end;
You will need to add 2 events to update the object data after the file was saved and reloaded. This are the events OnRequestHTTPImage and (this is important to load CSS styles) OnRequestHTTPString.
The following events modify the caption of a panel to show which file is currently downloaded.
procedure TForm1.WPRichText1RequestHTTPString(
RTFData: TWPRTFDataCollection; Reader: TWPCustomTextReader;
const LoadPath, URL: String; var LoadedString: String; var Ok: Boolean);
var loadurl : string;
begin
if pos('http:',lowercase(URL))>0 then
loadurl := URL
else loadurl := LoadPath + URL;
Panel2.Caption := URL;
Application.ProcessMessages;
LoadedString := IdHTTP1.Get(loadurl);
Ok := TRUE;
Panel2.Caption := '';
Application.ProcessMessages;
end;
procedure TForm1.WPRichText1RequestHTTPImage(RTFData: TWPRTFDataCollection;
Reader: TWPCustomTextReader; const LoadPath, URL: String;
TextObject: TWPTextObj; var Ok: Boolean);
var stream : TMemoryStream;
loadurl : string;
begin
if pos('http:',lowercase(URL))>0 then
loadurl := URL
else loadurl := LoadPath + URL;
stream := TMemoryStream.Create;
try
Panel2.Caption := URL;
try
Application.ProcessMessages;
IdHTTP1.Get(loadurl,stream);
except
on e : Exception do Panel2.Caption := URL + '-->' + e.Message;
end;
if stream.Size>0 then
try
TextObject.LoadObjFromStream(URL,stream);
except
on e : Exception do Panel2.Caption := URL + '-->' + e.Message;
end;
ok := TRUE;
if ok then Panel2.Caption := '';
Application.ProcessMessages;
finally
stream.Free;
end;
end;
Alles anzeigen
If you load a file which was placed on a website and which is using relative links it is necessary to make the path to the website known by the reader:
WPRichText1.Memo.FileLoadPath := url_of_html_text;
WPRichText1.AsString := html_text;
note: you can use
html_text:= IdHTTP1.Get(url_of_html_text);
to retrieve the text from a web server.