I have been using PDFBox to generate pdf files and was wondering if it is possible to add a border around images. If not, is there some algorithm that allows you to efficiently draw lines precisely around the image? I have the following code that allows myself to add an image to a pdf page:
//image for page 2
public File processPDF()
{
//creating pdf
PDDocument document = new PDDocument();
File file = new File("NWProofReference.pdf");
//adding first page to pdf, blank
PDPage page = new PDPage();
PDPageContentStream contentStream;
try {
BufferedImage awtImage = ImageIO.read(new File(PDFProcessing.image));
PDXObjectImage ximage = new PDPixelMap(document, awtImage);
float scale = 1.0f; // alter this value to set the image size
contentStream.drawXObject(ximage,100,400,
(ximage.getWidth()*scale,ximage.getHeight()*scale);
contentStream.close();
document.save(file);
document.close();
} catch (Exception e)
{
e.printStackTrace();
}
return file;
}
Using this or any code, is there any way to actually add a border around the image itself that is made available through the PDFBox API?
Here's some code that adds a red border:
BufferedImage awtImage = ImageIO.read(new File(PDFProcessing.image));
PDXObjectImage ximage = new PDPixelMap(document, awtImage);
float scale = 1.0f; // alter this value to set the image size
contentStream.drawXObject(ximage,100,400,ximage.getWidth()*scale,ximage.getHeight()*scale);
// these three lines are new
contentStream.setStrokingColor(Color.red);
contentStream.addRect(100-3, 400-3, ximage.getWidth()*scale+6, ximage.getHeight()*scale+6);
contentStream.closeAndStroke();
contentStream.close();
good luck! You can of course change the "3" to a smaller number.