How to match the size of graphics with the size of a page?
I am creating a graphical object using PdfGraphics2D
, but it has a different size than my page. How do I match the size of my graphics with the size of my page?
Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); PdfContentByte cb = writer.getDirectContent(); PdfTemplate pdfTemplate = cb.createTemplate(750,750); Graphics2D g2 = pdfTemplate.createGraphics(750, 750); Drawer drawer = new Drawer(); drawer.setSource(new File(jTextField1.getText())); drawer.paintComponent(g2); g2.dispose(); cb.addTemplate(pdfTemplate, 10, 10); document.close();
Posted on StackOverflow on Mar 23, 2015 by jiji
When you create a Document
object like this:
Document document = new Document();
You are creating a document with size A4 that measures 595 x 842 user units (1 user unit = 1 point by default).
You also create a PdfTemplate
that measures 750 x 750 user units:
PdfTemplate pdfTemplate = cb.createTemplate(750,750);
And you add this template with an offset of 10 user units:
cb.addTemplate(pdfTemplate, 10, 10);
Graphics that measure 750 by 750 user units do not match with pages that measuer 595 by 842 user units. How do we solve this?
Option 1: adapt the page to your graphics:
I assume that you want a margin of 10 user units on every side, so the dimension of your page should be at least 770 x 770 user units (750 to accommodate the image plus 10 times two for the margins on either side). In that case, you should create your document like this:
Document document = new Document(new Rectangle(770, 770));
Now you are creating a document with a page that corresponds with the size of the graphic you want to add.
Option 2: adapt the size of your graphics to the size of your page:
Throw away this code:
PdfTemplate pdfTemplate = cb.createTemplate(750,750);
Graphics2D g2 = pdfTemplate.createGraphics(750, 750);
Drawer drawer = new Drawer();
drawer.setSource(new File(jTextField1.getText()));
drawer.paintComponent(g2);
g2.dispose();
cb.addTemplate(pdfTemplate, 10, 10);
Replace it with:
PdfTemplate pdfTemplate = cb.createTemplate(750,750);
Graphics2D g2 = pdfTemplate.createGraphics(750, 750);
Drawer drawer = new Drawer();
drawer.setSource(new File(jTextField1.getText()));
drawer.paintComponent(g2);
g2.dispose();
Image img = Image.getInstance(pdfTemplate);
img.scaleToFit(575, 822);
img.setAbsolutePosition(10, 10);
document.add(img);
As you can see, we scale the image so that it measures 20 user units less in width (and height) than the page. That's because your code indicates that you want a margin of 10 user units for the image. Now we are sure that the image will fit the page (and even leave a margin of at least 10 points).
We define the absolute position at (10, 10)
which were the coordinates you used in your addTemplate()
method and we add the image to the document.
Don't worry: wrapping a PdfTemplate
inside an Image
object will not rasterize the image. If the template contained vector data, the vector data will be preserved.