JAVA生成PDF文件
生成PDF文件是主要应用的是ITEXT插件
import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter; public class MySelf { /**
* @param args
* @throws IOException
* @throws DocumentException
*/
public static void main(String[] args) throws DocumentException, IOException {
// TODO Auto-generated method stub
float head= (float)50.0;
float lineHeight1 = (float)50.0;
float lineHeight2 = (float)50.0;
float lineHeight3 = (float)51.0;
//创建文档,设置页面大小, 左、右、上和下页边距。
Document document = new Document(PageSize.A4, 10, 10, 50, 50);
//处理中文显示问题,使用计算机自带字体
BaseFont bfChinese=BaseFont.createFont("c://windows//fonts//simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//方法二:使用iTextAsian.jar中的字体
//BaseFont baseFont = BaseFont.createFont("STSong-Light",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); //方法三:使用资源字体(ClassPath)
////BaseFont baseFont = BaseFont.createFont("/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); Font headfont=new Font(bfChinese,10,Font.BOLD);//设置字体大小 样式
Font keyfont=new Font(bfChinese,12,Font.BOLD);//文字加粗
Font title=new Font(bfChinese,18,Font.BOLD);//文字加粗
Font textfont=new Font(bfChinese,16,Font.NORMAL);//正常文字 //document是创建的文档,FileOutputStream是向文档中输入
PdfWriter.getInstance(document, new FileOutputStream(new File("D:\\MySelf.pdf")));
//打开文档
document.open();
//开始生成一个2列的表格
PdfPTable table=new PdfPTable(2);
PdfPTable table1=new PdfPTable(6);
//定义每个单元格的宽度
float[] widthsHeader={20f,5f};
float[] widthsHeade1={3f,3f,3f,4f,3f,6f};
//设置表格每一各的宽度
table.setWidths(widthsHeader);
table1.setWidths(widthsHeade1);
//设置边距
//设置单元格间距
//table.setSpacing(1f);
//设置表格的总体宽度
table.setWidthPercentage(100); //如果表格格式一样采用遍历的方式读取添加
/*List list=new ArrayList();
list.add("姓名");
list.add("姓名");
list.add("姓名");
list.add("姓名");
list.add("姓名");
list.add("姓名");
list.add("姓名");
for(int i=0;i<list.size();i++){
Cell cell=null;
cell=new Cell(new Paragraph((String) list.get(i),keyfont));
//设置单元格背景颜色
cell.setBackgroundColor(Color.lightGray);
//设置水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//设置垂直居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell); }
document.add(table);
document.close();
System.out.println("表格创建成功");
return;*/ //单元格对象
PdfPCell cell=null;
//设置单元格内容
cell=new PdfPCell(new Paragraph("文档标题栏",title));
//合并单元格列
cell.setColspan(2);
//设置垂直居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//设置水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setFixedHeight(head);
//将单元格内容添加到表格中去
table.addCell(cell); cell=new PdfPCell(new Paragraph("A",keyfont));
//设置单元格背景颜色
cell.setBackgroundColor(Color.lightGray);
//设置水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//设置垂直居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight1);
table1.addCell(cell); cell=new PdfPCell(new Paragraph("B",keyfont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight1);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("C",keyfont));
cell.setBackgroundColor(Color.lightGray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight1);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("D",keyfont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight1);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("E",keyfont));
cell.setBackgroundColor(Color.lightGray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight1);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("F",keyfont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight1);
table1.addCell(cell); cell=new PdfPCell(new Paragraph("G",keyfont));
cell.setBackgroundColor(Color.lightGray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight2);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("H",keyfont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight2);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("I",keyfont));
cell.setBackgroundColor(Color.lightGray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight2);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("J",keyfont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight2);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("K",keyfont));
cell.setBackgroundColor(Color.lightGray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight2);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("L",keyfont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight2);
table1.addCell(cell); cell=new PdfPCell(new Paragraph("M",keyfont));
cell.setBackgroundColor(Color.lightGray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight3);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("N",keyfont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight3);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("O",keyfont));
cell.setBackgroundColor(Color.lightGray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight3);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("P",keyfont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight3);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("Q",keyfont));
cell.setBackgroundColor(Color.lightGray);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight3);
table1.addCell(cell);
cell=new PdfPCell(new Paragraph("R",keyfont));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight3);
table1.addCell(cell); PdfPCell pdfpcell=new PdfPCell(table1);
pdfpcell.setPadding(0);
table.addCell(pdfpcell);
//加载图片添加到指定位置
Image image = Image.getInstance ("C:/Users/admin/Desktop/日志文件/IMG_0696.JPG");
float height=image.height();
float width=image.width();
System.out.println(height);
System.out.println(width); cell=new PdfPCell(image);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
cell.setFixedHeight(lineHeight1+lineHeight2+lineHeight3);
table.addCell(cell); document.add(table);
document.close(); } }
生成PDF代码
JAVA生成PDF文件的更多相关文章
- Java生成PDF文件(转)
原文地址:https://www.cnblogs.com/shuilangyizu/p/5760928.html 一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iT ...
- [itext]Java生成PDF文件
一.前言 最近在做也导出试卷的功能,刚开始是导出为doc,可是导出来格式都有变化,最后说直接将word转为pdf,可是各种不稳定,各种报错.最后想到直接将文件写入pdf(参考:http://www.c ...
- java生成PDF文件(itext)
itextpdf-5.4.3.jar下载地址: http://www.kuaipan.cn/file/id_58980483773788178.htm 导入itextpdf-5.4.3.jar ToP ...
- java生成pdf文件 --- Table
Java利用itext实现导出PDF文件 所需要的jar包:com.lowagie.text_2.1.7.v201004222200.jar jar包下载地址:http://cn.jarfire.or ...
- 【文件】java生成PDF文件
package test; import java.awt.Color; import java.io.FileOutputStream; import org.junit.Test; import ...
- java调用wkhtmltopdf生成pdf文件,美观,省事
最近项目需要导出企业风险报告,文件格式为pdf,于是搜了一大批文章都是什么Jasper Report,iText ,flying sauser ,都尝试了一遍,感觉不是我想要的效果, 需要自己调整好多 ...
- Java 生成pdf表格文档
最近在工作做一个泰国的项目,应供应商要求,需要将每天的交易生成pdf格式的报表上传到供应商的服务器,特此记录实现方法.废话不多说,直接上代码: THSarabunNew.ttf该文件是泰国字体自行网上 ...
- Java 动态生成 PDF 文件
每片文章前来首小诗: 今日夕阳伴薄雾,印着雪墙笑开颜.我心仿佛出窗前,浮在半腰望西天. --泥沙砖瓦浆木匠 需求: 项目里面有需要java动态生成 PDF 文件,提供下载.今天我找了下有关了,系 ...
- Java Itext 生成PDF文件
利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...
随机推荐
- MSSQLServer基础05(联合查询,连接查询)
联合结果集union(集合运算符) 集合运算符是对两个集合操作的,两个集合必须具有相同的列数,列具有相同的数据类型(至少能隐式转换的),最终输出的集合的列名由第一个集合的列名来确定.(可以用来连接多个 ...
- [译]Atomic VS. Non-Atomic 操作
原文链接:atomic-vs-non-atomic-operations 在网上已经写了很多关于原子操作的文章,但是通常都集中在原子的读-修改-写(RMW. read-modify-write)操作. ...
- allegro添加多个过孔
place--Via arrays matrix: [数] 矩阵:模型:[生物][地质] 基质:母体:子宫:[地质] 脉石 boundary:边界:范围:分界线 unplaced : adj. 未受到 ...
- JavaScript DOM高级程序设计 4.3控制事件流和注册事件侦听器--我要坚持到底!
一.事件流 我们通过下面一个实例,进行说明. <body> <h1>Event Flow</h1> <ul id="nav"> &l ...
- Git教程(10)git比较复杂的功能
1,只拣选某分支中的一个提交,然后把它合并到当前分支 $ git cherry-pick e43a6fd3e94888d76779ad79fb568ed180e5fcdf 2,Rerere 它是一种重 ...
- Git教程(2)官方命令文档及常用命令表
http://www.cnblogs.com/angeldevil/archive/2013/11/26/3238470.html 1,官方命令文档 http://www.git-scm.com/do ...
- Android开发之parseSdkContent failed Could not initialize class android.graphics.Typeface
在进行android开发过程中,忽然发现经常弹出来parseSdkContent failed 这个错误,然后google了下解决办法 方法1: 删除.android文件 重启eclipse. 该方法 ...
- MarkupExtension的使用
记得第一次看到MarkupExtension是在几年前的一次面试中.很不好意思说,当时不知道是什么东东.最近在项目中又用到了WPF的这个功能,决定在博客里记录一下. 在Xaml中为某个对象以Attri ...
- UML中关系图解
转自http://blog.csdn.net/duran1986/article/details/5573415 最近在教软件工程项目实践,就又仔细了解了下UML中各种关系的意义,虽然有点简单,但是有 ...
- 武汉北大青鸟解读2016年10大IT热门岗位
武汉北大青鸟解读2016年10大IT热门岗位 2016年1月5日 13:37 北大青鸟 这是IT从业者的辉煌时代,IT行业的失业率正处在历史的低点,而且有的岗位——例如网络和安全工程师以及软件开发人员 ...