如何在iText中定位PDFGraphis2D对象? [英] How to position a PDFGraphis2D object in iText?

查看:151
本文介绍了如何在iText中定位PDFGraphis2D对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个PDF,我想在那里添加一个JPanel。

I'm creating a PDF and somewhere in there I want to add a JPanel.

使用 PdfContentByte PdfGraphics2D 我可以添加它到文件但是:

Using PdfContentByte and PdfGraphics2D I am able to add it to the document but:


  • 如何定位它,使其位于左边距而不是左页边缘?

  • 如何防止它出现在其他元素上?

  • 换句话说:我怎样才能把它放在段落中?

代码片段:

// multiple Paragraphs
// ...
JPanel myPanel = ...

PdfContentByte canvas = writer.getDirectContent();
int origWidth = myPanel.getWidth();
int origHeight = myPanel.getHeight();
float width = document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin();
double scale = width / origWidth;
Graphics2D g2 = new PdfGraphics2D(canvas, origWidth, origHeight);
g2.scale(scale, scale);
myPanel.paint(g2);
g2.dispose();

// even more Paragraphs
//...


推荐答案

我通过使用 PdfTemplate 并从中创建图像那个。

I got it working by using a PdfTemplate and creating an Image from that.

PdfContentByte canvas = writer.getDirectContent();
int origWidth = myPanel.getWidth();
int origHeight = myPanel.getHeight();
PdfTemplate template = canvas.createTemplate(origWidth, origHeight);
Graphics2D g2 = new PdfGraphics2D(template, origWidth, origHeight);
myPanel.paint(g2);
g2.dispose();
Image image = Image.getInstance(template);
float width = document.getPageSize().getWidth() - document.leftMargin() - document.rightMargin();
image.scaleToFit(width, 1000);
document.add(image)

这篇关于如何在iText中定位PDFGraphis2D对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆