在 PDFBox 中制作 PDF 时图像周围的边框

ant*_*989 3 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");

    //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;
}
Run Code Online (Sandbox Code Playgroud)

使用此代码或任何代码,是否有任何方法可以在通过 PDFBox API 提供的图像本身周围添加边框?

Til*_*err 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();
Run Code Online (Sandbox Code Playgroud)

祝你好运!您当然可以将“3”更改为较小的数字。