2014-10-29 149 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!

相關問題