In order to facilitate text formatting, text strings
passed to the Canvas.DrawText method may contain certain HTML tags.
You must let DrawText know that the string
is to be treated as HTML by setting an additional parameter,
HTML, to true, e.g.
Canvas.DrawText "For more info, click <A HREF=""http://www.zzz.com""><U>here</U></A>", "x=10; y=20; html=true", Font
6.4.1 Supported Tags and Syntax
The following HTML tags are currently supported:
<A HREF="...">
<B>
<BR>
<CENTER>
<DIV ALIGN="..." STYLE="...">
<FONT SIZE="..." FACE="..." COLOR="...">
<FONT STYLE="...">
<I>
<P>
<SUB>
<SUP>
<U>
All other tags are ignored. Each attribute value inside a tag must be enclosed in double-quotes, otherwise the attribute
will be ignored altogether. All attributes are optional.
Tags and attributes are case-insensitive.
The CR (13), LF (10) and Tab (9) characters are treated as spaces.
Multiple consecutive space characters are treated as a single space.
The SIZE attribute of a <FONT> tag must be an integer between 1 and 7
which corresponds to absolute font size of
7.5, 10, 12, 13.5, 18, 24 and 36, respectively (in default user units).
To specify an arbitrary font size, use the STYLE attribute described below.
By default, font size is set to 10 (or the value of the Size parameter passed to the DrawText method.)
The FACE attribute of a <FONT> tag specifies a font name
to be used to draw text that follows the tag.
By default, the font object passed to the DrawText method as an argument is used.
You must always specify a font family, such as "Courier New", instead of a
specific font name such as "Courier New Bold". To draw text in
bold, italic, or both, you must use the
tags <B> and <I>, or style attributes font-weight and font-style (described below).
For example, the following text string will be displayed using four different fonts,
Arial, Arial Bold, Arial Bold Italic, and Arial Italic:
"<FONT FACE=""Arial"">Happy <B>Birthday <i>To</b> You</i></FONT>"
Output: Happy Birthday To You
Note that to be able to use the <B> and <I> tags, the respective
font styles must be installed on your system,
or an error exception will be thrown. AspPDF will
not attempt to replace a missing font with a similar one.
The COLOR attribute of a <FONT> tag specifies font color.
A color can be specified either by name or a Hex value prefixed by #, e.g.
<FONT COLOR="violet">
<FONT COLOR="#EE82EE">
By default,
the color is set to black (#000000) or whatever value specified by the Color parameter passed to DrawText.
The STYLE attribute of the <FONT> and <DIV> tags allows you to specify
various font attributes in a single tag using the Cascading Style Sheet (CSS) syntax.
The following CSS attributes are currently supported:
color
font-family
font-size (in pt, in, cm or mm)
font-style (normal or italic)
font-weight (normal or bold)
text-decoration (none or underline)
For example:
<FONT STYLE="font-family: Arial; font-size: 12pt; font-weight: bold; color: red">
Text alignment to the left, center, right, and both sides is to be specified via a <DIV> tag
with the ALIGN attribute set to "left", "center", "right", and "justify", respectively.
In addition to the HTML tags mentioned above, the DrawText method recognizes
the following special symbols:
& (&)
¢ (¢)
© (©)
° (°)
€ (€)
≥ (≥)
> (>)
≤ (≤)
< (<)
(non-breakable space)
≠ (≠)
£ (£)
" (")
® (®)
™ (™)
¥ (¥)
&#NNN; (an arbitrary character with Unicode code NNN)
6.4.2 Spanning Multiple Pages
If a text string does not fit in a single page,
the DrawText method must be called on the next page and
passed a remainder of the string, and this has to be repeated as many times
as necessary.
The size of the remainder is determined by the return value of DrawText
(see the code sample in Section 6.1.4.)
For the most part, this method applies to HTML strings as well, except that
by cutting off a portion of the string that has already been drawn on a previous
page we may inadvertently cut off HTML tags that are still in effect. As a result,
text on subsequent pages may not be displayed correctly.
To circumvent this problem, the PdfCanvas object provides a read-only HtmlTag
property which is populated by a call to DrawText if the HTML parameter
is set to true and
the specified text string does not fit in the page.
The property contains an HTML tag representing a set of font
attributes currently in effect. This tag should simply be prepended to the
remainder of the text string for the next call to DrawText.
The following code sample demonstrates this technique:
VBScript |
...
' Load string from file
Text = Pdf.LoadTextFromFile(Server.MapPath("html.txt") )
Set param = pdf.CreateParam("x=10; y=290; width=280; height=280; html=true")
Do While Len(Text) > 0
CharsPrinted = Page.Canvas.DrawText(Text, param, Font )
' Save HTML tag generated by DrawText to reflect current font state
HtmlTag = Page.Canvas.HtmlTag
' We printed the entire string. Exit loop.
if CharsPrinted = Len(Text) Then Exit Do
' Otherwise print remaining text on next page
Set Page = Page.NextPage
Text = HtmlTag & Right( Text, Len(Text) - CharsPrinted)
Loop
...
|
C# |
...
String strText = objPDF.LoadTextFromFile( Server.MapPath("html.txt") );
IPdfParam objParam = objPDF.CreateParam("x=10; y=290; width=280; height=280; html=true");
while( strText.Length > 0 )
{
// DrawText returns the number of characters that fit in the box allocated.
int nCharsPrinted = objPage.Canvas.DrawText( strText, objParam, objFont );
// HTML tag generated by DrawText to reflect current font state
String strHtmlTag = objPage.Canvas.HtmlTag;
// The entire string printed? Exit loop.
if( nCharsPrinted == strText.Length )
break;
// Otherwise print remaining text on next page
objPage = objPage.NextPage;
strText = strHtmlTag + strText.Substring( nCharsPrinted );
}
...
|
Click the links below to run this code sample:
http://localhost/asppdf/manual_06/06_html.asp
http://localhost/asppdf/manual_06/06_html.aspx
6.4.3 Issues and Limitations
- DrawText requires that attribute values in a tag be enclosed in double quotes,
although most other HTML implementations do not require that.
- An <A> tag cannot span multiple pages.
- An <A> tag can only be used on a PdfCanvas object associated with a page.
It cannot be used on a PdfCanvas object associated with a graphics or table cell.
This limitation is due to the fact that HTML anchors (links) in PDF are implemented
via annotations, and an annotation can only be associated with a page.
- When the HTML parameter is set to True, the ReverseHebrewArabic
and Angle parameters are ignored.
Starting with Version 1.6, AspPDF provides a much more extensive HTML support
via the ImportFromUrl method. For more information, see
Chapter 13 - HTML to PDF Conversion.