操作方法
/** *首先啥也不干,先写一个转换中文的方法,话说谁要整一个全英文数字的就不 * 要写这个方法了.... *str :要转换的内容 c:字体大小 */ private static Paragraph getChinese(String str,int c) throws DocumentException, IOException{ BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); Font FontChinese = new Font(bfChinese, c, Font.NORMAL); // 设置为中文 Paragraph pragraph=new Paragraph(str, FontChinese); return pragraph;
我这里举一个PDF表格的例子 File path = new File(Util.DOWNLOAD+"确认件PDF");//给将要生成的PDF创建一个容身之所 if(!path.exists()){ path.mkdirs(); } String name = Util.DOWNLOAD+"确认件PDF/"+filename+".PDF" FileOutputStream fos = new FileOutputStream(name);
Document document = new Document();// 文档 PdfWriter.getInstance(document,fos);//PDF文档 document.open();//写入数据之前要打开文档 Table table = new Table(8, 13);//创建一个8列13行的表格 //PS 这里一行有8列,只有在8列满了之后会自动换行,8列不满是会继续在这行的列里写入数据的 table.setBorderWidth((float) 0.7); //表格边框线条宽度table.setPadding(5);//边距table.setSpacing(0);//间距
private final static int BT =15;//大表头private final static int xBT =10;//表头private final static int NR =7;//内容 Cell cell = new Cell(getChinese("金枪鱼"+"\n 货物运输单",BT));// cell.setUseAscender(true);//文本过多自动换行 cell.setHorizontalAlignment(Element.ALIGN_CENTER); //水平居中 cell.setVerticalAlignment(Element.ALIGN_MIDDLE); //垂直居中 cell.setColspan(8);//合并8个单元格,如果只有一个单元格可省略此行 table.addCell(cell); 就这样一个一个单元格添加.....
document.add(table); document.close(); 这样PDF就算是生成了
下载: PDF下载时会直接打开解决办法: 1.项目里有一个web.xml的文档 看看写没写入 <mime-mapping> <extension>pdf</extension> <mime-type>application/pdf</mime-type></mime-mapping> 2.打开你的Adobe acrobat 看看你的 编辑--->首选项--->种类--->因特网 图1 3.还不行的话,就是浏览器设置问题了,IE的话 工具-->管理加载项-->禁用Adobe 4.还不行的话...你也太惨了,跟我一样...如图2 5.还不行的话,哥们找到方法请告诉我... 6. 其他浏览器也有类似设置,因为我们只能用IE其他的就不是和了解了...
至于下载方法...网上很多,response或者window.location.href = path;都行