When I render PDF from HTML, PageInfo.AnyMargin does not seem to have any effect.
This it the code I’m using
private void CreatePdfDocument()
{
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(_htmlDocument.Html!));
var options = GetHtmlLoadOptions();
_pdfDocument = new Document(ms, options);
}
protected HtmlLoadOptions GetHtmlLoadOptions()
{
var options = new HtmlLoadOptions();
options.PageInfo.Margin = GetFirstPageMarginInfo();
options.PageInfo.AnyMargin = GetNextPagesMarginInfo();
options.PageInfo.Width = 597.6;
options.PageInfo.Height = 842.4;
return options;
}
@VladimirEfimenko
Answer: Why HtmlLoadOptions.PageInfo.AnyMargin seems to have no effect and how to make it work
1. What AnyMargin does
HtmlLoadOptions.PageInfo.AnyMargin is the default margin that Aspose.PDF applies to every page after the first one when an HTML document is converted to PDF.
PageInfo.Margin → margin for the first page
PageInfo.AnyMargin → margin for all subsequent pages
If the HTML itself contains CSS margin definitions (e.g., body { margin: … } or an @page rule), those CSS values have higher priority and will overwrite the values supplied through HtmlLoadOptions. That is the most common reason why AnyMargin appears to be ignored.
2. Typical reasons why AnyMargin does not work
| # |
Reason |
How to verify / fix |
| 1 |
CSS in the HTML sets its own margins (body, @page, etc.) |
Open the source HTML and look for margin: or @page rules. Remove them or set them to 0 (e.g., @page { margin:0 }). |
| 2 |
GetNextPagesMarginInfo() returns null or an invalid MarginInfo |
Debug that method and make sure it returns a fully‑populated MarginInfo (Left, Right, Top, Bottom) in points (1 pt = 1/72 in). |
| 3 |
Using an older Aspose.PDF for .NET version where the bug existed |
Upgrade to the latest release (≥ 23.12 at the time of writing). The bug where AnyMargin was ignored for HTML→PDF conversion was fixed in version 22.10. |
| 4 |
The HTML never splits into multiple pages (content fits on one page) |
Add enough content or explicit page‑breaks (<div style="page-break-before:always;"></div>) to force more than one page. AnyMargin is applied only when a second page is created. |
| 5 |
Using PageInfo.IsFixedMargins = true (if you set it) |
Do not set IsFixedMargins – it forces the same margin on every page. |
3. Minimal, working example
using System.IO;
using System.Text;
using Aspose.Pdf;
private void CreatePdfDocument()
{
// -------------------------------------------------
// 1️⃣ Prepare the HTML source (UTF‑8 encoded)
// -------------------------------------------------
var html = _htmlDocument.Html!; // your HTML source
using var htmlStream = new MemoryStream(Encoding.UTF8.GetBytes(html));
// -------------------------------------------------
// 2️⃣ Configure HtmlLoadOptions with distinct margins
// -------------------------------------------------
var loadOptions = new HtmlLoadOptions
{
// Page size (A4 in points)
PageInfo = {
Width = 597.6, // 210 mm × 72 pt/inch
Height = 842.4, // 297 mm × 72 pt/inch
// Margin for the **first** page
Margin = new MarginInfo {
Left = 40, // 40 pt ≈ 14 mm
Right = 40,
Top = 60,
Bottom = 60
},
// Margin for **all following** pages
AnyMargin = new MarginInfo {
Left = 20,
Right = 20,
Top = 30,
Bottom = 30
}
}
};
// -------------------------------------------------
// 3️⃣ Load the document
// -------------------------------------------------
var pdfDocument = new Document(htmlStream, loadOptions);
// -------------------------------------------------
// 4️⃣ Save / further process
// -------------------------------------------------
pdfDocument.Save("Result.pdf");
}
Important points in the sample
MarginInfo values are expressed in points (1 pt = 1/72 in).
- The page size (A4) is set explicitly – otherwise the library may pick a different size that changes the layout.
- No CSS margin is defined in the HTML; if you need a CSS reset you can add at the top of the HTML:
<style>
@page { margin:0 }
body { margin:0 }
</style>
4. Checklist to troubleshoot your current code
-
Inspect the HTML – does it contain any margin: or @page CSS?
If yes → remove or neutralise them.
-
Verify GetNextPagesMarginInfo() returns a non‑null MarginInfo with proper numeric values.
-
Confirm you are on a recent Aspose.PDF for .NET version (check Aspose.Pdf.Version).
If not → download the latest from https://downloads.aspose.com/pdf/net.
-
Force a second page (add a page‑break element) to see whether the margin changes.
-
Run the minimal example above with the same HTML.
If the margin changes as expected, the problem resides in one of the points above.
5. Links to the official documentation
6. TL;DR
AnyMargin works, but CSS margins in the HTML (or an outdated library) will override it.
- Ensure the HTML does not set its own margins, supply a valid
MarginInfo for AnyMargin, and use the latest Aspose.PDF for .NET version.
If after applying the checklist the problem still persists, please reply with:
- The exact Aspose.PDF for .NET version you are using.
- A small reproducible HTML snippet (or the part that defines margins).
- The values returned by
GetFirstPageMarginInfo() and GetNextPagesMarginInfo().
I’ll be happy to dive deeper with that information.
Input:
Aspose.PDF for .net version 25.10.0
html string - without any margins
code:
private void CreatePdfDocument()
{
using var ms = new MemoryStream(Encoding.UTF8.GetBytes(htmlStringWithoutMargins);
var options = GetHtmlLoadOptions();
_pdfDocument = new Document(ms, options);
}
protected HtmlLoadOptions GetHtmlLoadOptions()
{
var options = new HtmlLoadOptions();
options.PageInfo.Margin = new MarginInfo();
options.PageInfo.AnyMargin = new MarginInfo(100, 100, 100, 100);
options.PageInfo.Width = 597.6;
options.PageInfo.Height = 842.4;
return options;
}
Result:
No margins applied on a second page. (the same as on a first page)
Also, when I create a new PDF document from scratch and use Document.PageInfo.AnyMargin property it does not affect on anything.
So that if Margin and AnyMargin set then only Margin will be applied to all pages, AnyMargin being ignored completely.
Can you explain the purpose of AnyMargin property? I thought it defined a margin for all pages except the first one.
public void Test()
{
SetLicense();
var document = new Aspose.Pdf.Document();
var margin = new MarginInfo { Top = 10, Bottom = 10, Left = 10, Right = 10 };
var anyMargin = new MarginInfo { Top = 100, Bottom = 100, Left = 100, Right = 100 };
document.PageInfo.Margin = margin;
document.PageInfo.AnyMargin = anyMargin;
var page1 = document.Pages.Add();
var textFragment = new TextFragment("This is a text on a page");
textFragment.TextState.FontSize = 20;
textFragment.TextState.Font = FontRepository.FindFont("Arial");
page1.Paragraphs.Add(textFragment);
var page2 = document.Pages.Add();
page2.Paragraphs.Add(textFragment);
document.Save("C:\\temp\\test_output_pdf_margin.pdf");
}
@VladimirEfimenko
If possible, could you please provide a HTML string or file in .zip format as well? We will log an investigation ticket in our issue tracking system and share the ID with you.
test.zip (2.6 KB)
Thank you for the reply. Here is zipped html file attached.
This is the test code I use:
public void Test2()
{
SetLicense();
var margin = new MarginInfo { Top = 10, Bottom = 10, Left = 10, Right = 10 };
var anyMargin = new MarginInfo { Top = 200, Bottom = 200, Left = 200, Right = 200 };
var htmlLoadOptions = new HtmlLoadOptions()
{
PageInfo = { Margin = margin, AnyMargin = anyMargin }
};
var document = new Aspose.Pdf.Document(filename: "C:\\temp\\test.html", options: htmlLoadOptions);
document.Save("C:\\temp\\test2_output_pdf_margin.pdf");
}
@VladimirEfimenko
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PDFNET-61168
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
1 Like
Thanks a lot for your help