itext导出图片,Echart图片,表格

1 篇文章 0 订阅
1 篇文章 0 订阅

由于项目的需要,需要导出的word包括表格,图片

需要的jar包

需要说明的是必须是这个版本哦,而且我也测试过了哦。首先感谢http://blog.csdn.net/aeolus1019/article/details/7973255的作者。及参考地址

代码如下:

echartImage的获取是从echarts的对象中getDateUrl()获取的,是base64位的。

经过处理:

echartImage = echartImage.replaceAll(" ", "+");

String[] arr = echartImage.split("base64,");
if (arr.length > 1) {
echartImage=arr[1]=;
}

public void exportImage(String echartImage,HttpServletResponse response,String fileName){
try {
//建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中
RtfWriter2.getInstance(document, os);
document.open();
// echartImage = "/9j/"+echartImage;
BASE64Decoder decoder = new BASE64Decoder(); 
byte[] buffer = decoder.decodeBuffer(echartImage); 
//添加图片
Image img = Image.getInstance(buffer);
img.setAbsolutePosition(0, 0);  
       img.setAlignment(Image.ALIGN_CENTER);  
       img.scaleAbsolute(100,100);  
       img.scalePercent(50);  
       img.scalePercent(50, 50);  
       img.setRotation(30); 
       img.setOriginalType(Image.ORIGINAL_PNG);
document.add(img);
setHeader(response,fileName);
} catch (Exception e) {
e.printStackTrace();
}finally{
document.close();
System.out.println("完成");
}
}

//表格

public  void exportWordTable(String titleString,HttpServletResponse response,String fileName){
try {
/** 创建Document对象(word文档) */
  Rectangle rectPageSize = new Rectangle(PageSize.A4);
  rectPageSize = rectPageSize.rotate();
//   String  fileName="F:/企业详细信息登记表_"+System.currentTimeMillis()+".doc";
//   OutputStream out = new FileOutputStream(fileName);
  /** 建立一个书写器与document对象关联,通过书写器可以将文档写入到输出流中 */
  RtfWriter2.getInstance(document, os);
  document.open();
  /** 标题字体*/
  RtfFont titleFont = new RtfFont("微软雅黑", 12, Font.BOLD,Color.BLACK);
  RtfFont contentFont = new RtfFont("微软雅黑", 12, Font.NORMAL,Color.BLACK);
  Table table = getTable();
  /** 第一行(标题)*/
  Paragraph title = new Paragraph(titleString);
  // 设置标题格式对其方式
  title.setAlignment(Element.ALIGN_CENTER);
  document.add(title);
  // 设置第一行空的列数(缩进)
  // context.setFirstLineIndent(20);
  Cell cell=null;
  if(null != columnNames && columnNames.length>0){
  for(int i=0;i<columnNames.length;i++){
  Paragraph p =new Paragraph(columnNames[i],titleFont);  
          p.setAlignment(Element.ALIGN_CENTER);   
          p.setFont(titleFont);  
  cell=new Cell(p);
  cell.setHeader(true);
  table.addCell(cell);
  }
  }
 if(null != dataList && dataList.size()>0){
 for(int i=0;i<dataList.size();i++){
 Map<String,Object> map = dataList.get(i);
 if(null != map && map.size()>0 && null != mappingKeys && mappingKeys.length>0){
 for(int j=0;j<mappingKeys.length;j++){
 Paragraph p =null;  
 Object value = map.get(mappingKeys[j]);
 if(null != value){
 if(j==0){
 p =new Paragraph(value.toString(),titleFont);  
 }else{
 p =new Paragraph(value.toString(),contentFont);
 }
 }else{
 if(j==0){
 p =new Paragraph("",titleFont);  
 }else{
 p =new Paragraph("",contentFont);
 } 
 }
 p.setAlignment(Element.ALIGN_CENTER);   
 cell=new Cell(p);
 table.addCell(cell);  
 }
 }
 }
 }
 
  document.add(table);
  setHeader(response,fileName);
}  catch (Exception e) {
e.printStackTrace();
}finally{
document.close();
System.out.println("完成");
}
}
private Table getTable() {
Table table = null;
try {
  /** 表格设置   第一个参数是列,第二个参数是行*/
  table = new Table(columnNames.length, dataList.size());
//   table = new Table(4, 3);
  /** 居中显示*/
  table.setAlignment(Element.ALIGN_CENTER);
  /** 自动填满  */
  table.setAutoFillEmptyCells(true);
  table.setBorderWidth(5); // 边框宽度  
  table.setBorderColor(new Color(0, 125, 255)); // 边框颜色  
  table.setPadding(12);// 衬距,看效果就知道什么意思了  
  table.setSpacing(0);// 即单元格之间的间距  
  table.setBorder(5);// 边框  
} catch (Exception e) {
e.printStackTrace();
}

return table;
}



iTextPDF是一个流行的Java库,用于处理PDF文档,包括添加、编辑和生成PDF。如果你想要在使用iTextPDF导出PDF时添加图片,你可以按照以下步骤进行: 1. 添加依赖:首先,确保你在项目中已经包含了iTextPDF库。如果你使用Maven,可以在pom.xml文件中添加如下依赖: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> <!-- 请根据最新版本替换 --> </dependency> ``` 2. 导入资源:从类路径或文件系统加载图片,通常使用`Image.getInstance()`方法: ```java import com.itextpdf.text.Image; String imagePath = "path/to/your/image.jpg"; // 替换为你的图片文件路径 Image image = Image.getInstance(imagePath); ``` 3. 将图片添加到PDF:创建PdfPCell来包含图片,然后将它添加到PdfDocument中的某一节: ```java import com.itextpdf.text.Document; import com.itextpdf.text.PdfPCell; import com.itextpdf.text.PdfDocument; import com.itextpdf.text.pdf.ColumnText; import com.itextpdf.text.pdf.PdfWriter; // 创建PdfDocument实例 Document document = new Document(); try (PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("output.pdf"))) { document.open(); // 获取ColumnText对象,便于绘制表格或添加多列内容 ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_CENTER, image, 50, 700, 0); // x, y, rotation // 如果你想在特定的表格单元格中添加图片,可以创建PdfPCell并添加图片 PdfPCell cell = new PdfPCell(image); cell.setBorder(0); // 设置无边框 PdfPTable table = new PdfPTable(1); // 创建一个1列的表格 table.addCell(cell); // 添加图片表格单元格 // 将表格写入PDF table.writeOn(document, 50, 500); // x, y位置 document.newPage(); // 结束当前页面后,开始新的一页 } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值