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

标签 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;
}

使用此代码或任何代码,是否有任何方法可以在通过 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/

相关文章:

java - 创建 JPA native 查询很慢

java - 如何根据另一个类的更改状态更新进度条?

tomcat - Itext 5.5 将 RTL 语言(阿拉伯语)的 HTML 转换为 PDF 不适用于 tomcat

如果字段多次出现在 PDF 表单中,Java PDFBox 不会维护该字段的字体外观

java - 如何让 javac 和 java 知道我下载的 API?

c# - C# 中继承的奇怪行为

java - 有没有办法在定义函数的返回类型时指定数组的长度?

pdf - BibTeX 引用中的 LaTeX 数学不适用于 pandoc

php - 更改 PDF 文件中的内容

java - PdfBox - PDColorSpaceFactory.createColorSpace(document, iccColorSpace) 抛出 nullpointerexception