This blog post was inspired by a problem I looked at this week. A customer was reporting issues with some code which turned out to be due to some errors in what they were inputting on the Graphics2D settings. The code was doing exactly what it was told to do. Software is like that – it does not do what you want it to do, only what you tell it to…
“On two occasions I have been asked, “Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?” … I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.”
—Charles Babbage, 1864, Passages from the Life of a Philosopher
Many times it can be all to easy to make assumptions about code and not think any further about it. This can often lead to incorrect inputs (Garbage In) and incorrect output (Garbage Out). In my opinion this is often one of the most common causes of reported “issues” when using software libraries, especially amongst new comers to a library or project. The important thing to remember is that the code is stupid. It does exactly what it is told. This isn’t even a new concept either, it can be dated back to the 1864 and maybe even earlier.
For quite some time we have had a method that will draw pdf pages straight on to a Graphics2d object to be displayed elsewhere. This method will draw the designated page on to the graphics2d object passed in at the specified scaling value.
It should be noted about now that the renderToG2 method is stupid, it does exactly what it is told. The method will attempt to draw the pdf page at the given scaling regardless of the size of the graphics2D object. This means if the graphics2d object has a smaller drawable area than the page would cover you will only get a partial page drawn.
This also applies for pages that are rotated within the pdf. When a page is rotated to 90 or 270 degrees the pages height and width values also need to be swapped to take this into account (as the pdf height and width are the pages height and width before any rotation).
So if using these values to determine the size of the graphics2D you need to remember these values will need to be swapped on rotated pages otherwise you will find sections of the page could be missing.
Did you know...
IDRsolutions offers a whole range of online file converters to convert PDF and Microsoft Excel, Word and Office Documents to HTML5, SVG or image formats?
It is free to use for single file conversions and also includes Developer links if you want to use our commercial software for bulk conversions. Find out more on this page