我一直在使用 PDFBox 生成 pdf 文件,想知道是否可以在图像周围添加边框。如果没有,是否有某种算法可以让您有效地精确地在图像周围画线?我有以下代码,允许我将图像添加到 pdf 页面:
//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;
}
使用此代码或任何代码,是否有任何方法可以在通过 PDFBox API 提供的图像本身周围实际添加边框?
最佳答案
下面是一些添加红色边框的代码:
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();
祝你好运!您当然可以将“3”更改为更小的数字。
关于java - 在 PDFBox 中制作 PDF 时图像周围的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26640572/