本文介绍了Apache PDFBox呈现PNG弯曲的直线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PDF,当我将其渲染为png时,它会渲染弯曲的线,或者在其中带有台阶.这是PDF,其外观应为: https://drive.google .com/file/d/1E-zucbreD7pVwWcc3Z4MNe_lzsP6D9m49/view

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

以下是使用PDFBox 2.0.13和openjdk版本1.8.0_181的完整PNG渲染:

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

以下是具有该步骤的PNG的特定部分:

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

推荐答案

页面内容流的摘录:

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"是仿射变换,"m"是移入,"l"是移入).可以看到两条线略有不同,一条在450.003,另一条在450.

("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.

下面是一些通过复制PDFBox正在执行的操作来模拟错误的代码:

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);

这可以在PDFBox的源代码中完成,也可以通过在PDFRenderer.setRenderingHints()中传递renderHints来完成.但是,该版本目前不可用,但将在2.0.14中可用(请参见问题 PDFBOX-4435 ,请尝试快照).而且,由于没有抗锯齿,因此可以预期渲染质量很差.

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);

来源.

这篇关于Apache PDFBox呈现PNG弯曲的直线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 21:58