2015-12-09 173 views
1

我目前使用itextPdf庫來生成PDF文件。Image in mode mosaic in PdfPCell

對於設置我用itextpdf.com的this solution圖像0​​現在我想成立一​​個小尺寸的圖像作爲PdfPCell在模式馬賽克背景:如果電池有3×IMAGESIZE,在PDF我將我的圖像重複單元格中的3次

我該怎麼做?

這是我的例子

public class ImageBackgroundEvent implements PdfPCellEvent { 

    protected Image image; 
    protected boolean mosaic; 
    protected boolean full; 

    public ImageBackgroundEvent(Image image, boolean mosaic, boolean full) { 
     this.image = image; 
     this.mosaic = mosaic; 
     this.full = full; 
    } 

    public void cellLayout(PdfPCell cell, Rectangle position, 
          PdfContentByte[] canvases) { 
     try { 
      PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS]; 
      if(full){ 
       cell.setImage(image); 
      } 
      else if(mosaic){ 
       float imgWidth = image.getWidth(); 
       float imgHeight = image.getHeight(); 

       float cellWidth = cell.getWidth(); 
       float cellHeight = cell.getHeight(); 

       if(imgHeight < cellHeight && imgWidth < cellWidth){ 
        PdfPatternPainter pattern = cb.createPattern(imgWidth, imgHeight); 
        pattern.addImage(image); 
        pattern.setPatternMatrix(-0.5f, 0f, 0f, 0.5f, 0f, 0f); 
        cb.setPatternFill(pattern); 
        //cb.ellipse(180, 408, 450, 534); 
        cb.fillStroke(); 

       } else{ 
        image.scaleAbsolute(position); 
        image.setAbsolutePosition(position.getLeft(), position.getBottom()); 
        cb.addImage(image); 
       } 
      } else{ 
       image.scaleAbsolute(position); 
       image.setAbsolutePosition(position.getLeft(), position.getBottom()); 
       cb.addImage(image); 
      } 

     } catch (DocumentException e) { 
      throw new ExceptionConverter(e); 
     } 
    } 
} 
+1

是的,這是可能的。 –

+0

布魯諾,感謝您的回覆。我編輯了我的問題。我不知道如何應用馬賽克過程。 – Valeriane

+0

我會創建一個圖案顏色使用圖像作爲瓷磚模式中的一個瓷磚。然後,我會將該圖案顏色作爲背景顏色應用。 –

回答

1

請大家看看TiledBackgroundColor例子。這需要一個燈泡的圖像,並用它來定義圖案顏色:

PdfContentByte canvas = writer.getDirectContent(); 
Image image = Image.getInstance(IMG); 
PdfPatternPainter img_pattern = canvas.createPattern(
     image.getScaledWidth(), image.getScaledHeight()); 
image.setAbsolutePosition(0, 0); 
img_pattern.addImage(image); 
BaseColor color = new PatternColor(img_pattern); 

現在你可以使用顏色你的背景:

PdfPCell cell = new PdfPCell(); 
cell.setFixedHeight(60); 
cell.setBackgroundColor(color); 
table.addCell(cell); 

結果如下: tiled_patterncolor.pdf

enter image description here

或者你可以在一個單元事件中添加圖像,如圖中TiledBackground例子。這個例子是寫在回答這個問題iTextSharp. Why cell background image is rotated 90 degrees clockwise?

我寫這個例子的變化:TiledBackgroundColor2

事件看起來是這樣的:

class TiledImageBackground implements PdfPCellEvent { 

    protected Image image; 

    public TiledImageBackground(Image image) { 
     this.image = image; 
    } 

    public void cellLayout(PdfPCell cell, Rectangle position, 
      PdfContentByte[] canvases) { 
     try { 
      PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS]; 
      image.scaleToFit(10000000, position.getHeight()); 
      float x = position.getLeft(); 
      float y = position.getBottom(); 
      while (x + image.getScaledWidth() < position.getRight()) { 
       image.setAbsolutePosition(x, y); 
       cb.addImage(image); 
       x += image.getScaledWidth(); 
      } 
     } catch (DocumentException e) { 
      throw new ExceptionConverter(e); 
     } 
    } 

} 

正如你看到的,我不關心圖像的實際尺寸。我按照適合細胞高度的方式縮放圖像。我也不使用圖案顏色。我只是添加圖像的時間,因爲它適合單元格的寬度。

這是我的聲明事件到小區:

PdfPCell cell = new PdfPCell(); 
Image image = Image.getInstance(IMG); 
cell.setCellEvent(new TiledImageBackground(image)); 

結果看起來是這樣的:

enter image description here

許多變化是可能根據您的具體要求。