iText PdfContentByte addTemplate 位置不影响
我正在尝试将位置设置为添加到 PDF 的图像,但它始终定位为 0,0。 我搜索了很多但找不到解决方案。我想我对定位不太了解。
这是始终定位到 0,0 的代码,但它应该是 200,300!
非常感谢您的帮助,
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue(String.format("%s, %s", "pie1", "pie1"),20);
dataset.setValue(String.format("%s, %s", "pie2", "pie2"),80);
JFreeChart chart = ChartFactory.createPieChart("testPie", dataset, true, true, false);
Document document = new Document();
document.addCreationDate();
document.setPageSize(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(300, 300);
Graphics2D g2 = cb.createGraphics(300, 300, new DefaultFontMapper());
Rectangle2D r2D = new Rectangle2D.Double(0, 0, 300, 300);
chart.draw(g2, r2D, null);
g2.dispose();
cb.addTemplate(tp, 200, 300);
document.close();
I am trying to set position to image which i added to PDF but it always positions to 0,0.
I searched a lot but could not find the solution. I think i could not understand well about positioning.
Here is the code that always postions to 0,0 but it should be 200,300!
Thanks a lot for your help,
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue(String.format("%s, %s", "pie1", "pie1"),20);
dataset.setValue(String.format("%s, %s", "pie2", "pie2"),80);
JFreeChart chart = ChartFactory.createPieChart("testPie", dataset, true, true, false);
Document document = new Document();
document.addCreationDate();
document.setPageSize(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("test.pdf"));
document.open();
PdfContentByte cb = writer.getDirectContent();
PdfTemplate tp = cb.createTemplate(300, 300);
Graphics2D g2 = cb.createGraphics(300, 300, new DefaultFontMapper());
Rectangle2D r2D = new Rectangle2D.Double(0, 0, 300, 300);
chart.draw(g2, r2D, null);
g2.dispose();
cb.addTemplate(tp, 200, 300);
document.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的模板是空的...您直接从作者的直接内容中获取 PdfGraphics2D (CB.createGraphics 而不是您可能想要的 TP.createGraphics)。
有几种解决方案:
选项 1:从模板获取 Graphics2D
选项 2:放弃模板,直接将图表移动到 contentByte 中。 Graphics2D 接口有点笨重,因此您通常应该尽可能直接在 contentByte 中执行操作。它工作得很好,但它构建的内容流效率并不高。在这种特殊情况下,我认为这并不重要,但这是一个很好的经验法则。
选项三:放弃模板并从 Graphics2D 实例中移动图表:
Your template is empty... you're getting the PdfGraphics2D directly from the writer's direct content (CB.createGraphics instead of what you probably intended, TP.createGraphics).
There are several solutions:
option 1: get the Graphics2D from the template
option 2: ditch the template, move the chart directly in the contentByte. The graphics2D interface is a bit clunky, so you should generally prefer to do things directly in the contentByte whenever possible. It works fine, but the content stream it builds isn't as efficient as it could be. In this particular case, I don't think it'll matter, but that's a good rule of thumb.
Option three: Ditch the template and move the chart from within the Graphics2D instance: