生成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. AC题目简解-数据结构

    A - Japan  POJ 3067 要两条路有交叉,(x1,y1)(x2,y2)那么需要满足:(x1-x2)*(y1-y2)<0判断出这是求逆序的问题 树状数组求逆序,先通过自定义的比较器实 ...

  2. Web缓存杂谈--Etag & If-None-Match

    一.概述 缓存通俗点,就是将已经得到的‘东东’存放在一个相对于自己而言,尽可能近的地方,以便下次需要时,不会再二笔地跑到起始点(很远的地方)去获取,而是就近解决,从而缩短时间和节约金钱(坐车要钱嘛). ...

  3. 【HDOJ】4351 Digital root

    digital root = n==0 ? 0 : n%9==0 ? 9:n%9;可以简单证明一下n = a0*n^0 + a1*n^1 + ... + ak * n^kn%9 = a0+a1+..+ ...

  4. Android开发UI之在子线程中更新UI

    转自第一行代码-Android Android是不允许在子线程中进行UI操作的.在子线程中去执行耗时操作,然后根据任务的执行结果来更新相应的UI控件,需要用到Android提供的异步消息处理机制. 代 ...

  5. git删除中文文件

    git中出现如下代码时,是因为文件中包含中文.而且我们也无法用 git rm name 命令来删除该文件. deleted: "chrome_plugin/source_file/iHub\ ...

  6. bzoj3156

    斜率优化dp,比较裸 注意int64的运算 ..] of int64;     i,n,h,t:longint;     x,y,z:int64; function g(j,k:int64):doub ...

  7. 各种兼容的placeholder

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  8. 【 D3.js 高级系列 — 1.0 】 文本的换行

    在 SVG 中添加文本是使用 text 元素.但是,这个元素不能够自动换行,超出的部分就显示不出来了,怎么办呢? 高级系列开篇前言 从今天开始写高级系列教程.还是那句话,由于本人实力有限,不一定保证入 ...

  9. php网页显示正方形图片缩略图

    需求是这样的:原始图片的大小是不定的,类似800*600.1000*756,现有一个页面要以正方形(60*60)显示这些图片,注意:图片只能在内存处理,不能缩小后保存到本地磁盘. 解决办法: html ...

  10. put a ContextMenu into the header of a TabPage z

    publicclassMyTabControl:TabControl { protected override void OnMouseUp(MouseEventArgs e){ if(e.Butto ...