java导出pdf
来源:互联网 发布:c语言中逗号运算符 编辑:程序博客网 时间:2024/05/17 22:16
java实现pdf导出,导出的内容是字符串,如果要导出数据库的数据,就需要获取内容,组成字符串,\n是换行,需要用到的jar包http://download.csdn.net/detail/xionglangs/9483586
代码实现
/**
*
*/
package com.cloudsun.draw.util;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
/**
* @author Administrator
*
*/
public class ExportPdf {
public static void main(String[] args) {
try {
String fileOne = "E:/新建文件夹", nameOne = "exportPdf.pdf", fileTwo = "E:/新建文件夹", nameTwo = "exportPdf.pdf";
writeToPdf(fileOne, nameOne);// 向fileOne地址生成一个名字为nameOne的pdf文件
exportToPdf(fileTwo, nameTwo);// 向fileTwo地址下一个名字为nameTwo的pdf文件写入内容
// 一般情况下fileOne就是fileTwo,nameOne就是nameTwo
} catch (Exception e) {
e.printStackTrace();
}
}
public static void writeToPdf(String fileOne, String nameOne) {
try {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);// 新建document对象,参数意义第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距
PdfWriter.getInstance(document, new FileOutputStream(fileOne + "/"
+ nameOne));// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中,
// 创建 PdfWriter 对象
// 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径
document.open();// 打开文档
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph(
"Some more text on the first page with different color and font type.",
FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD,
new Color(255, 150, 200))));// 向文档中添加内容,通过
// com.lowagie.text.Paragraph
// 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
// 打出的东西是字符串,如果是数据库的可以把获得的数据转换为字符串
} catch (Exception e) {
e.printStackTrace();
}
}
public static void exportToPdf(String fileTwo, String nameTwo) {
try {
// 新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
PdfWriter.getInstance(document, new FileOutputStream(fileTwo + "/"
+ nameTwo));
document.open();// 打开文件
// 标题
document.addTitle("Hello mingri example");
// 作者
document.addAuthor("wolf");
// 主题
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello mingri");
document.addCreator("My program using iText");
// document.newPage();
// 向文档中添加内容
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("\n"));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph(
"Some more text on the first page with different color and font type.",
FontFactory.getFont(FontFactory.defaultEncoding, 10,
Font.BOLD, new Color(0, 0, 0))));
Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(
FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0,
255)));
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD,
new Color(255, 0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph(
"This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
document.add(chapter1);
// 关闭文档
document.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
- JAVA 导出PDF文件
- Java实现Pdf导出
- java导出pdf
- java导出pdf
- java PDF文件导出
- java 导出PDF
- java导出pdf
- java 导出pdf
- java导出PDF文件
- java导出pdf
- Java导出PDF文件
- Java导出Pdf范例
- java导出pdf文档
- java导出pdf
- java导出pdf
- java导出pdf
- java导出pdf
- Java导出pdf表格
- ajax异步提交以及nicevalidator自定义验证示例
- python3学习笔记1--TypeError: 'dict' object is not callable的解决
- 如何在TabBar上自定义添加个性按钮
- opencv环境在QT的配置
- iOS图片裁剪和小图看大图动画
- java导出pdf
- CDMA2000 3G Chat 拨号
- 实现简单的队和栈结构,附代码,图
- HDU 2693 Bone Collector II
- win32下安装mingw32和cmake来编译opencv2.4.9
- 機器學習基石(Machine Learning Foundations) 机器学习基石 作业三 Q13-15 C++实现
- WebRequest请求范例
- Hibernate 下载、安装和使用
- Docker初学