Welcome Guest Search | Active Topics | Sign In | Register

HtmlToPdf - higher DPI Options
Civica Pty Ltd
Posted: Thursday, January 28, 2016 3:51:47 AM
Rank: Member
Groups: Member

Joined: 7/9/2014
Posts: 20
Hi,

I'm using HtmlToPdf to generate page images (I just opened a separate thread about disposal). I've come up with a hacky way of getting higher resolution images but was wondering if you could work a better way into the API (or perhaps such a way exists and I overlooked it).

I want A4 images but at ~300dpi instead of the 96dpi. I do this in my code

Code: C#
//in my class
    private const float EoPdfDefaultDpi = 96;


    //simplified extract of method code
            //calculate the multiplier
            float multiplier = dpi / EoPdfDefaultDpi;    

            //Start with an A4 page, but then blow it up to be bigger
            var pageSize = PdfPageSizes.A4;
            pageSize = new SizeF(pageSize.Width * 3, pageSize.Height * 3);
            //Create output area - have half inch margin top & bottom plus another half inch at the top... Also have 1.0" at right
            var outputRect = (new RectangleF(0, 0.5f, pageSize.Width - 1.0f, pageSize.Height - 1.5f));

            var options = new HtmlToPdfOptions()
            {
                PageSize = pageSize,
                AutoFitX = HtmlToPdfAutoFitMode.None,
                AutoFitY = HtmlToPdfAutoFitMode.None,
                ZoomLevel = (float)Math.Pow(pageSize.Height / pageSize.Width, multiplier),
                AllowLocalAccess = true,
                AutoAdjustForDPI = false,
                OutputArea = outputRect,
                GeneratePageImages = true,
                PreserveHighResImages = true
            };
            
            var dummyPdfDoc = new PdfDocument();

            var result = HtmlToPdf.ConvertUrl(url, dummyPdfDoc, options);
            
            for (int i = 0; i < result.pageImages.Length; i++)  
            {
               var pageImage = result.pageImages[imageIndex];

               var clonedImage = (Bitmap)pageImage.Clone();

               clonedImage.SetResolution(clonedImage.HorizontalResolution * multiplier, clonedImage.VerticalResolution * multiplier);

               //go do something with the cloned image
               //.....

               //  (aside: in my other support thread, it's at this point in the code that I'm wondering if I should call pageImage.Dispose();)
            }


So I get the size of an A4 page and multiply it whilst also using the HtmlToPdfOptions ZoomLevel to account for the big page. I then fix up the bitmap's resolution afterwards.

It works really well but feels like cheating. Is there a better way of doing this?

Thank you for your help.
eo_support
Posted: Thursday, January 28, 2016 10:29:46 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Hi,

Your method is actually the correct way to do this. The DPI value by itself doesn't really mean much, it's just a hint used mostly by the printer. The pixel size of the image is what matters. So increasing DPI without increasing the pixel size is useless. By applying a zoom value, you have correctly increased the pixel size of the image. Once you increase the pixel size of the image, you can increase the DPI value accordingly to maintain the same physical size. However since your image pixel size has been increased, the image quality is increased.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.