When converting HTML to Docx, is it possible to make certain pages landscape? Currently I’m setting the page orientation to Portrait using the @page section in the css provided in the html document. I’d like to be able to allow some pages to be set to Landscape and some to Portrait.
Currently, defining multiple Section elements with a single HTML content is not supported.
So perhaps, as a workaround, you could break your content into multiple HTML instances and use @page on each. Then, you could load each HTML into a separate DocumentModel and merge them at the end.
For example:
static void Main()
{
var html1 = @"
<style>
@page { size: landscape; }
h1 { page-break-before: always; }
</style>
<h1>First Section</h1>
<h1>First Section</h1>";
var html2 = @"
<style>
@page { size: portrait; }
h1 { page-break-before: always; }
</style>
<h1>Second Section</h1>
<h1>Second Section</h1>";
var document1 = LoadHtml(html1);
var document2 = LoadHtml(html2);
document1.Content.End.InsertRange(document2.Content);
document1.Save("output.docx");
}
static DocumentModel LoadHtml(string html)
{
var htmlLoadOptions = new HtmlLoadOptions();
using var htmlStream = new MemoryStream(htmlLoadOptions.Encoding.GetBytes(html));
return DocumentModel.Load(htmlStream, htmlLoadOptions);
}