生成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代码

04-25 13:28