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 ...
随机推荐
- 使用IntelliJ Idea创建Spring MVC项目
- NFC(6)NFC编程的几个重要类,NFC硬件启动android应用原理
用于NFC编程的几个重要类 Tag NFC 标签 NfcAdapter Nfc 的适配类 NdefMessage 描述NDEF格式的信息 NdefRecord 描述NDEF信息的一个信息段,类似tab ...
- NPOI的测试代码
NPOI\testcases\main\testcases vs10.csproj 需要注意,重新引用一下NPOI类库 需要注意的是,测试项目,使用了NUnit 找到测试项目下的SS文件夹,再定位到U ...
- IT项目量化管理:细化、量化与图形化 与 中国IT项目实施困惑
IT项目开发和实施的组织先后在组织中引入项目管理模型的管理制度.流程和方法,但收入甚微.大量的IT项目依然面临着无休止的需求蔓延与频繁加班.项目工期失控.质量低下等典型的项目失控现象.对项目引入量化意 ...
- I.MX6 U-boot GPIO hacking
/******************************************************************************* * I.MX6 U-boot GPIO ...
- erlang mnesia 数据库实现SQL查询
Mnesia是一个分布式数据库管理系统,适合于电信和其它需要持续运行和具备软实时特性的Erlang应用,越来越受关注和使用,但是目前Mnesia资料却不多,很多都只有官方的用户指南.下面的内容将着重说 ...
- java中的final关键字
谈到final关键字,想必很多人都不陌生,在使用匿名内部类的时候可能会经常用到final关键字.另外,Java中的String类就是一个final类,那么今天我们就来了解final这个关键字的用法.下 ...
- C# 创建WebServices及调用方法
发布WebServices 1.创建 ASP.NET Web 服务应用程序 SayHelloWebServices 这里需要说明一下,由于.NET Framework4.0取消了WebService ...
- 11、四大组件之二-Service高级(二)Native Service
一.Service的分类 1.1>Android Service 使用Java编写在JVM中运行的服务 1.2>Native Service 使用C/C++完成的服务,一般在系统开始时完成 ...
- 168. Excel Sheet Column Title
Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear i ...