If the method succeeds, the return value is the length of the document
text in characters, not including the terminating null. If the method
fails, the return value is zero. To get extended error information, call
GetLastError.
The GetDocumentText method retrieves the HTML
markup for the current document loaded in the browser. This includes
the complete contents of the <html> element, such
as the <head> and <body>
sections, rather than just the visible text.
The text returned will reflect the current state of the document at
the time the method is called. If the page has been dynamically
modified by scripts after it was initially loaded, those changes will
be included. The value may be large, particularly for complex pages,
and applications should allocate a buffer of sufficient size by first
calling the method with a NULL pointer to determine the required
length.
If your project is configured to use a multi-byte character set
(ANSI) instead of Unicode, this method will always return the content
as UTF-8 encoded text, regardless of the system's current locale or
code page. Legacy ANSI code pages cannot reliably represent the full
range of characters, and using them in place of Unicode or UTF-8 can
lead to data loss or corrupted text.
This method should only be called after navigation has completed.
If called while a page is still loading, the results may be incomplete
or empty. When the content is provided by a local file or string, the
returned HTML will be the original content that was passed to the
control. The returned text is raw HTML and may contain scripts, style
definitions, and markup that are not directly suitable for display in
a plain-text context.
If a blank page is currently being displayed, such as when
navigating to about:blank or by setting the current document
text to an empty string, this method can still return a value.
Internally, the browser creates a blank page using the markup
"<html><head></head><body></body></html>"
and that is what this method will return.
The GetFilteredText method can be used to obtain a
filtered version of the text with the HTML markup removed.
INT nLength = pWebView->GetDocumentLength();
if (nLength > 0)
{
LPTSTR pszBuffer = (LPTSTR)malloc((nLength + 1) * sizeof(TCHAR));
if (!pszBuffer)
{
return;
}
nLength = pWebView->GetDocumentText(pszBuffer, nLength + 1);
if (nLength > 0)
{
_putts(pszBuffer);
_puttc(_T('\n'), stdout);
}
free(pszBuffer);
}