2016-02-05 55 views
0

我想为放置在pdf中的图像创建3D阴影效果。我正在使用itextpdf。 问题类似于: Adding shadow effect on iText elements使用itextpdf在图像上创建3D阴影效果

但与图像,而不是文本。 我的图像放置在表格单元格中。只要表格没有完成,就无法获得图像的实际大小或页面中的坐标,这使得它很棘手。 任何辉煌的ID? 感谢和问候, Sylvain

+0

请出示您的绘图图像的码表单元格。 – mkl

回答

0

4小时后,我找到了一种方法来使用PdfCellEvent并绘制我需要在单元格的填充内。

static class ShadowRectangle implements PdfPCellEvent { 
     public void cellLayout(PdfPCell cell, Rectangle rect, 
           PdfContentByte[] canvas) { 
      PdfContentByte lCb = canvas[PdfPTable.LINECANVAS]; 

      // Paddings for the border 
     int paddingHorizontal = 8; 
     int paddingVertical = 8; 

     // Width of the shadow 
     int shadowwidth = 7; 

     // Calculate border location and size 
     float left = rect.getLeft() + paddingHorizontal; 
     float bottom = rect.getBottom() + paddingVertical; 
     float width = rect.getWidth() - 2 * paddingHorizontal; 
     float height = rect.getHeight() - 2 * paddingVertical; 


     lCb.saveState(); 
     lCb.setColorFill(BaseColor.GRAY); 
     // Draw the shadow at the bottom 
     lCb.rectangle(left + shadowwidth, bottom - shadowwidth, width, shadowwidth); 
     lCb.fill(); 
     // Draw the shadow at the right 
     lCb.rectangle(left + width, bottom - shadowwidth, shadowwidth, height); 
     lCb.fill(); 

     //lCb.setColorStroke(BaseColor.RED); 
     // Draw the border 
     //lCb.rectangle(left, bottom, width, height); 

     lCb.stroke(); 
     lCb.restoreState(); 
    } 
} 

//我填补PdfPTable这样

PdfPTable lImages = new PdfPTable(NB_PICTURE_PER_ROW); 
lImages.getDefaultCell().setBorder(Rectangle.NO_BORDER); 

// ...在一个循环

lImageBoucle = Image.getInstance(lFile.getCanonicalPath() + File.separator + lTabPhotos[i]); 
       PdfPCell lCell = new PdfPCell(); 
       lCell.setImage(lImageBoucle); 
       lCell.setBorder(Rectangle.NO_BORDER); 
       lCell.setPadding(8); 

       PdfPCellEvent lShadowRectangle = new ShadowRectangle(); 
       lCell.setCellEvent(lShadowRectangle); 
       lImages.addCell(lCell);