PDFBox制作PDF时如何给图片添加边框?

3

我一直在使用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中可用的图像周围实际添加边框?

2个回答

6
这是一段添加红色边框的代码:
        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”更改为较小的数字。


1
哇,代码更少,甚至比我的还锐利。太棒了的答案。谢谢你。 - antihero989
谢谢...作为奖励,您还可以查看setLineCapStyle和setLineJoinStyle以查看边缘/线末端的不同样式。 - Tilman Hausherr
我一定会认真研究的。 - antihero989
1
在这一行缺少一个括号: contentStream.drawXObject(ximage, 100, 400, (ximage.getWidth()*scale), ximage.getHeight()*scale); - Shaikh Hafiz Ahamed
在我看来,这有点多余,我已经将其删除。谢谢!(希望我理解正确) - Tilman Hausherr

1

我找不到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;
}

希望这能帮助到使用Java PDFBox的未来用户!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接