Multistyle Example

ABCpdf .net

 
   

This example shows how to create multistyled text.

 

   
Setup  

We want to display all our proper names in bold so we enclose them in bold tags.

[C#]
string theText = "<b>Gallia</b> est omnis divisa in partes tres, quarum unam incolunt <b>Belgae</b>, aliam <b>Aquitani</b>, tertiam qui ipsorum lingua <b>Celtae</b>, nostra <b>Galli</b> appellantur.";

[Visual Basic]
Dim theText As String = "<b>Gallia</b> est omnis divisa in partes tres, quarum unam incolunt <b>Belgae</b>, aliam <b>Aquitani</b>, tertiam qui ipsorum lingua <b>Celtae</b>, nostra <b>Galli</b> appellantur."


   

Doc Obj
 

Next we create an ABCpdf Doc object and give it some basic settings. Although we could pass our HTML styled text directly to the AddHtml function, we can take more control over the way that fonts are added to the PDF if we specify font IDs.

[C#]
Doc theDoc = new Doc();
theDoc.FontSize = 72;
theDoc.Rect.Inset(10, 10);
theDoc.FrameRect();
int theFont1 = theDoc.EmbedFont("Verdana", LanguageType.Latin, false, true);
int theFont2 = theDoc.EmbedFont("Verdana Bold", LanguageType.Latin, false, true);

[Visual Basic]
Dim theDoc As Doc = New Doc()
theDoc.FontSize = 72
theDoc.Rect.Inset(10, 10)
theDoc.FrameRect()
Dim theFont1 As Integer = theDoc.EmbedFont("Verdana", LanguageType.Latin, False, True)
Dim theFont2 As Integer = theDoc.EmbedFont("Verdana Bold", LanguageType.Latin, False, True)

 

   

Adding
 

We replace the bold tags with font tags that directly reference our chosen fonts and then add the HTML styled text to the current rectangle.

[C#]
theText = "<font pid=" + theFont1.ToString() + ">" + theText + "</font>";
theText = theText.Replace("<b>", "<font pid=" + theFont2.ToString() + ">");
theText = theText.Replace("</b>", "</font>");
theDoc.AddHtml(theText);

[Visual Basic]
theText = "<font pid=" + theFont1.ToString() + ">" + theText + "</font>"
theText = theText.Replace("<b>", "<font pid=" + theFont2.ToString() + ">")
theText = theText.Replace("</b>", "</font>")
theDoc.AddHtml(theText)

 

   

Save
 

Finally we save and clear the document.

[C#]
theDoc.Save(Server.MapPath("styles.pdf"));
theDoc.Clear();

[Visual Basic]
theDoc.Save(Server.MapPath("styles.pdf"))
theDoc.Clear()

 

   

Results
 


styles.pdf