import java.awt.Color;
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class StreamDemo_15 {
// 生成PDF文件中的内容
public void creatPDF(String fileName) {
File file = new File(fileName);
FileOutputStream out = null;
try {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);// 实例化文档对象
out = new FileOutputStream(file);
PdfWriter writer = PdfWriter.getInstance(document, out);// 创建写入器
document.open();// 打开文档准备写入内容
// 设置可以在PDF中输入汉字的字体
BaseFont bfChinese = BaseFont.createFont(
"C:\\WINDOWS\\Fonts\\SIMHEI.TTF", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
Font font = new Font(bfChinese, 16, Font.BOLD);
font.setColor(0, 0, 255);
Paragraph paragraph1 = new Paragraph("第7章 IO——输入输出流", font);// 创建段落对象
// 创建了一个章节对象,标题为"第7章 IO——输入输出流"
Chapter chapter1 = new Chapter(paragraph1, 0);
// 将编号级别设为 0 就不会在页面上显示章节编号
chapter1.setNumberDepth(0);
Font font1 = new Font(bfChinese, 14, Font.BOLD);
font1.setColor(255, 0, 0);
Paragraph section1_title1 = new Paragraph("7.1 什么是对象序列化?", font1);// 创建小节对象的标题
Section section1 = chapter1.addSection(section1_title1);// 创建小节对象
Font font2 = new Font(bfChinese, 12, Font.NORMAL);
font2.setColor(0, 0, 0);
Paragraph text = new Paragraph("什么是对象序列化呢?简单的说,就是将对象写入流,\n"
+ "而序列化解体则指从流中获取数据后,重构对象的过程。\n"
+ "Java的对象可以分为可序列化对象和不可序列化对象,\n"
+ "从说明文档中,可以看到只有实现了\"Serializable\"接口的对象才是可序列化对象。", font2);// 创建一个段落
section1.add(text);// 将段落写入小节中
text = new Paragraph("File类主要方法列表", font2);
section1.add(text);
Table table = new Table(2, 5); // 创建表格对象
table.setBorderColor(new Color(220, 255, 100)); // 设置表格边框颜色
// 设置单元格的边距间隔等
table.setPadding(1);
table.setSpacing(1);
table.setBorderWidth(1);
Cell cell = null; // 单元格对象
// 添加表头信息
cell = new Cell(new Paragraph("方法名称", font2));
cell.setHeader(true);
table.addCell(cell);
cell = new Cell(new Paragraph("用法", font2));
cell.setHeader(true);
table.addCell(cell);
table.endHeaders();
// 添加表的内容
table.addCell(new Cell("public Boolean canread()"));
table.addCell(new Cell(new Paragraph("测试这个文件是否可以读?", font2)));
table.addCell(new Cell("public Boolean canwrite()"));
table.addCell(new Cell(new Paragraph("测试这个文件是否可写?", font2)));
table.addCell(new Cell("public Boolean createNewFile()"));
table.addCell(new Cell(new Paragraph(
"看这个文件或目录是否存在,如有此文件则返回false,如果没有这个文件则创建这个类的对象.", font2)));
table.addCell(new Cell("public Boolean delete()"));
table.addCell(new Cell(new Paragraph(
"删除当前对象所指文件。删除成功返回true,否则返回false.", font2)));
section1.add(table); // 将表格对象添加到小节对象中
List list = new List(true, false, 20);// 创建列表
ListItem item = new ListItem("带缓存的字符输出流BufferedWriter类;", font2);// 创建列表项
list.add(item);// 将列表项添加到列表中
item = new ListItem("字符输入流FileReader类和FileWriter类的使用;", font2);
list.add(item);
item = new ListItem("读取带缓存的BufferedReader字符流t.", font2);
list.add(item);
section1.add(list); // 将列表对象添加到小节对象中
// 将章节对象加入到文档中
document.add(chapter1);
// 关闭文档
document.close();
System.out.println("PDF文件生成成功,PDF文件名:" + file.getAbsolutePath());
} catch (DocumentException e) {
System.out.println("PDF文件" + file.getAbsolutePath() + "生成失败!" + e);
e.printStackTrace();
} catch (IOException ee) {
System.out.println("PDF文件" + file.getAbsolutePath() + "生成失败!" + ee);
ee.printStackTrace();
} finally {
if (out != null) {
try {
// 关闭输出文件流
out.close();
} catch (IOException e1) {
}
}
}
}
public static void main(String[] args) {
StreamDemo_15 pdf = new StreamDemo_15();
String fileName = "D:/temp/myPDF.pdf";
pdf.creatPDF(fileName);
}
}
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class StreamDemo_15 {
// 生成PDF文件中的内容
public void creatPDF(String fileName) {
File file = new File(fileName);
FileOutputStream out = null;
try {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);// 实例化文档对象
out = new FileOutputStream(file);
PdfWriter writer = PdfWriter.getInstance(document, out);// 创建写入器
document.open();// 打开文档准备写入内容
// 设置可以在PDF中输入汉字的字体
BaseFont bfChinese = BaseFont.createFont(
"C:\\WINDOWS\\Fonts\\SIMHEI.TTF", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
Font font = new Font(bfChinese, 16, Font.BOLD);
font.setColor(0, 0, 255);
Paragraph paragraph1 = new Paragraph("第7章 IO——输入输出流", font);// 创建段落对象
// 创建了一个章节对象,标题为"第7章 IO——输入输出流"
Chapter chapter1 = new Chapter(paragraph1, 0);
// 将编号级别设为 0 就不会在页面上显示章节编号
chapter1.setNumberDepth(0);
Font font1 = new Font(bfChinese, 14, Font.BOLD);
font1.setColor(255, 0, 0);
Paragraph section1_title1 = new Paragraph("7.1 什么是对象序列化?", font1);// 创建小节对象的标题
Section section1 = chapter1.addSection(section1_title1);// 创建小节对象
Font font2 = new Font(bfChinese, 12, Font.NORMAL);
font2.setColor(0, 0, 0);
Paragraph text = new Paragraph("什么是对象序列化呢?简单的说,就是将对象写入流,\n"
+ "而序列化解体则指从流中获取数据后,重构对象的过程。\n"
+ "Java的对象可以分为可序列化对象和不可序列化对象,\n"
+ "从说明文档中,可以看到只有实现了\"Serializable\"接口的对象才是可序列化对象。", font2);// 创建一个段落
section1.add(text);// 将段落写入小节中
text = new Paragraph("File类主要方法列表", font2);
section1.add(text);
Table table = new Table(2, 5); // 创建表格对象
table.setBorderColor(new Color(220, 255, 100)); // 设置表格边框颜色
// 设置单元格的边距间隔等
table.setPadding(1);
table.setSpacing(1);
table.setBorderWidth(1);
Cell cell = null; // 单元格对象
// 添加表头信息
cell = new Cell(new Paragraph("方法名称", font2));
cell.setHeader(true);
table.addCell(cell);
cell = new Cell(new Paragraph("用法", font2));
cell.setHeader(true);
table.addCell(cell);
table.endHeaders();
// 添加表的内容
table.addCell(new Cell("public Boolean canread()"));
table.addCell(new Cell(new Paragraph("测试这个文件是否可以读?", font2)));
table.addCell(new Cell("public Boolean canwrite()"));
table.addCell(new Cell(new Paragraph("测试这个文件是否可写?", font2)));
table.addCell(new Cell("public Boolean createNewFile()"));
table.addCell(new Cell(new Paragraph(
"看这个文件或目录是否存在,如有此文件则返回false,如果没有这个文件则创建这个类的对象.", font2)));
table.addCell(new Cell("public Boolean delete()"));
table.addCell(new Cell(new Paragraph(
"删除当前对象所指文件。删除成功返回true,否则返回false.", font2)));
section1.add(table); // 将表格对象添加到小节对象中
List list = new List(true, false, 20);// 创建列表
ListItem item = new ListItem("带缓存的字符输出流BufferedWriter类;", font2);// 创建列表项
list.add(item);// 将列表项添加到列表中
item = new ListItem("字符输入流FileReader类和FileWriter类的使用;", font2);
list.add(item);
item = new ListItem("读取带缓存的BufferedReader字符流t.", font2);
list.add(item);
section1.add(list); // 将列表对象添加到小节对象中
// 将章节对象加入到文档中
document.add(chapter1);
// 关闭文档
document.close();
System.out.println("PDF文件生成成功,PDF文件名:" + file.getAbsolutePath());
} catch (DocumentException e) {
System.out.println("PDF文件" + file.getAbsolutePath() + "生成失败!" + e);
e.printStackTrace();
} catch (IOException ee) {
System.out.println("PDF文件" + file.getAbsolutePath() + "生成失败!" + ee);
ee.printStackTrace();
} finally {
if (out != null) {
try {
// 关闭输出文件流
out.close();
} catch (IOException e1) {
}
}
}
}
public static void main(String[] args) {
StreamDemo_15 pdf = new StreamDemo_15();
String fileName = "D:/temp/myPDF.pdf";
pdf.creatPDF(fileName);
}
}