We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Create multiline text in PDF document

Dear all,

I have struggled a bit with the official Xamarin Forms PDF Documentation. I have managed to create a table, bind it to values and append it to a PdfDocument.

However odd enough, I cannot seem to draw text strings on separate lines. Each text that I try to draw using graphics.DrawString or TextElement gets drawn on top of each other in the first line of the document.

Is there a working sample on how to append text line after line and in between them to have tables?

Thank you!

5 Replies

GR Gayathri Ramalingam Syncfusion Team January 16, 2017 12:55 PM UTC

Hi Bogdan, 
 
Thank you for using Syncfusion products. 
 
Draw text and tables one after another sequentially is possible using Essential PDF. We can achieve this  by using PdfLayoutResult(check class name) by getting the bounds of the table. Please refer the below KB link to get the Layout result and draw the text after the table.
​​​​​​​
 
For PdfLightTable 
PdfLayoutResult result = pdfLightTable.Draw(page, new PointF(10, 10)); 
 
For PdfGrid 
PdfLayoutResult result = pdfGrid.Draw(page, new PointF(10, 10)); 
 
 
Please let us know if you have any concerns. 
 
 
With Regards, 
Gayathri R 



BU Bogdan Ungureanu January 16, 2017 05:06 PM UTC

Hello Gayathri,

Thank you for the reply!

What about drawing different strings one after the other or below one another? I saw that DrawString() chained results in those string being rendered in the same place, overposing.

Example:

graphics.DrawString(someString);
graphics.DrawString(anotherString);

This example results in the 2 different string being rendered on the first line of the document, overposing each other.

Is there a way to accomplish this task as well?

Thank you!


PV Prakash Viswanathan Syncfusion Team January 17, 2017 04:19 PM UTC

Hi Bogdan, 

Drawing text one after another is possible using Essential PDF. We can draw text by one after another by specifying its position in DrawString method. I have attached a simple sample for your reference. 

Please refer below code snippet, 
string text = "Hello world!!! Hello world!!!"; 
 
//Creates a new PDF document. 
PdfDocument doc = new PdfDocument(); 
 
//Adds a page. 
PdfPage page = doc.Pages.Add(); 
 
//Acquires page's graphics 
PdfGraphics graphics = page.Graphics; 
 
PdfStringFormat drawFormat = new PdfStringFormat(); 
drawFormat.WordWrap = PdfWordWrapType.Word; 
drawFormat.Alignment = PdfTextAlignment.Justify; 
drawFormat.LineAlignment = PdfVerticalAlignment.Top; 
 
//Set the font. 
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 10f); 
 
//Create a solid brush. 
PdfBrush brush = new PdfSolidBrush(new PdfColor(Color.Red)); 
RectangleF bounds = new RectangleF(new PointF(10, 10), new SizeF(page.Graphics.ClientSize.Width - 30, page.Graphics.ClientSize.Height - 20)); 
 
// Draw the string one after another. 
graphics.DrawString(text, font, brush, bounds, drawFormat); 
graphics.DrawString(text, font, brush, bounds.X ,bounds.Y + 10, drawFormat); 
 
// Creates a PdfLightTable. 
PdfLightTable pdfLightTable = new PdfLightTable(); 
 
// Initializes DataTable to assign as DateSource to the light table. 
DataTable table = new DataTable(); 
 
//Includes columns to the DataTable. 
table.Columns.Add("Name"); 
table.Columns.Add("Age"); 
table.Columns.Add("Sex"); 
 
//Includes rows to the DataTable. 
table.Rows.Add(new string[] { "abc", "21", "Male" }); 
 
//Assigns data source. 
pdfLightTable.DataSource = table; 
 
//Includes the style to display the header of the light table. 
pdfLightTable.Style.ShowHeader = true; 
 
//Draws PdfLightTable and returns the rendered bounds. 
PdfLayoutResult result = pdfLightTable.Draw(page, new PointF(10, 40)); 
 
//draw string with returned bounds from table 
graphics.DrawString(text, font, brush, result.Bounds.X,result.Bounds.Bottom, drawFormat); 
 
// Creates a new PdfLightTable. 
pdfLightTable = new PdfLightTable(); 
 
pdfLightTable.DataSource = table; 
 
//Includes the style to display the header of the light table. 
pdfLightTable.Style.ShowHeader = true; 
 
//Draws PdfLightTable with returned bounds from previous table 
PdfLayoutResult layoutResult = pdfLightTable.Draw(page, new PointF(10, result.Bounds.Bottom + 10)); 
 
//draw string with returned bounds from table 
graphics.DrawString(text, font, brush, bounds.X, layoutResult.Bounds.Bottom + 10, drawFormat); 
//Saves the document. 
doc.Save("Output.pdf"); 
doc.Close(true); 
 

Please let us know if you need further assistance on this. 

Regards, 
Prakash V. 



BU Bogdan Ungureanu January 22, 2017 02:00 PM UTC

Hello Prakash,

Thank you for the reply! It worked perfectly now and as expected! Thumbs up to you!


GR Gayathri Ramalingam Syncfusion Team January 23, 2017 05:06 AM UTC

Hi Bogdan, 
Thank you for your update.  
Please get back to us if you need further assistance on this.  
 
With Regards, 
Gayathri R 
 


Loader.
Live Chat Icon For mobile
Up arrow icon