How to place a PDFGraphis2D object in iText?

I am creating a PDF file, and somewhere there I want to add JPanel.

Using PdfContentByte and PdfGraphics2D , I can add it to the document, but:

  • How to place it so that the left margin instead of the left edge of the page?
  • How can I prevent other elements from appearing?
  • In other words: how can I put it in a paragraph?

Code snippet:

 // 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 //... 
+3
source share
1 answer

I started working using PdfTemplate and creating 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) 
+3
source

All Articles