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

How to draw different font style in the same line of PDF in WinRT?

Platform: WinForms |
Control: PDF |
Published Date: January 5, 2016 |
Last Revised Date: January 24, 2020

The PdfTextElement class can be used to draw the text using different font styles in the same line, by getting the bounds of the preceding text. Please find the sample and code snippet below.

C#:

float nextText = 0;
private void Button_Click(object sender, RoutedEventArgs e)
{
//Creates a new pdf document
PdfDocument doc = new PdfDocument();
//Add a page
PdfPage page = doc.Pages.Add();
//Create Pdf graphics for the page
PdfGraphics g = page.Graphics;
PdfBrush brush = PdfBrushes.Black;
PdfFont boldFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12, PdfFontStyle.Bold);
PdfFont regularFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12, PdfFontStyle.Regular);
PdfFont italicFont = new PdfStandardFont(PdfFontFamily.TimesRoman, 12, PdfFontStyle.Italic);
PdfLayoutResult result;
result = AddString("Hello World ", page, 0, brush, boldFont);
result = AddString("Hello World ", page, result.Bounds.Width, brush, regularFont);
result = AddString("Hello World ", page, result.Bounds.Width, brush, italicFont);
}
private PdfLayoutResult AddString(string note, PdfPage newPage, float xPosition, PdfBrush brush, PdfFont font)
{
float SpacValue = 10.0f;
nextText = nextText + (xPosition + SpacValue);
//Rendering Text
PdfTextElement richTextElement = new PdfTextElement(note, font, brush);
//Draws String
PdfLayoutResult result = richTextElement.Draw(newPage, new RectangleF(nextText, 0, 0, 0));
return result;
 }

 

Sample Link:

http://www.syncfusion.com/downloads/support/forum/118609/ze/SinlgeLineText-1228246529

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
DLIMA
Jul 28, 2021

How to draw different font style in the same line of PDF in flutter lib pdf

Reply
Gowthamraj Kumar [Syncfusion]
Jul 29, 2021

Hi DLIMA,

Thank you for contacting Syncfusion support.

We can draw text with various font styles by using ‘PdfTextElement’. We can draw text with fonts which have different font styles and get layout result to maintain the text bounds. Please find the sample to draw text with different font styles from https://www.syncfusion.com/downloads/support/directtrac/general/ze/create_pdf_file-2133312064.

Please find the code example below,

PdfFont regularFont = PdfStandardFont(PdfFontFamily.helvetica, 12);
PdfFont boldFont = PdfStandardFont(PdfFontFamily.helvetica, 12, style: PdfFontStyle.bold);
PdfFont italicFont = PdfStandardFont(PdfFontFamily.helvetica, 12, style: PdfFontStyle.italic);
PdfFont boldItalicFont = PdfStandardFont(PdfFontFamily.helvetica, 12, multiStyle: <PdfFontStyle>[PdfFontStyle.bold, PdfFontStyle.italic]);

//Load the text into PdfTextElement with standard font with regular style
PdfTextElement textElement = PdfTextElement(text: 'This is sample text with different font styles like', font: regularFont);
//Draw the text on page and maintain the position in PdfLayoutResult
PdfTextLayoutResult layoutResult = textElement.draw(page: page, bounds: Rect.fromLTWH(0, 50, page.getClientSize().width, 100))! as PdfTextLayoutResult;

//Load the text into PdfTextElement with standard font with bold style
textElement = PdfTextElement(text: ' bold,', font: boldFont);
//Draw the text on page and maintain the position in PdfLayoutResult
layoutResult = textElement.draw(page: page, bounds: Rect.fromLTWH(layoutResult.lastLineBounds!.right, 50, page.getClientSize().width - layoutResult.lastLineBounds!.right, 100))! as PdfTextLayoutResult;

//Load the text into PdfTextElement with standard font with bold style
textElement = PdfTextElement(text: ' italic,', font: italicFont);
//Draw the text on page and maintain the position in PdfLayoutResult
layoutResult = textElement.draw(page: page, bounds: Rect.fromLTWH(layoutResult.lastLineBounds!.right, 50, page.getClientSize().width - layoutResult.lastLineBounds!.right, 100))! as PdfTextLayoutResult;

//Load the text into PdfTextElement with standard font with bold style
textElement = PdfTextElement(text: ' bold and italic.', font: boldItalicFont);
//Draw the text on page and maintain the position in PdfLayoutResult
layoutResult = textElement.draw(page: page, bounds: Rect.fromLTWH(layoutResult.lastLineBounds!.right, 50, page.getClientSize().width - layoutResult.lastLineBounds!.right, 100))! as PdfTextLayoutResult;

Please let us know if you need any further assistance in this.

Regards,

Gowthamraj K

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon 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.Close Icon

Live Chat Icon For mobile