EXCEL转PDF,JACOB,生成checkbox

 因为项目报表有EXCEL导出,现在要新增PDF导出,我就想用EXCEL转PDF,于是找到JCOB调用COM组件,但是发现JCOB不能在LINUX下运行。同时JCOB也有个坏处。如果EXCEL里面表格不是自动换行,里面的字段也显示不全。必须严格调整EXCEL的格式。(导入jacob.jar (1.9),同时将jacob.dll放入到jdk目录下jre/bin下面)
 ActiveXComponent app = new ActiveXComponent("Excel.Application");
  String els = “D:\\aaaa.xlsx”;
 String pdf = “D:\\pdf.pdf”;
       try{
           app.setProperty("Visible",false);
           Dispatch workbooks = app.getProperty("Workbooks").toDispatch();
           Dispatch workbook = Dispatch.invoke(workbooks, "Open", Dispatch.Method, new Object[]{els, new Variant(false),new Variant(false)}, new int[3]).toDispatch();
          Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] { pdf, new Variant(57), new Variant(false),new Variant(57), new Variant(57), new Variant(false),new Variant(true), new Variant(57), new Variant(true),new Variant(true), new Variant(true) }, new int[1]);
           Variant f = new Variant(false);
           System.out.println("to pdf" + pdf);
           Dispatch.call(workbook, "Close", f);
       }catch(Exception e){
           e.printStackTrace();
       }finally{
           if(null!=app){
              app.invoke("Quit", new Variant[] {});
           }
       }

于是找到Itext,决定用读POI读EXCEL方式,再写入生成PDF, 在官网上下载了5.5.6还有对应的中文itext-asian.jar包。对应的jar我放在附件里面。用POI读的方式也有个缺点,要非常精准确定行数读取,报表里面有表格还有段落。有点繁琐。
1. 需使用中文字体

BaseFont bfChinese= BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
  Font f8 = new Font(bfChinese,8, Font.NORMAL);
Font f8_bold = new Font(bfChinese,8, Font.BOLD);

加粗:Chunk chunk = new Chunk("显示中文加粗字体",f8_bold);
            document.add(chunk);
增加下划线:Chunk underline = new Chunk("hello,AJava.org ",f8_bold);
           underline.setUnderline(0.1f, -1f);
  1. 生成table,按钮图片
public class TestTest {
    public static void main(String[] args) throws Exception{
       TestTest test = new TestTest();
       Document document = new Document();
        PdfWriter writer= PdfWriter.getInstance(document, new FileOutputStream("src/test.pdf"));
       document.open();
       BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
       Font f8 = new Font(bfChinese, 8, Font.NORMAL);
       Font f8_bold = new Font(bfChinese, 8, Font.BOLD);
       PdfPTable tab = new PdfPTable(2);
       tab.setWidthPercentage(100);
       PdfPCell cell = new PdfPCell(test.createCell(new Phrase("个人客户",f8), new float[]{1,38}, true));
       tab.addCell(cell);
       cell = new PdfPCell(test.createCell(new Phrase("非个人客户",f8), new float[]{1,38}, false));
       tab.addCell(cell);
       document.add(tab);

       document.close();
    }

    public PdfPTable createCell(Phrase phrase,float[] cells,boolean ischecked)throws Exception{
       PdfPTable tab = new PdfPTable(2);
       tab.setWidths(cells);
        PdfPCell cell = newPdfPCell(createImage(ischecked));
       //居中
       cell.setUseAscender(true);
       cell.setUseDescender(true);
       cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//     cell.setPaddingLeft(1f); //左1f
       cell.setBorder(Rectangle.NO_BORDER);
        tab.addCell(cell);
        cell = new PdfPCell(phrase);
      //居中
       cell.setUseAscender(true);
       cell.setUseDescender(true);
       cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//     cell.setPaddingLeft(1f); //左1f
        cell.setBorder(Rectangle.NO_BORDER);
        tab.addCell(cell);
       return tab;
    }

    public PdfPTable createImage(boolean ischecked)throws Exception{
       PdfPTable table = new PdfPTable(1);
       Image img = Image.getInstance("src/btn_uncheck.png");
       if(ischecked){
            img = Image.getInstance("src/btn_checked.png");
       }
       img.scaleToFit(9, 7);
       PdfPCell cell = new PdfPCell();
       cell.addElement(img);
       cell.setBorder(Rectangle.NO_BORDER);
       table.addCell(cell);
       return table;
    }
}
  1. 画图生成checkbox
 public void createCheckBox(PdfWriter writer,int llx,int lly,int urx,int ury,String name,boolean ischeck){
       try{
           RadioCheckField bt = new RadioCheckField(writer, new Rectangle(llx,lly, urx, ury), name, "Yes");
           bt.setCheckType(RadioCheckField.TYPE_CHECK);
           bt.setBorderWidth(BaseField.BORDER_WIDTH_THIN);
           bt.setBorderColor(BaseColor.BLACK);
           bt.setBackgroundColor(BaseColor.WHITE);
           bt.setChecked(ischeck);
           PdfFormField ck = bt.getCheckField();
           writer.addAnnotation(ck);
       }catch(Exception e){
           e.printStackTrace();
       }
    }

用html格式生成pdf,对于html标签有些不能识别,必须严格按要求写html

       Document document = new Document();
       PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));
       document.open();

       XMLWorkerHelper.getInstance().parseXHtml(writer,document, new FileInputStream("D:/template.html"));
       document.close();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值