pvginkel / PdfiumViewer

PDF viewer based on Google's PDFium.
Apache License 2.0
972 stars 418 forks source link

Possible Bug at PdfDocument.Render() #161

Open M4nju opened 6 years ago

M4nju commented 6 years ago

In the task #79 and #114 is explained how the PdfRenderFlag CorrectFromDpi should be used, to recalculate from points (Pdf maesurement) into pixel. Which looks like this inside the method PdfDocument.Render:

if ((flags & PdfRenderFlags.CorrectFromDpi) != 0) { width = width * (int)dpiX / 72; height = height * (int)dpiY / 72; }

Intersting enough is, that if i use the CorrectFromDpi PdfRenderFlag, the resulting image will be in the wrong size. So a dinA4 pdf file using these settings (I wrote here only the numbers for width and height, which came from the pdfDocument): Image image = document.Render(pageNumber, 595, 842, 600, 600,PdfRenderFlags.CorrectFromDpi) resulted in an image with 4800x6600 pixels and 600dpi. This is the equivalent of 8x11 inches. But a dina4 page should have 8.3x11.7 inches. So i experimented with increasing the file length by some points, to find out that this increase does not change anything. Of cause a little bit of accuracy we loose converting points into pixel in the method PdfDocument.Render. But it shouldnt be that much.

If i Use no Render Flags and execute the method with my own recalculation: Image image = document.Render(i, (int)(595*600/72), (int)(842*600/72), 600, 600, PdfRenderFlags.None) it works perfekt.

I couldnt figure out what is happening there, that the calculations are this far off. Maybe its worth looking for?

Greetings, Manju

MadBeango commented 6 years ago

It's because it's computing on integers in the wrong order. Your computation is far more precise, because it converts the result to int after all the maths. If I take dpiX as 600, in the code way I get (int)dpiX / 72 =>8, therefore Width = 595 8 => 4760. In fact, if you compute it your way, it's Width = 595 8.333, which makes 4958. Look at your code, and you'll see, you're having the cast to int at the very end of the maths, not like in the original code. There you have your difference.