Apache PDFBox Renders Straight Line Crooked in PNG

雨燕双飞 提交于 2019-12-08 04:23:45

问题


I have a PDF that when I render it to a png it renders a line crooked, or rather with a step in it. This is the PDF and what it should look like: https://drive.google.com/file/d/1E-zucbreD7pVwWc3Z4MNe_lzsP6D9m49/view

Here is the full PNG rendering using PDFBox 2.0.13 and openjdk version 1.8.0_181:

And here is the specific portion of the PNG that has the step:


回答1:


Excerpt of the page content stream:

q
1 0 0 1 35.761 450.003 cm
0 i
0.75 w
0 0 m
50.923 0 l
S
Q

q
1 0 0 1 86.139 450 cm
0 i
0.75 w
0 0 m
14.9 0 l
S
Q

("cm" is an affine transform, "m" a moveto, "l" a lineto). One can see that the two lines are slightly different, one at 450.003, the other one at 450.

Here's some code that simulates the error by replicating what PDFBox is doing:

BufferedImage bim = new BufferedImage(612, 792, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) bim.getGraphics();
RenderingHints r = new RenderingHints(null);
r.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
r.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
r.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.addRenderingHints(r);
g.translate(0, 792);
g.scale(1, -1);
g.setStroke(new BasicStroke(0.75f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10));
g.setColor(Color.black);
GeneralPath path = new GeneralPath();
path.moveTo(35.761f, 450.003f);
path.lineTo(35.761f + 50.923f, 450.003f);
g.draw(path);
path = new GeneralPath();
path.moveTo(86.139f, 450f);
path.lineTo(86.139f + 14.9f, 450f);
g.draw(path);
g.dispose();
ImageIO.write(bim, "png", new File("...."));

One can get rid of the error by commenting this line:

r.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

This could be done in the source code of PDFBox, or by passing the renderingHints in PDFRenderer.setRenderingHints(). However that one isn't available now, but will be available in 2.0.14 (see issue PDFBOX-4435, try a snapshot). And you can expect the rendering to be of poor quality by not having anti aliasing.

Update: instead of removing the line mentioned above, add this one:

r.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);

Source.



来源:https://stackoverflow.com/questions/54658008/apache-pdfbox-renders-straight-line-crooked-in-png

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