java 使用itext 导出pdf 控制图片的大小

在使用itext 导出pdf 的过程中,可能会遇到这样的需求,就是导出文字加图片。使用我们前面写的程序,确实是可以导出图片,如果针对于类似简历的需求,图片填充单元格,会满足效果,如下图所示:
java 使用itext 导出pdf 控制图片的大小

但在实际的很多需求中,可能需要的是小图标,此时我们就需要去控制图片的大小了。
使用com.lowagie.text.Image 控制图片大小的几种方式:

	//5、创建图片对象,加入headerTable中,此处写入图片路径
	 Image image = Image.getInstance("E:\\2019-01\\ygtd.png");  
	// image.scaleAbsolute(5,5);//自定义大小
	 image.scalePercent(150);//缩放百分比 --- 测试这种,图片不显示了。
	// image.scaleToFit(50, 50);//自定义大小

接下来,我们使用使用 基础程序进行测试。基础程序见:java 使用itext导出PDF文件,图片文字左右布局

在基础程序中添加image 对象的位置,修改image 图片的大小:

java 使用itext 导出pdf 控制图片的大小

	//5、创建图片对象,加入headerTable中,此处写入图片路径
	 Image image = Image.getInstance("E:\\2019-01\\ygtd.png");  
	// image.scaleAbsolute(5,5);//自定义大小
	// image.scalePercent(getPercent2(image.getWidth(),image.getHeight()));//缩放百分比 --- 不起作用
	// image.scaleToFit(50, 50);//自定义大小
	 headerTable.addCell(image); 

运行测试后,发现图片大小并未被修改。效果还是如图一 一样。为何会这样呢!
其实headerTable.addCell(image);运行这句之后,image 并加入到table 中,虽然图片大小被修改了。但是加入到table中,图片还是会被拉大,填充整个单元格。此时产生了一个思路:使用单元格的方式,加图片加入到单元格中,使用单元格的构造方法。PdfPCell(Image image, boolean fit);此构造方法会将图片与单元格进行匹配。源代码为:

/**
     * Constructs a <CODE>PdfPCell</CODE> with an <CODE>Image</CODE>.
     * The default padding is 0.25 for a border width of 0.5.
     * 
     * @param image the <CODE>Image</CODE>
     * @param fit <CODE>true</CODE> to fit the image to the cell  【将图像与单元格匹配】
     */
    public PdfPCell(Image image, boolean fit) {
        super(0, 0, 0, 0);
        borderWidth = 0.5f;
        border = BOX;
        if (fit) {
            this.image = image;
            column.setLeading(0, 1);
            setPadding(borderWidth / 2);
        }
        else {
            column.addText(this.phrase = new Phrase(new Chunk(image, 0, 0)));
            column.setLeading(0, 1);
            setPadding(0);
        }
    }

完整的测试程序如下:

/**
 * 思路:使用嵌套表格对象完成数据,图片左右布局
 * 1、创建document对象。
 * 2、创建表格对象PdfPTable headerTable。两列的表格对象。图片分为一列,数据划分为一列。
 * 3、创建左边数据表格PdfPTable iTable,划分为N列
 * 4、往左边表格中写入数据,加入iTable中
 * 5、创建图片对象,加入headerTable中
 * @author liucong
 *
 */
public class MergeCell {
    public static void main(String[] args) {
         float lineHeight1 = (float)25.0;
        float lineHeight2 = (float)25.0;        
         try{
             //新建document对象
             Document pdfDoc = new Document(PageSize.A4.rotate(), 36, 36, 24, 36);
             // 构造好的pdf文件输出位置  
             PdfWriter.getInstance(pdfDoc, new FileOutputStream("F:\\test.pdf"));    
             //打开pdf文件---注:只有document 打开后,才能往文件内写入导出信息
             pdfDoc.open();

             //PDFTable类似于html的表格文件,但是只有列的概念,定义列的数量,不能定义行的数量。
             //创建一个两列的表格
             PdfPTable headerTable = new PdfPTable(2);
             headerTable.setWidthPercentage(40);

             //3、创建左边数据表格PdfPTable iTable,划分为N列

             PdfPTable leftTable = new PdfPTable(4);//创建左边表格
             //4、往左边表格中写入数据,加入iTable中
             //4-1表格中可以合并单元格
             PdfPCell leftCell = new PdfPCell(new Paragraph("Hello"));
             leftCell.setHorizontalAlignment(Element.ALIGN_CENTER);
             leftCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
             leftCell.setFixedHeight(lineHeight1);
             leftCell.setColspan(4);
             leftTable.addCell(leftCell);
             //4-2填充数据
                for(int i=0;i<24;i++){
                 leftCell = new PdfPCell(new Paragraph("data"));
                 leftCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                 leftCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                 leftCell.setFixedHeight(lineHeight2);
                 leftTable.addCell(leftCell);
                }
             //将表格加入到第一列中
             PdfPCell cell = new PdfPCell(leftTable);
             cell.setPadding(0);
             headerTable.addCell(cell);         

             //5、创建图片对象,加入headerTable中,此处写入图片路径
             Image image = Image.getInstance("E:\\2019-01\\ygtd.png");  
            // image.scaleAbsolute(5,5);//自定义大小
            // image.scalePercent(50);//缩放百分比 --- 测试不起作用
            // image.scaleToFit(50, 50);//自定义大小
             PdfPCell acell = new PdfPCell(image,false);
             headerTable.addCell(acell);            
             //将主要的表格headerTable加入document文档对象中
             pdfDoc.add(headerTable);
             //关闭文档对象,注:当文档对象真正关闭后,数据才会写入文件中。
             pdfDoc.close();

         } catch(Exception e) {
                 e.printStackTrace();
         }
     }
 }

附:转发 iText2.1.7的api文档【网上资源】

iText2.1.7的api文档免费下载(百度网盘下载)