Insert HTML String in Word Document using C# .NET | Fit Page Size Based on Content | Maximum Allowed Page Width Height

I want to use InsertHtml to add an Html string in Word document by this code:

var doc = new Aspose.Words.Document();
var builder = new Aspose.Words.DocumentBuilder(doc);
builder.InsertHtml(html, true);
doc.Save(targetFilePath, Aspose.Words.SaveFormat.Docx);

It works fine but when the Html string contains large width content the final Word document does not show all of Html. (Like attached file).
Insert Html.zip (12.0 KB)

I look for a way to autofit page size base on the content. It is important to me that do not change the Html size and import it in the original size. So I need to change page size.

@freydoonk,

Aspose.Words mimics the behavior of MS Word. Try to open this HTML with MS Word and ‘Save As’ to DOCX format. However, you can adjust page size by using the PageSetup.PageWidth, PageSetup.PageHeight or PageSetup.PaperSize properties. You can also adjust the orientation of the page by using PageSetup.Orientation Property.

Is there any way to calculate content size?

@freydoonk,

Please check Aspose.Words.Layout namespace that provides classes (such as LayoutCollector & LayoutEnumerator) that allow to access information such as on what page and where on a page particular document elements are positioned.

In your case, the HTML comprises of a Table and you can use the following code to measure which Table Row has the highest Width and according to that set the width of Page:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

PageSetup ps = doc.FirstSection.PageSetup;
// Maximum allowed page width/height you can specify using MS Word is 22 inches
ps.PageWidth = 22 * 72;
ps.PageHeight = 22 * 72;
ps.LeftMargin = 0;
ps.RightMargin = 0;

string html = File.ReadAllText("C:\\temp\\Insert Html\\chart.html");
builder.InsertHtml(html);

Table table = doc.FirstSection.Body.Tables[0];

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

double maxWidth = 0;
foreach (Row row in table.Rows)
{
    enumerator.Current = enumerator.Current = collector.GetEntity(row.LastCell.FirstParagraph);

    while (enumerator.MoveParent())
        if (enumerator.Type.Equals(LayoutEntityType.Row))
            break;

    // Get the width of Row
    float rowWidth = enumerator.Rectangle.Left + enumerator.Rectangle.Width;

    if ((rowWidth > maxWidth))
        maxWidth = rowWidth;
}

ps.PageWidth = maxWidth;

doc.Save("C:\\temp\\Insert Html\\20.10.docx");

@awais.hafeez

Thank you. The code works fine. I changed it like bellow to calculate PageHeight too.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

PageSetup ps = doc.FirstSection.PageSetup;
// Maximum allowed page width/height you can specify using MS Word is 22 inches
ps.PageWidth = 22 * 72;
ps.PageHeight = 22 * 72;

string html = File.ReadAllText("C:\\temp\\Insert Html\\chart.html");
builder.InsertHtml(html);

var collector = new Aspose.Words.Layout.LayoutCollector(doc);
var enumerator = new Aspose.Words.Layout.LayoutEnumerator(doc);

double maxWidth = 0;
double maxHeight = 0;
var table = doc.FirstSection.Body.Tables[0];

foreach (Aspose.Words.Tables.Row row in table.Rows)
{
	enumerator.Current = collector.GetEntity(row.LastCell.FirstParagraph);

	while (enumerator.MoveParent())
	{
	    if (enumerator.Type.Equals(Aspose.Words.Layout.LayoutEntityType.Row))
	        break;
	}

    // Get the width of Row
	float rowWidth = enumerator.Rectangle.Left + enumerator.Rectangle.Width; 

    // Get the height of Row
	float rowHeight = enumerator.Rectangle.Top + enumerator.Rectangle.Height;

	if (rowWidth > maxWidth)
	    maxWidth = rowWidth;

	if (rowHeight > maxHeight)
	    maxHeight = rowHeight;
}

ps.PageWidth = maxWidth + ps.LeftMargin + ps.RightMargin;
ps.PageHeight = maxHeight + ps.FooterDistance + ps.HeaderDistance + ps.TopMargin + ps.BottomMargin;

doc.Save("C:\\temp\\Insert Html\\20.10.docx");

@freydoonk,

It is great that you were able to find what you were looking for. Please let us know any time you may have any further queries in future.