Doc AddText Function. Adds a block of text to the current page. ABCpdf .NET PDF Library.

ABCpdf .net

 
   

Adds a block of text to the current page.

 

   
Syntax  

[C#]
int AddText(string text)

[Visual Basic]
Function AddText(text As String) As Integer

 

   

Params
 
Name Description
text The text to be added to the page.
return The Object ID of the newly added Text Object.

 

   

Notes
 

Adds a block of single styled text to the current page.

For adding multi-styled text or for chaining text from one page to another you should use the AddHtml method which is used for adding HTML styled text.

The text is in the current style, size and color and starts at the location specified in the current position. If the text is long it will will wrap and extend downwards until it fills the current rectangle. Text positioning in the rectangle is determined by the horizontal and vertical positioning.

You can chain together multiple text blocks so that text flows from one to the next. To do this you need to first add a block of text using AddText. Then add multiple new text blocks using AddHtml each time passing in the ID obtained from the previous call after adjusting the target location (such as the rectangle or the page).

The AddText function returns the Object ID of the newly added Text Object. If no text could be added then zero is returned. This will happen if a zero length string was supplied or if the rectangle was too small for even one character to be displayed.

Typically you will get a return value of zero if your text was too large to fit in your Rect or if the Pos was at the end of the Rect. So if you are expecting text to be displayed and are seeing a return value of zero, check your text size, check your Rect is where you think it is by framing it using FrameRect and ensure your Pos is set at the top left of the Rect.

Text is drawn word-wrapped within the current rectangle with the first character at the location specified by the Pos property. Normally the Pos property reflects the top left position of the current rectangle. However if you need to alter the position at which text drawing starts you can modify the Pos property after changing the Rect. When the text has been drawn the Pos will be updated to reflect the next text insertion point.

Character positioning is specified from the top left of the character. Please see Pos for details on positioning when using vertical fonts. The FontSize determines the total line height and the character baseline is 80% of the way down from the top of the line.

 

   

Example
 

The following code adds a number of chunks of text to a document. Each chunk is in a different style. This sample makes use of the fact that the Pos is updated to point to the next text insertion point after adding a piece of text. However note that when inserting muti-styled text it is generally more efficient to use the AddHtml method.

[C#]
Doc theDoc = new Doc();
theDoc.Page = theDoc.AddPage();
theDoc.FontSize = 48;
int theF1 = theDoc.AddFont("Times-Roman");
int theF2 = theDoc.AddFont("Times-Bold");
theDoc.Font = theF1;
theDoc.AddText("Gallia est omnis ");
theDoc.Font = theF2;
theDoc.AddText("tertiam Galli appellantur ");
theDoc.Font = theF1;
theDoc.AddText("divisa in partes tres, ");
theDoc.Font = theF2;
theDoc.AddText("quarum unam incolunt ");
theDoc.Font = theF1;
theDoc.AddText("Belgae, aliam Aquitani. ");
theDoc.Font = theF2;
theDoc.AddText("tertiam Galli appellantur");
theDoc.Save(Server.MapPath("docaddtext.pdf"));
theDoc.Clear();

[Visual Basic]
Dim theDoc As Doc = New Doc()
theDoc.Page = theDoc.AddPage()
theDoc.FontSize = 48
Dim theF1 As Integer = theDoc.AddFont("Times-Roman")
Dim theF2 As Integer = theDoc.AddFont("Times-Bold")
theDoc.Font = theF1
theDoc.AddText("Gallia est omnis ")
theDoc.Font = theF2
theDoc.AddText("tertiam Galli appellantur ")
theDoc.Font = theF1
theDoc.AddText("divisa in partes tres, ")
theDoc.Font = theF2
theDoc.AddText("quarum unam incolunt ")
theDoc.Font = theF1
theDoc.AddText("Belgae, aliam Aquitani. ")
theDoc.Font = theF2
theDoc.AddText("tertiam Galli appellantur")
theDoc.Save(Server.MapPath("docaddtext.pdf"))
theDoc.Clear()


docaddtext.pdf