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.
Unfortunately, activation email could not send to your email. Please try again.

Create multiline text in PDF document

Thread ID:

Created:

Updated:

Platform:

Replies:

128336 Jan 15,2017 02:47 PM Jan 23,2017 12:06 AM Xamarin.Forms 5
loading
Tags: PDF
Bogdan Ungureanu
Asked On January 15, 2017 02:47 PM

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!

Gayathri Ramalingam [Syncfusion]
Replied On January 16, 2017 07:55 AM

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 


Bogdan Ungureanu
Replied On January 16, 2017 12:06 PM

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!

Prakash Viswanathan [Syncfusion]
Replied On January 17, 2017 11:19 AM

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. 


Bogdan Ungureanu
Replied On January 22, 2017 09:00 AM

Hello Prakash,

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

Gayathri Ramalingam [Syncfusion]
Replied On January 23, 2017 12:06 AM

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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;