因为项目报表有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);
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;
}
}
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();