2014-10-29 42 views
0

我一直在使用PDFBox生成PDF文件并想知道是否可以在图像周围添加边框。如果没有,是否有一些算法可以让您有效地在图像周围绘制线条?我有以下的代码,使自己的图像添加到PDF页面:在PDFBox中制作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?

回答

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”更改为更小的数字。

+0

哇,更少的代码,甚至比我更尖锐的角落。真棒回答。谢谢。 – antihero989 2014-10-30 18:46:34

+0

谢谢......作为奖励,您还可以看看setLineCapStyle和setLineJoinStyle,以查看边缘/线条末端中的不同样式。 – 2014-10-30 19:28:03

+0

我一定会考虑一下。 – antihero989 2014-10-30 20:00:57

0

我找不到任何parto的f允许边界创建API,但我没有想出一些代码,使我们使用创建一个围绕图像的苗条和清洁边境:

PDPageContentStream.drawLine(xStart, yStart, xEnd, yEnd) 

添加到我张贴在我的问题代码,这里就是我的回答:

PDFBox的的
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!

相关问题