Java 在PDFBox中制作PDF时图像周围的边框
我一直在使用PDFBox生成pdf文件,想知道是否可以在图像周围添加边框。如果没有,是否有某种算法可以让您高效地精确地在图像周围绘制线条?我有以下代码,可以将图像添加到pdf页面:Java 在PDFBox中制作PDF时图像周围的边框,java,pdf,pdfbox,Java,Pdf,Pdfbox,我一直在使用PDFBox生成pdf文件,想知道是否可以在图像周围添加边框。如果没有,是否有某种算法可以让您高效地精确地在图像周围绘制线条?我有以下代码,可以将图像添加到pdf页面: //image for page 2 public File processPDF() { //creating pdf PDDocument document = new PDDocument(); File file = new File("NWProofReference.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提供的图像本身周围添加边框 我在API中找不到任何允许创建边框的部分,但我确实提出了一些代码,允许我们使用以下方法在图像周围创建一个纤细干净的边框:
PDPageContentStream.drawLine(xStart, yStart, xEnd, yEnd)
加上我在问题中发布的代码,以下是我的答案:
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;
float titleWidth, titleHeight, width, height;
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
xStart = 100; //left most x position of image
yStart = 400; //bottom most y position of image
width = ximage.getWidth()*scale; //width of image
height = ximage.getHeight()*scale; //height of image
contentStream.drawXObject(ximage,xStart,yStart,width, height); //draw image
//start to draw border
contentStream.drawLine(xStart, yStart, xStart + width, yStart); //bottom
contentStream.drawLine(xStart, yStart + height , xStart + width, yStart + height); //top
contentStream.drawLine(xStart, yStart, xStart, yStart + height); //left
contentStream.drawLine(xStart + width, yStart, xStart + width, yStart + height); //right
document.save(file);
document.close();
} catch (Exception e)
{
e.printStackTrace();
}
contentStream.close();
return file;
}
希望这对PDFBOxforJava的用户和未来用户有所帮助 下面是一些添加红色边框的代码:
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”改为一个较小的数字。哇,代码更少,甚至比我的代码更锐利。真棒的回答。谢谢,谢谢。。。另外,您还可以查看setLineCapStyle和setLineJoinStyle,以查看边/线端的不同样式。我一定会查看它。这一行
contentStream.drawXObject(ximage,100400,(ximage.getWidth()*缩放,ximage.getHeight()*缩放)缺少一个括号
我觉得太多了,我已经把它拿走了。谢谢!(希望我把它弄对了)