How to set different margin settings in HTML when converting to PDF using C# in ASP.NET MVC?
Syncfusion® HTML to PDF for .NET is used to convert webpages, SVG, MHTML, and HTML to PDF. Using this library, you can set different page margins in HTML when converting to PDF document.
Steps to set different margin settings in HTML to PDF conversion programmatically:
- Create a new ASP.NET MVC application project.
- Install the Syncfusion.HtmlToPdfConverter.AspNet.Mvc5 NuGet package as a reference to your .NET Framework applications from NuGet.org.
- A default controller with name HomeController.cs gets added on creation of ASP.NET MVC project. Include the following namespaces in that HomeController.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.HtmlConverter; using System.Drawing;
- A default action method named Index will be presented in HomeController.cs. Right-click this action method and select Go To View, where you will be directed to its associated view page Index.cshtml.
- Add a new button in the Index.cshtml as follows.
@using (Html.BeginForm("ConvertHtmlToPdf", "Home", FormMethod.Post)) { <input type="submit" value="Convert" />
- Add a new action method named ConvertHtmlToPdf in HomeController.cs and include the following code snippet to set different margin settings in the conversion of HTML to PDF file and download it.
C#
//Input HTML URL to convert string url = Server.MapPath("~/Data/TableHTML.htm"); //Initialize HTML to PDF converter HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter(); //Initialize Blink converter settings BlinkConverterSettings settings = new BlinkConverterSettings(); //Enable JavaScript settings.EnableJavaScript = true; settings.MediaType = MediaType.Print; //Disable split images and text lines settings.SplitImages = false; settings.SplitTextLines = false; //Set blink view port size settings.ViewPortSize = new Size(1200, 0); //Set PDF page margin settings.Margin = new Syncfusion.Pdf.Graphics.PdfMargins { Top = 40, Left = 30, Right = 40, Bottom = 50 }; htmlConverter.ConverterSettings = settings; //Convert URL to PDF document PdfDocument document = htmlConverter.Convert(url); //Save and close the PDF document document.Save("Output.pdf", HttpContext.ApplicationInstance.Response, HttpReadType.Save); document.Close(true); return null;
A complete working sample can be downloaded from DifferentMarginSettingsHtmlToPDF.zip.
By executing the program, you will get the PDF file as follows.
Take a moment to peruse the documentation, where you can find converting HTML pages to PDF document along with respective customization options and features.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
An online sample link for Converting HTML to PDF.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message. |
Conclusion
I hope you enjoyed learning about how to set different margin settings in HTML when converting to PDF using C# in ASP.NET MVC.
You can refer to our ASP.NET MVC PDF feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to present and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!