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.

To generate a PDF which inside the paragraph some words' font is different.

Thread ID:

Created:

Updated:

Platform:

Replies:

105224 Sep 19,2012 06:20 PM Sep 21,2012 01:58 AM ASP.NET Web Forms (Classic) 1
loading
Tags: PDF
Tony Qu
Asked On September 19, 2012 06:20 PM

Hi, 

Recently we are trying to generate the PDF which content retrieved from back end table. It should display as below. 

Title 1 - title content (Font different with the items)
    this is the first item under this title, item content could be last in multiple lines (font Italic: datetime: Sep. 1st, 2012). other item content but fond is normal.
    this is the second item under this title, item content could be last in multiple lines (font Italic: datetime: Sep. 1st, 2012). other item content but fond is normal.
    this is the third item under this title, item content could be last in multiple lines (font Italic: datetime: Sep. 1st, 2012). other item content but fond is normal.

Title 2 - title content (Font different with the items)
    this is the first item under this title, item content could be last in multiple lines (font Italic: datetime: Sep. 1st, 2012),other item content but fond is normal.
    this is the second item under this title, item content could be last in multiple lines (font Italic: datetime: Sep. 1st, 2012). other item content but fond is normal.
    this is the third item under this title, item content could be last in multiple lines (font Italic: datetime: Sep. 1st, 2012).other item content but fond is normal.

Actually I am already lost among the class, methods I should use, if you can please shed me a light.....My code snippet is as the following

        Dim firstx As Integer = 10
        Dim secondx As Integer = 10
        Dim thirdx As Integer = 10
        Dim commiteex As Integer = 10

        Dim firsty As Integer = 0
        Dim secondy As Integer = 0
        Dim thirdy As Integer = 0
        Dim commiteey As Integer = 0

        doc = New Syncfusion.Pdf.PdfDocument()
        doc.PageSettings.Margins.Top = 50.4F
        doc.PageSettings.Margins.Bottom = 57.6F
        doc.PageSettings.Margins.Left = 50.0F
        doc.PageSettings.Margins.Right = 43.2F
        doc.PageSettings.Orientation = PdfPageOrientation.Portrait
        doc.PageSettings.Size = PageSize.Letter
        page = doc.Pages.Add()
        graphics = page.Graphics
        font = New PdfStandardFont(PdfFontFamily.TimesRoman, fontSize)
        format = New PdfStringFormat()
        format.Alignment = PdfTextAlignment.Left
        format.LineAlignment = PdfVerticalAlignment.Top
        format.WordWrap = PdfWordWrapType.Word
       
        layoutFormat = New PdfLayoutFormat()
        layoutFormat.Break = PdfLayoutBreakType.FitPage
        layoutFormat.Layout = PdfLayoutType.Paginate
        element = New PdfTextElement()
               
         element.Brush = New PdfSolidBrush(Color.Black)
         element.StringFormat = format

        element.Text = currentstr  ' currentstr is got from back end table in a loop.
        element.Font = New PdfStandardFont(PdfFontFamily.TimesRoman, 9)
        element.Draw(page, New PointF(firstx, firsty), layoutFormat)
         firstx += currentstr.Length

        currentstr = " datetime string get from table"
        element.Text = currentstr
        element.Font = New PdfStandardFont(PdfFontFamily.TimesRoman, 9, PdfFontStyle.Italic)
        element.Draw(page, New PointF(firstx, firsty), layoutFormat)
        firstx += currentstr.Length
 
1. As inside the paragraph line, some text font is different with others, so should I use PdfTextElement or just use the pdfgraphic.DrawString? Also if I use PdfTextElement object, the text won't wrap automatically and keep drawing to the same line and being truncated behind the page margin I set.

2. It seems in previous version, can use pdfDoc.Graphics.MeasureString to measure a string, but why I cannot use this method in 10.3, as I thought  "firstx += currentstr.Length" is wrong, should using MeasureString to get the new location.

3. Is that possible to generate the whole text (include all paragraphs and append into one string, at the end of each item inside the paragraph added vbCrLf for separation), and draw once to multiple pages automatically? then maybe use some "replace" or "search" methods to find those datestring and change their font to be Italic?

If you can please give me some code snippet for the solution, that would be great.

Thanks 

Jiewen
        


Geetha M [Syncfusion]
Replied On September 21, 2012 01:58 AM

Hi Jiewen,

Thank you for your interest in Syncfusion products.

1.    You can make use of PdfTextElement, however, the text wrapping is possible only of you specify the rectangle bounds.

element.Draw(page, New RectangleF(New PointF(firstx, firsty), page.Graphics.ClientSize), layoutFormat)

2.    You can measure the string using the chosen font and not using Graphics. Please check the code snippet below:

firstx += CInt(Fix(element.Font.MeasureString(currentstr).Width))

3.    It is not possible search and replace elements in PDF. You can make use of PdfHtmlTextElement to draw the entire content. This class converts the HTML formatted text to PDF and draws it along with the pagination. Please check the following documentation for more details on this:

Html Styled Text

With this, you can format the text from backend in memory and then draw using PDF. I had placed a simple sample demonstrating this in the link below.

Please try this and let me know if you have any questions.

Regards

Geetha



TextLength_b616a08b.zip

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.

;