What is the resulting fontsize in PDF for rich text used in a SimpleXhtmlShape
TallPDF supports the SimpleXhtmlShape. This shape accepts PDF rich text as its Text attribute. This PDF rich text is xhtml with a small set of supported elements (body, p, i, b, span). See the PDF reference at page 680 under ‘Rich Text Strings’.
In this rich text, css settings can be used to set the fontsize. The following named fontsizes are supported:
This sample shows the translation of these named fontsizes to actual fontsizes when used in PDF. ‘larger’ and ‘smaller’ are relative fontsizes. These are calculated relative to the fontsize setting on the SimpleXhtmlShape.
The sample will create a PDF document with 3 pages. Each page will contain a SimpleXhtmlShape. These shapes will be created with a different fontsize. The text of the shapes will be an xhtml string. It presents a list of font settings and shows the actual fontsize for the various named fontsizes.
The sample proves/shows the mapping of the named fontsizes to the actual fontsizes. It shows:
Absolute fontsizes are mapped independent of the fontsize of the shape
Relative fontsizes are mapped relative to the fontsize of the shape
C# code sample with overview of fontsize mapping for named fontsizes used in a SimpleXhtmlShape
Xhtml that will be used
This is the content of the sample file that we use with a fontsize of 8pt. There are also files for fontsize 12pt and 18pt.
In the first column the named fontsize is used to set the fontsize. In the second column the calculated fontsize is used. This allows us to verify that the mapping, as presented in the table, is correct. Both columns in a row should show the same fontsize.
Step 1: Create a SimpleXhtmlShape
The SimpleXhtmlShape uses the Xhtml string that was generated to set the ‘Text’ attribute. The ‘DefaultFontSize’ attribute of the shape is set.
Step 2: Create the PDF document with 3 shapes with different fontsizes
String fileName = @"..\..\result.pdf";
Document document = new Document();
Section section = document.Sections.Add();
// Writing the PDF document to disk
using (var file = new FileStream(fileName, FileMode.Create, FileAccess.Write))
// open the PDF document
Creating the fontsize mappings with 3 different fontsizes for the XhtmlShape will show that the relative fontsizes (‘smaller’, ‘larger’) are calculated relative to that fontsize and that absolute fontsizes are calculated independent of the fontsize setting on the shape.