iText PdfContentByte addTemplate 位置不影响

发布于 2024-10-09 07:33:42 字数 943 浏览 2 评论 0原文

我正在尝试将位置设置为添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埋情葬爱 2024-10-16 07:33:42

您的模板是空的...您直接从作者的直接内容中获取 PdfGraphics2D (CB.createGraphics 而不是您可能想要的 TP.createGraphics)。

有几种解决方案:

选项 1:从模板获取 Graphics2D

 Graphics2D g2 = tp.createGraphics(...)

选项 2:放弃模板,直接将图表移动到 contentByte 中。 Graphics2D 接口有点笨重,因此您通常应该尽可能直接在 contentByte 中执行操作。它工作得很好,但它构建的内容流效率并不高。在这种特殊情况下,我认为这并不重要,但这是一个很好的经验法则。

 cb.saveState();
 cb.concatMatrix(1, 0, 0, 1, 200, 300);
 Graphics2D g2 = cb.createGraphics(...);
 ...
 g2.dispose();
 cb.restoreState();
 document.close();

选项三:放弃模板并从 Graphics2D 实例中移动图表:

 g2.transform(AffineTransform.getTranslateInstance(200, 300));
 chart.draw(...);

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

 Graphics2D g2 = tp.createGraphics(...)

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.

 cb.saveState();
 cb.concatMatrix(1, 0, 0, 1, 200, 300);
 Graphics2D g2 = cb.createGraphics(...);
 ...
 g2.dispose();
 cb.restoreState();
 document.close();

Option three: Ditch the template and move the chart from within the Graphics2D instance:

 g2.transform(AffineTransform.getTranslateInstance(200, 300));
 chart.draw(...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文