How to add HTML styled text in ASP.NET Core?
The HTML-to-PDF convertor is a .NET PDF library for converting webpages, SVG MHTML and HTML files to PDF using C#. It uses the popular rendering engine Blink (Google Chrome). It is reliable and accurate. The result preserves all graphics, images, text, fonts, and the layout of the original HTML document or webpage.
Using this library, you can add HTML styled text in PDF document.
Steps to add HTML styled text in a PDF programmatically:
- Create a new ASP.NET Core MVC application.
- Install the Syncfusion.Pdf.Net.Core NuGet package as reference to your .NET Core application from NuGet.org
- Use the following code samples in Index.cshtml.
@{
Html.BeginForm("CreatePdf", "Home", FormMethod.Get);
{
<div>
<input type="submit" value="Create PDF" style="width:150px;height:27px">
</div>
}
Html.EndForm();
}
- Include the following namespaces in the HomeController.cs file.
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
- Use the following code sample in HomeController.cs for converting HTML styled string to PDF in C#.
public IActionResult CreatePdf()
{
//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);
stream.Position = 0;
return File(stream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Pdf, "Output.pdf");
}
Build and run the application, the website will open in the browser. Then you can click create PDF button, you will get the PDF document as follows.
You can download a complete working sample from HtmlStyledText.zip.
Take a moment to peruse the documentation, where you can find converting HTML pages to PDF document along with respective customization options and features.
Click hereto explore the rich set of Syncfusion Essential PDF features.
Note:
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or the NuGet feed, include a license key in your projects. Refer to the linkto learn about generating and registering the Syncfusion license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to add HTML styled text ASP.NET core.
You can refer to our .NET PDF library feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET PDF library example to understand how to create and manipulate data.
For current customers, you can check out our Document Processing Libraries from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-trac, or feedback portal. We are always happy to assist you!