Hello,
When using the PdfTextElement from Syncfusion.Pdf.Graphics to display text on a pdf document: like this
new PdfTextElement(text, font). The text variable is a string containing HTML. Then we draw it on the pdf like this textElement.Draw(page,....)
Is there a way to display the HTML correctly in the PDF and not display the tags?
Kind Regards,
Tsvetan
Hi Tsvetan,
We have created the sample to achieve your requirement of rendering HTML text format in a PDF document using PdfHTMLTextElement class. We have attached the sample and output document for your reference, please try the below sample on your end and let us know the result.
Sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/HtmlTextConsoleCore-583899618
Please find the below code snippet,
|
//create a new PDF document
PdfDocument doc = new PdfDocument();
//Add a page to the document
PdfPage page = doc.Pages.Add();
//create PDF graphics for the page
PdfGraphics graphics = page.Graphics;
//set the font
PdfFont font = new PdfStandardFont(PdfFontFamily.TimesRoman, 14, PdfFontStyle.Regular);
//simple HTML content
string htmlText = "<font color='#0000F8' face='TimesRoman' size='14'><i><b><u>Essential PDF</u></b></i></font> is a <u><i>.NET</i></u> library with the capability to produce Adobe PDF files";
//Render Html text
PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, font, PdfBrushes.Black);
//Format layout
PdfLayoutFormat format = new PdfLayoutFormat();
format.Layout = PdfLayoutType.Paginate;
format.Break = PdfLayoutBreakType.FitPage;
//Draw htmlString.
richTextElement.Draw(page, new RectangleF(0, 20, page.GetClientSize().Width, page.GetClientSize().Height), format);
//Save the document into stream
MemoryStream stream = new MemoryStream();
doc.Save(stream); |
Please refer
to the below link for more information,
UG: https://help.syncfusion.com/file-formats/pdf/working-with-text#adding-a-html-styled-text
Note: The PdfHTMLTextElement class provides support for a basic set of HTML tags, to render HTML format text in the PDF document. If you are using some other tags, it does not render in PDF document. We do not have any possibilities to support the advance HTML tags to render in the PDF document. The below finalize tags are only supported in PdfHTMLTextElement.
|
|
Please try the above solution on your end and let us know if you need any further assistance with this.
Regards,
Gowthamraj K
Hi Gowthamraj,
Thank you for the detailed answer.
Do you have plans for supporting more tags? Like:
strong, ol, ul, li
Tags that are supported by rich text editors.
Kind Regards,
Tsvetan
Hi Tsvetan,
We have only a limited tags support for while drawing HTML text using PdfHTMLTextElement class. The supported tags are mentioned in the following UG documentation https://help.syncfusion.com/file-formats/pdf/working-with-text#adding-a-html-styled-text. So currently we don’t have to support the more styles tags when drawing text using the PdfHTMLTextElement API.
We suggest you use our HTML to PDF converter for rendering complex HTML with CSS and URL. Please refer to the below links for preserving resources (CSS/Images/Scripts etc.,) about HTML string to PDF conversion,
UG: https://help.syncfusion.com/file-formats/pdf/converting-html-to-pdf
KB: https://www.syncfusion.de/kb/8174/how-to-preserve-resources-during-html-string-to-pdf-conversion
HTML string to PDF: https://help.syncfusion.com/file-formats/pdf/convert-html-to-pdf/webkit#html-string-to-pdf
Kindly please try the above-suggested solution on your
end and let us know if it suits your requirement.
Regards,
Gowthamraj K
Hello
I have this working on my pdf for <b> tag thank you! I am trying to the <br> to get a line break but it just errors whenever i use this or the <p> tags...any ideas?
This is my code that works fine
string htmlText = "<b>Essential PDF</b>Is this on a new line?";
PdfFont font = CreatePdfFont(FONT_NAME, fontSize, FontStyle.Regular);
PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, font, CreateSolidBrush(fontColour));
PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Layout = PdfLayoutType.Paginate;
format.Break = PdfLayoutBreakType.FitPage;
richTextElement.Draw(page, new RectangleF(intX + 5, intY, intWidth - 6, height), format);
If I change this line to include a <br> it then breaks and wont load and the exception is just null.
string htmlText = "<b>Essential PDF</b><br>Is this on a new line?";
any ideas why as really need to ideally get line breaks to work... thank you
Hi Tsvetan,
Please use the below code snippet to achieve your requirement:
//Create a new PDF document. PdfDocument doc = new PdfDocument(); //Add a page to the document. PdfPage page = doc.Pages.Add(); //Create PDF graphics for the page. PdfGraphics graphics = page.Graphics; //Set the font. PdfFont font = new PdfStandardFont(PdfFontFamily.Courier, 14); //Simple HTML content string htmlText = "<b>Essential PDF</b><br/>Is this on a new line?"; //Render HtmlText. PdfHTMLTextElement richTextElement = new PdfHTMLTextElement(htmlText, font, PdfBrushes.Black); richTextElement.TextAlign = TextAlign.Left; //Format Layout. PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat(); format.Layout = PdfLayoutType.Paginate; format.Break = PdfLayoutBreakType.FitPage; //Draw htmlString. richTextElement.Draw(page, new RectangleF(0, 20, page.GetClientSize().Width, page.GetClientSize().Height), format); //Save the document. doc.Save("Output.pdf"); //Close the document. doc.Close(true); |
Please try this on your end and let us know the result.
Regards,
Irfana J
Amazing! Thank you Irfana
my issue seemed to be using <br> and I need to use <br/> as you had!
thank you!