[Solved] Help converting html to pdf

1 Answer 9 Views
PdfProcessing
Jose
Top achievements
Rank 1
Jose asked on 10 Aug 2026, 12:09 PM

Hi Guys

I am having problem with output pdf shrunk with too much {top, left, right and bottom} margins.

Is there a way to fix output pdf result to not shrink the page margins?

ASP.NET C# WebForm Framework 4.7.2 and Using Telerik File Ver 2017.1.213.40

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using Telerik.Windows.Documents.Flow.FormatProviders.Html;
using Telerik.Windows.Documents.Flow.FormatProviders.Pdf;
using Telerik.Windows.Documents.Flow.Model;

public static byte[] ConvertHtmlToPdf(string htmlContent)
        {
            // 1. Initialize the HTML provider to parse the string
            HtmlFormatProvider htmlProvider = new HtmlFormatProvider();

            // 2. Import the HTML into a Telerik RadFlowDocument object
            RadFlowDocument document = htmlProvider.Import(htmlContent);

            // 3. Initialize the PDF provider to convert the document object
            PdfFormatProvider pdfProvider = new PdfFormatProvider();

            // 4. Export the document structure to a byte array
            byte[] pdfBytes = pdfProvider.Export(document);

            return pdfBytes;
        }


1 Answer, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 10 Aug 2026, 05:04 PM

Hello Jose,

I saw you opened this question on the following public item as well: Convert Html To PDF.

I am pasting the same answer for visibility, but if you have any additional questions plaese, use one of the public threads.

You can manipulate the page margins from the RadFlowDocument API using the Sections's PageMargins property:

public static byte[] ConvertHtmlToPdf(string htmlContent)
{
	// 1. Initialize the HTML provider to parse the string
	HtmlFormatProvider htmlProvider = new HtmlFormatProvider();

	// 2. Import the HTML into a Telerik RadFlowDocument object
	RadFlowDocument document = htmlProvider.Import(htmlContent);
	
	// 2.1 Remove the document margins
	foreach (Section section in document.Sections)
	{
		section.PageMargins = new Padding(0);
	}

	// 3. Initialize the PDF provider to convert the document object
	PdfFormatProvider pdfProvider = new PdfFormatProvider();

	// 4. Export the document structure to a byte array
	byte[] pdfBytes = pdfProvider.Export(document);

	return pdfBytes;
}

Regards,
Martin
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
PdfProcessing
Asked by
Jose
Top achievements
Rank 1
Answers by
Martin
Telerik team
Share this question
or