4

私は PDF を作成していますが、そこのどこかに JPanel を追加したいと考えています。

を使用PdfContentBytePdfGraphics2Dてドキュメントに追加することはできますが、

  • ページの左端ではなく、左余白に配置するにはどうすればよいですか?
  • 他の要素の上に表示されないようにするにはどうすればよいですか?
  • 言い換えれば、どのように段落に入れることができますか?

コードフラグメント:

// 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
//...
4

1 に答える 1

4

を使用して、そこから をPdfTemplate作成することで機能Imageしました。

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)
于 2013-09-27T08:18:19.917 に答える