How to convert and view HTML files with user defined margins

Introduction

When you are converting HTML files, you can ajdust top/bottom, left/right page margins in final document. The units of margins are typography points.
Default values are:
Top/Bottom - 72
Left/Right - 5

Final margin value is calculated as:
Final margin value = [HTML page margin value] + [margin value in viewer ]

Page in Internet Explorer

How to set margins values

Setting margins values when converting to PNG

To set margins values when converting HTML files to PNG with GroupDocs.Viewer for .NET use following code:

using (Viewer viewer = new Viewer("sample.htm"))
{
       PngViewOptions options = new PngViewOptions("result_page_{0}.png");
       options.WordProcessingOptions.LeftMargin = 40;
       options.WordProcessingOptions.RightMargin = 40;
       options.WordProcessingOptions.TopMargin = 40;
       options.WordProcessingOptions.BottomMargin = 40;

       viewer.View(options);
}


The following screenshot shows the output PNG file opened in a Windows Photo Viewer application.

Output PNG result

Setting margins values when converting to JPG

To set margins values when converting HTML files to PNG with GroupDocs.Viewer for .NET use following code:

using (Viewer viewer = new Viewer("sample.htm"))
{
       JpgViewOptions options = new JpgViewOptions("result_page_{0}.jpg");
       options.WordProcessingOptions.LeftMargin = 40;
       options.WordProcessingOptions.RightMargin = 40;
       options.WordProcessingOptions.TopMargin = 40;
       options.WordProcessingOptions.BottomMargin = 40;

       viewer.View(options);
}


The following screenshot shows the output JPEG file opened in a Windows Photo Viewer application.

Output JPEG result

Setting margins values when converting to PDF

To set margins values when converting HTML files to PDF with GroupDocs.Viewer for .NET use following code:

using (Viewer viewer = new Viewer("sample.htm"))
{
       PdfViewOptions options = new PdfViewOptions("result.pdf");
       options.WordProcessingOptions.LeftMargin = 40;
       options.WordProcessingOptions.RightMargin = 40;
       options.WordProcessingOptions.TopMargin = 40;
       options.WordProcessingOptions.BottomMargin = 40;

       viewer.View(options);
}


The following screenshot shows the output PDF file opened in an Acrobat Reader.

Output PDF result

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured .NET library we provide simple but powerful free Apps. You are welcome to view Word, PDF, Excel, PowerPoint documents with free to use online GroupDocs Viewer App.