Points -> pixels iText (im)precision

孤街浪徒 提交于 2021-02-10 06:00:13

问题


Okay, here's some code (pdfDocument is a com.itextpdf.text.Document):

PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell();
cell.setFixedHeight(3f);
for (int i = 1; i < 100; i++)
{
    table.addCell(cell);
}

pdfDocument.add(table);

According to my calculations (pixels = points / 0.75f) 3f points should be EXACTLY 4 pixels on a screen with 96dpi (which mine is). However, when I create a table with the code above, I get alternating heights of the cells 4 - 3 - 4 - 3 - 4 - 3 etc...

Why does this happen? I would understand the value I put in was something that scales to x.6 pixels so every so often would appear one cell that is a pixel higher than the rest.


eg.:

If I decrease the value to 2f, most of the cells will be 2 pixels high, but every 10th ot 15th will be 3 pixels high.

If I increase the value to 4f, all cells will be 5 pixels high, but every third border wll be one pixel thicker.


Also, if I do use x.6 as a value, will the value be rounded (5.99 -> 6) or floored (5.99 -> 5) to an integer?

Since iText only works with points, I'm guessing it's an internal PDF thing,. but I'd still like to know.

I need to now exactly how high a cell would be given the float value so I can precisely predict how high the whole table or object would be so I can then calucalte that into the composition of the whole page. If I don't know reliably, it's impossible for me to do.

Is there a reliable way in which I can calculate exactly what the float value should be for the pdf version to be exactly x pixels high? It seems that the formula I'm using doesn't work, or maybe there's something else?


回答1:


At first glance the table rows seem to have different heights:

Grid with 3f height at 100%

But if you zoom in, you'll see that they actually have the same height, here e.g. at 1600%:

Grid with 3f height at 1600%

Thus, the effect you see at 100% is merely a display artifact of the PDF viewer.

If you furthermore look at the page content you'll find:

0.5 w
88.3 803 418.4 3 re
S
0.5 w
88.3 800 418.4 3 re
S
0.5 w
88.3 797 418.4 3 re
S
0.5 w
88.3 794 418.4 3 re
S
0.5 w
88.3 791 418.4 3 re
S
.
.
.
0.5 w
88.3 515 418.4 3 re
S
0.5 w
88.3 512 418.4 3 re
S
0.5 w
88.3 509 418.4 3 re
S

i.e. 99 rectangles with a height of 3 each and a vertical distance of 3, too, with a line width of 0.5. Furthermore there is no transformation or userspace unit definition, so the unit of those dimensions is 1 / 72".

In essence, therefore, the PDF content describes exactly what you wanted, a table of rows each of which exactly 3 / 72" high.

Thus, any display inaccuracy is due to limitations of the PDF viewer, the display device, or any other component involved (e.g. graphic drivers, OS display abstractions, ...)



来源:https://stackoverflow.com/questions/18078590/points-pixels-itext-imprecision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!