繁体   English   中英

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

[英]Border around Images when making PDF in 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;
}

使用此代码或任何代码,是否有任何方法可以在通过 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”更改为较小的数字。

我找不到允许创建边框的 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;
}

希望这对 PDFBox for Java 的未来用户有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM