We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Remove/ reduce page margins for pdf

I am using 17.9 version in my MVC .net project. I’m trying to convert html to pdf file.

Is there a way to reduce/ modify the default page margins. Right now when I print the pdf file, there is lot of extra white space. I tried the following code but the default page margins remained to be the same.
The code I tried (that did not work) is below-

var basePath = HttpContext.Current.Request.Url.Scheme + “://” + HttpContext.Current.Request.Url.Authority;
var htmlOptions = new HtmlLoadOptions(basePath);
htmlOptions.PageInfo.IsLandscape = true;
htmlOptions.PageInfo.Height = 612;
htmlOptions.PageInfo.Width = 792;
var pdf = new Document(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlString)), htmlOptions);

var headerHtml = new HtmlFragment(headerHTML);
HeaderFooter hf;
foreach (Aspose.Pdf.Page page in document.Pages)
{
hf = new Aspose.Pdf.HeaderFooter();
hf.Paragraphs.Add(headerHtml);
page.Header = hf;
}
document.Save(dataDir + “Output.pdf”);

@adityarlv,
You can update page size by calling SetPageSize() method of the Page class. Please refer to this help topic: Update Page Dimensions. However, if this does not help, then kindly send us your source HTML document. We will investigate and share our findings with you.

I ended up using the following code which actually fixed the solution on trimming the white spaces

htmlOptions.PageInfo.Margin.Left = 10;
htmlOptions.PageInfo.Margin.Right = 10;
htmlOptions.PageInfo.Margin.Top = 75;
htmlOptions.PageInfo.Margin.Bottom = 20;

@adityarlv,
It is nice to hear from you that the problem has been resolved. Please refer to another way to trim white-spaces around the page in this code example: Trim White-space Around a Page