How To Render HTML-Formatted Text In PDF
Learn how to render HTML-formatted text in PDF using eDocEngine VCL.
By R. Vyshakh
eDocEngine supports simple HTML tags in text strings it renders on PDF. Using this feature, you can specify formatted text using styles such as:
- bold
- italic
- bold and italic
strike through
In the code snippet shown below, you will see how to render strings containing HTML tags on PDF.
begin
// Use a PDF engine object
with gtPDFEngine1 do
begin
// Specify output PDF file name
FileName := 'FormattedText';
// Specify measurement units
MeasurementUnit := muInches;
// Specify header/footer dimensions
Page.HeaderHeight := 1;
Page.FooterHeight := 1;
BeginDoc;
BeginHeader;
// Use <i> HTML tag
HTMLTextOut(0.5, 0.5, '<b>This header is in bold style</b>');
EndHeader;
// Use <b> and <i> HTML tags
HTMLTextOut(
1, 1,
'<b>This is bold</b> and <i>this is italic</i>' +
'and <i><b>this is bold and italic</b></i>!');
BeginFooter;
// Use <u> HTML tags
HTMLTextOut(
0.5, 0.5,
'<u>This footer is underlined</u>.');
EndFooter;
BeginWatermark;
// Use <s> HTML tags
HTMLTextOut(
1, 4,
'<s>This watermark is in strike-through style.</s>');
EndWatermark;
EnDDoc;
end;
end;
Here is how the PDF document created by eDocEngine looks.
This site is best viewed on a screen with minimum resolution of 1152 x 864 pixels.
Windows users are advised to use Microsoft ClearType Tuning for optimal experience.
Linux and other users can enable font smoothing, as supported by their OS.
Also, please use the latest version of a standards-compliant browser such as Opera, FireFox, Chrome or Safari.