How to make PBY THtmlViewer loaded and show HTML file in unicode format?
I have some unicode.html files that I want to display inside a THtmlViewer component, in Delphi.
I can't convince the code to work just by doing ".LoadFromFile" - do I first need to load the unicode file into a stream and then convert it somehow?
Delphi 2007, THtmlViewer v9.45
I haven't done anything with unicode files or THtmlViewer yet.
source to share
Okay, well here is the courage of what I came up with. Constructive criticism and observations were appreciated!
// load either an ansi or unicode-type html doc into the browser component. // the filename has already been confirmed as an existing file procedure TfrmBrowser.LoadDocument (FFileName: string); var FWideText: Widestring; FAnsiText: AnsiString; FRequiredLen: Integer; FFileStream: TFileStream; FMemStream: TMemoryStream; FBuffer: Byte; begin FFileStream: = TFileStream.Create (FFileName, fmOpenRead or fmShareDenyNone); // anything less than half a dozen bytes would be pointless, but ... if FFileStream.Size> 1 then begin // checking the first byte of the file to give us a clue about file-type FFileStream.Read (FBuffer, 1); FFileStream.Position: = 0; // rewind position if (FBuffer = $ FF) or (FBuffer = $ EF) then begin // probably Unicode FRequiredLen: = FFileStream.Size div 2; // 2 bytes per char SetLength (FWideText, FRequiredLen); FFileStream.Read (FWideText [1], FFileStream.Size); // cast it into an Ansistring FAnsiText: = FWideText; FMemStream: = TMemoryStream.Create; FMemStream.Write (FAnsiText [1], FRequiredLen); FMemStream.Position: = 0; // rewind the position // load the stream into the THtmlViewer vwBrowse.LoadFromStream (FMemStream); FMemStream.Free; end else begin // probably Ansi, just load original filestream in vwBrowse.LoadFromStream (FFileStream); end; FFileStream.Free; end;
Obviously there is no error capture, but this is the basic idea.
source to share
FYI, THTMLViewer is actively maintained in google code (last done a couple of minutes ago): http://code.google.com/p/thtmlviewer/
D6-DXE2 and Lazarus compatibility, tons of fixes and improvements from the "original" version (9.45).
source to share
You are using Delphi 2007. This is before the unicode era of Delphi programming!
Although it is very tedious to work with Unicode in earlier versions of Delphi, it is quite possible to achieve satisfactory results in some controls, especially the THtmlView component.
I am posting sample code from one of my programs:
//code to toggle source or WYSIWYG views
var
htmEd: IHTMLDocument2;
begin
htmEd := HtmlEdit.Document as IHtmlDocument2;
if ToggleTabSet.TabIndex = 0 then
begin
HtmlEditContainer.PageIndex := 0; // Tab sheet index
htmEd.body.innerHTML := Memo1.Lines.Text; // TTntMemo
pnlEditorState.Caption := 'Design View';
end
else
if ToggleTabSet.TabIndex = 1 then
begin
HtmlEditContainer.PageIndex := 1;
Memo1.Lines.Text := HtmEd.body.innerHTML;
pnlEditorState.Caption := 'Source View';
end;
By reading the above code, you can see that I am using the TTntMemo component , into which the html file is loaded first. Then I load the "Text" of the note into the HtmlView property "body.innerHTML".
htmEd.body.innerHTML := Memo1.Lines.Text;
Note :
- TntWare 'Memo1.Lines.Text;' it is a WideString type.
- 'IHTMLDocument2' comes from TEmbeddedWB . See Reasons Why TEmbeddedWB Is Good?
This is what worked for me in the early days. I switched to Delphi 2009 and it is much easier now (just set the appropriate TEncoding when uploading files)!
source to share