We're sorry GroupDocs doesn't work properply without JavaScript enabled.

Free Support Forum - groupdocs.com

Cannot Resize the HTML file When Converting to PNG in C#

Hi,
When I convert HTML>PNG with the code from the link below, it does not allow me to assign the page size.
Even if I change the page size, it is using as Pagesize.Letter (612x792).
No matter what I set as Pagesize, the png is always 612x792.

    using (Viewer viewer = new Viewer("sample.html"))
    {
        var viewOptions = new PngViewOptions("sample_{0}.png");
        viewOptions.EmailOptions.PageSize = PageSize.A4;
        viewer.View(viewOptions);
    }

@fsimsek

This option viewOptions.EmailOptions.PageSize works only when you’re rendering e-mail messages e.g. MSG. You can adjust output image size by setting Width and Height properties

using (Viewer viewer = new Viewer("sample.html"))
{
    PngViewOptions viewOptions = new PngViewOptions();
    viewOptions.Width = 600;
    viewOptions.Height = 800;
    
    viewer.View(viewOptions);
}

In case you set Width or Height the image will be resized proportionally.

I set the width and height values as in the code below. The width of the source html page is 950px. no matter what page size I use, the resulting image does not display the entire width of the page. Therefore, I cannot view the entire invoice.

Results

Sample.html

Code;

public class TestView1
    {
        public void Do()
        {
            var file = @"sample.HTML";

            foreach (var size in PageSizes.All)
            {
                using (Viewer viewer = new Viewer(file, new LoadOptions() { Encoding = Encoding.UTF8 }))
                {
                    var viewOptions = new PngViewOptions("result_" + size.Name + "_{0}.png");
                    viewOptions.Width = size.Width;
                    viewOptions.Height = size.Height;
                    viewer.View(viewOptions, new[] { 1 });
                }
            }
        }


        public class PageSizes
        {
            public static PageSize LETTER = new PageSize(792, 612, "LETTER");
            public static PageSize LEDGER = new PageSize(1224, 792, "LEDGER");
            public static PageSize A0 = new PageSize(3371, 2384, "A0");
            public static PageSize A1 = new PageSize(2384, 1685, "A1");
            public static PageSize A2 = new PageSize(1684, 1190, "A2");
            public static PageSize A3 = new PageSize(1190, 842, "A3");
            public static PageSize A4 = new PageSize(842, 595, "A4");

            public static List<PageSize> All = new List<PageSize>() { LETTER, LEDGER, A0, A1, A2, A3, A4 };

            public class PageSize
            {
                public PageSize() { }
                public PageSize(int w, int h, string name)
                {
                    this.Name = name;
                    this.Width = w;
                    this.Height = h;
                }

                public string Name;
                public int Width;
                public int Height;
            }

        }
    }

@fsimsek

Thank you for attaching the source and output files. We’ll take a look and update you.

@fsimsek

We’ve completed the investigation of this issue. When opening and rendering HTML documents too narrow page size is set so the part of the content does not fit. Unfortunately, there is no way to set a page size when rendering HTML files. We’re going to add support for setting page size in the next public release of GroupDocs.Viewer for .NET that is planned to be published by the end of this month. The code would be similar to what you’ve tried, except this time it would be WordProcessingOptions instead of EmailOptions

    using (Viewer viewer = new Viewer("sample.html"))
    {
        var viewOptions = new PngViewOptions("sample_{0}.png");
        viewOptions.WordProcessingOptions.PageSize = PageSize.A3;
        viewer.View(viewOptions);
    }