Setting Page Width when Exporting to HTML

Hi

I have a question regarding export to HTML

Everything works fine, except I haven’t been able to figure out how to control the width of the document displayed on screen.

So far, no matter what I’ve tried, it didn’t work.

I just need some way to specify that the body should have margin: 0 auto, and then some width so that the content is centered on screen.

I tried setting margins and page width on the sections in the document, and it didn’t work

I also thought that it would be possible to provide my own CSS file using HtmlSaveOptions.CssStyleSheetFileName but it turned out it’s just a css file that holds the styles for the document.

Last but not least, I’m using Aspose.Words 10.3

Any ideas?

Hi Dejan,

Thanks for your inquiry and sorry for the delayed response.

Please note that each Section in Aspose.Words document model provides page setup information via PageSetup class. When you export a document to HTML format you can keep this information for further usage. Also, in most cases HTML is intended for viewing in browsers where pagination is not performed. You can use HtmlSaveOptions.ExportPageSetup property to specify whether page setup is exported to HTML, MHTML or EPUB or not. However, I am afraid, this will not affect the layout of the output HTML document.

Best Regards,

Hi there,

Thanks for your inquiry.

The Aspose.Words Document Object Model is based on the structure of a Word document. Since a section does not contain any horizontal alignment, in order to make all content aligned to the center of the page you should set all paragraphs and table content to center aligned instead.

Please see the code below:

foreach(Paragraph para in doc.GetChildNodes(NodeType.Paragraph, true))
    para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
foreach(Table table in doc.GetChildNodes(NodeType.Table, true))
    table.Alignment = TableAlignment.Center;

Thanks,

awais.hafeez:
You can use HtmlSaveOptions.ExportPageSetup property to specify whether page setup is exported to HTML, MHTML or EPUB or not. However, I am afraid, this will not affect the layout of the output HTML document.

Thanks for the response awais, but as you said, the suggested code will not work. This is the CSS (invalid) that is exported in the document

@page Section1 { size:400pt 841.9pt; margin:72pt }
div.Section1 { page:Section1 } 

It’s a pity that the width is not set on the body element, because the body selector is rendered in the styles

Thanks for your input aske, but I do not want the text in the paragraphs centered, I want the whole document centered on screen, and inside the document, have the paragraphs left aligned

Hi there,

Thanks for your inquiry.

I’m afraid since MS Word has no such analog, we do not write such an attribute to HTML. I suggest you try post processing the HTML to add the center attribute to any div elements.

Thanks,