生成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文件的更多相关文章

  1. Java生成PDF文件(转)

    原文地址:https://www.cnblogs.com/shuilangyizu/p/5760928.html 一.前言 前几天,做ASN条码收货模块,需要实现打印下载收货报表,经一番查找,选定iT ...

  2. [itext]Java生成PDF文件

    一.前言 最近在做也导出试卷的功能,刚开始是导出为doc,可是导出来格式都有变化,最后说直接将word转为pdf,可是各种不稳定,各种报错.最后想到直接将文件写入pdf(参考:http://www.c ...

  3. java生成PDF文件(itext)

    itextpdf-5.4.3.jar下载地址: http://www.kuaipan.cn/file/id_58980483773788178.htm 导入itextpdf-5.4.3.jar ToP ...

  4. java生成pdf文件 --- Table

    Java利用itext实现导出PDF文件 所需要的jar包:com.lowagie.text_2.1.7.v201004222200.jar jar包下载地址:http://cn.jarfire.or ...

  5. 【文件】java生成PDF文件

    package test; import java.awt.Color; import java.io.FileOutputStream; import org.junit.Test; import ...

  6. java调用wkhtmltopdf生成pdf文件,美观,省事

    最近项目需要导出企业风险报告,文件格式为pdf,于是搜了一大批文章都是什么Jasper Report,iText ,flying sauser ,都尝试了一遍,感觉不是我想要的效果, 需要自己调整好多 ...

  7. Java 生成pdf表格文档

    最近在工作做一个泰国的项目,应供应商要求,需要将每天的交易生成pdf格式的报表上传到供应商的服务器,特此记录实现方法.废话不多说,直接上代码: THSarabunNew.ttf该文件是泰国字体自行网上 ...

  8. Java 动态生成 PDF 文件

    每片文章前来首小诗:   今日夕阳伴薄雾,印着雪墙笑开颜.我心仿佛出窗前,浮在半腰望西天.  --泥沙砖瓦浆木匠 需求: 项目里面有需要java动态生成 PDF 文件,提供下载.今天我找了下有关了,系 ...

  9. Java Itext 生成PDF文件

    利用Java Itext生成PDF文件并导出,实现效果如下: PDFUtil.java package com.jeeplus.modules.order.util; import java.io.O ...

随机推荐

  1. 创业公司Playcafe关门大吉 创始人总结10大失败教训

    导读:互联网电视游戏网站PlayCafe的创始人马克·高登森(Mark Goldenson)日前撰文,总结了自己创业失败的十个教训.以下为文章主要内容: 一年半前,我与公司联合创始人戴维·奈格(Dev ...

  2. ArrayList源代码深入剖析

    第1部分 ArrayList介绍ArrayList底层采用数组实现,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAccess, Cloneable, java. ...

  3. Ubuntu 学习笔记

    1.   ubuntu开启root账号,设置分配很简单,只要为root设置一个root密码就行了: $ sudo passwd root 之后会提示要输入root用户的密码,连续输入root密码,再使 ...

  4. Android sd卡读取数据库

    先在 Manifest 里添加权限 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE&q ...

  5. itoa函数的实现(不同进制)

    2013-07-08 17:12:30 itoa函数相对于atoi函数,比较简单,还是要注意考虑的全面. 小结: 一下几点需要考虑: 对负数,要加上负号: 考虑不同进制,根据要求进行处理:对不同的进制 ...

  6. 【Codeforces】#345 Div1

    1. Watchmen1.1 题目描述给$n$个点,求曼哈顿距离等于欧式距离的点对数. 1.2 基本思路由$|x_i-x_j|+|y_i-yj| = \sqrt{(x_i-x_j)^2+(y_i-yj ...

  7. JPA详解

    2006 年 5 月 2 日,EJB 3.0 规范最终版由 JCP(Java Community Process) 正式公布,标准号为 JSR(Java Specification Request)2 ...

  8. bzoj3294

    感觉自己就是不怎么擅长计数的问题 设f[k,i,j]表示前k种颜色占据了i行j列的方案 g[k,i,j]表示第k种颜色占据了i行j列的方案,注意要减去并没占据满i行j列的情况 然后转移就很好写了 像这 ...

  9. Windows Server 2008文件同步

    配置Windows Server 2008文件同步   摘要: 众所周知,Linux系统可以用rsync来实现文件或目录的同步,windows系统下也一样可以.我们现在就用cwRsync来实现wind ...

  10. C# 有关文件路径的操作

    1. 由文件全路径,获取文件扩展名.文件名等信息 string fullPath = @"\WebSite1\Default.aspx"; string filename = Sy ...