【问题标题】:Border around Images when making PDF in PDFBox在 PDFBox 中制作 PDF 时图像周围的边框
【发布时间】:2014-10-29 20:59:44
【问题描述】:

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

【问题讨论】:

    标签: java pdf pdfbox


    【解决方案1】:

    这是一些添加红色边框的代码:

            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,100,400,(ximage.getWidth()*scale,ximage.getHeight()*scale);
    • 恕我直言,这有点太多了,我已经删除了它。谢谢! (希望我做对了)
    【解决方案2】:

    我在 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 的未来用户有所帮助!

    【讨论】:

      猜你喜欢
      • 2014-01-14
      • 2011-06-08
      • 1970-01-01
      • 2011-08-22
      • 2020-01-01
      • 2023-03-07
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多