Java itext给PdfPTable单元格绘制圆角矩形

itext给PdfPTable单元格绘制圆角矩形

最近工作中用到了itext来绘制图形,里面需要用到圆角矩形,本来打算通过图片直接展示的,但是考虑到图片的样式调整耗时不一定比直接绘制省时,于是在网上搜了一下,用了itext自带的画笔进行绘制,废话不多说,直接上代码:


cell.setCellEvent((cell, position, canvases) -> {
//		获取单元格背景画布
        PdfContentByte cb = canvases[PdfPTable.BACKGROUNDCANVAS];
//		保存前面的状态
        cb.saveState();
//      设置边框为0
        cb.setLineWidth(0f);
//      绘制原型矩阵样式
        cb.roundRectangle(position.getLeft() + 2.4f, position.getBottom() + 2f, position.getWidth() - 4,
                position.getHeight() - 1.5f,
                2f);
//		填充画笔颜色
        cb.setColorStroke(BaseColor.BLACK);
        cb.setColorFill(BaseColor.BLACK);
        cb.fill();
//		绘制
        cb.stroke();
//		重置画笔状态,如果不重置,则后面的单元格会受到影响
        cb.restoreState();
});

嗯,就这么简单哈哈,还有不明白的?度娘嘛

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,使用iTextPdfPTable类将Excel转换为PDF的步骤如下: 1. 添加iText库依赖 在您的Java项目中,您需要添加iText库的依赖。您可以在iText官网上下载iText的jar包,然后将其添加到项目的classpath中。 2. 使用POI库读取Excel文件 使用POI库读取Excel文件内容,并将其存储在Java程序中。以下是一个示例代码: ```java FileInputStream inputStream = new FileInputStream(new File("example.xlsx")); Workbook workbook = new XSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); Iterator<Row> iterator = sheet.iterator(); ``` 3. 创建PDF文件 使用iText库创建一个新的PDF文件,并设置页面大小、边距等。以下是创建PDF文件的示例代码: ```java Document pdfDoc = new Document(PageSize.A4, 50, 50, 50, 50); PdfWriter.getInstance(pdfDoc, new FileOutputStream(new File("example.pdf"))); pdfDoc.open(); ``` 4. 将Excel表格转换为PDF表格 使用iTextPdfPTable类创建一个新的PDF表格,并将Excel表格中的内容添加到PDF表格中。以下是将Excel表格转换为PDF表格的示例代码: ```java PdfPTable pdfTable = new PdfPTable(3); PdfPCell cell; while (iterator.hasNext()) { Row row = iterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell excelCell = cellIterator.next(); cell = new PdfPCell(new Phrase(excelCell.getStringCellValue())); pdfTable.addCell(cell); } } pdfDoc.add(pdfTable); ``` 5. 关闭PDF文件 使用pdfDoc.close()方法关闭PDF文件。 完整的示例代码如下: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; import com.itextpdf.text.Phrase; import com.itextpdf.text.pdf.PdfPCell; import com.itextpdf.text.pdf.PdfPTable; import com.itextpdf.text.pdf.PdfWriter; public class ExcelToPdf { public static void main(String[] args) throws IOException { FileInputStream inputStream = new FileInputStream(new File("example.xlsx")); Workbook workbook = new XSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); Iterator<Row> iterator = sheet.iterator(); Document pdfDoc = new Document(PageSize.A4, 50, 50, 50, 50); PdfWriter.getInstance(pdfDoc, new FileOutputStream(new File("example.pdf"))); pdfDoc.open(); PdfPTable pdfTable = new PdfPTable(3); PdfPCell cell; while (iterator.hasNext()) { Row row = iterator.next(); Iterator<Cell> cellIterator = row.cellIterator(); while (cellIterator.hasNext()) { Cell excelCell = cellIterator.next(); cell = new PdfPCell(new Phrase(excelCell.getStringCellValue())); pdfTable.addCell(cell); } } pdfDoc.add(pdfTable); pdfDoc.close(); workbook.close(); inputStream.close(); } } ``` 希望这可以帮助您将Excel文件转换为PDF文件!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值