borderWidth,
BufferedImage.TYPE_INT_RGB);
Graphics2D g1 = bufferedTop.createGraphics();
g1.setBackground(color);
g1.clearRect(0, 0, bufferedTop.getWidth(),bufferedTop.getHeight());
Image top = Image.getInstance(bufferedImage2ByteArray(bufferedTop));
top.setAbsolutePosition(img.getAbsoluteX(), img.getAbsoluteY()+img.getScaledHeight()-bufferedTop.getHeight()/2);
BufferedImage bufferedBottom = new BufferedImage((int)img.getScaledWidth(),
borderWidth,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = bufferedBottom.createGraphics();
g2.setBackground(color);
g2.clearRect(0, 0, bufferedBottom.getWidth(),bufferedBottom.getHeight());
Image bottom = Image.getInstance(bufferedImage2ByteArray(bufferedBottom));
bottom.setAbsolutePosition(img.getAbsoluteX(), img.getAbsoluteY()-bufferedTop.getHeight()/2);
BufferedImage bufferedLeft = new BufferedImage(borderWidth,
(int)img.getScaledHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g3 = bufferedLeft.createGraphics();
g3.setBackground(color);
g3.clearRect(0, 0, bufferedLeft.getWidth(),bufferedLeft.getHeight());
Image left = Image.getInstance(bufferedImage2ByteArray(bufferedLeft));
left.setAbsolutePosition(img.getAbsoluteX()-bufferedLeft.getWidth()/2, img.getAbsoluteY());
BufferedImage bufferedRight = new BufferedImage(borderWidth,
(int)img.getScaledHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D g4 = bufferedRight.createGraphics();
g4.setBackground(color);
g4.clearRect(0, 0, bufferedRight.getWidth(),bufferedRight.getHeight());
Image right = Image.getInstance(bufferedImage2ByteArray(bufferedRight));
right.setAbsolutePosition(img.getAbsoluteX()+img.getScaledWidth()-bufferedRight.getWidth()/2, img.getAbsoluteY());
doc.add(top);
doc.add(bottom);
doc.add(left);