- 浏览: 28713 次
- 性别:
- 来自: 北京
最新评论
-
wangdgsc:
你好,我能不能向你请教一个问题,我刚用xfire,但是却出了s ...
XFire中实现WS-Security时出现的异常及原因 -
larva:
这个与CAS有什么关系?
SSO CAS Srpring MVC -
melody404:
不错,很有质量 希望楼主经常总结
SSO CAS Srpring MVC -
jelver:
很不错,收藏了,
SSO CAS Srpring MVC
1。itext是用于把数据写到pdf中!他需要两个jar包:iTextAsian和iText
2。具体代码
package com.jw.text;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.awt.Color;
import java.awt.Point;
import java.io.*;
import java.net.MalformedURLException;
import sun.management.FileSystem;
import com.jw.dao.BookDAO;
import com.jw.po.Books;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.Watermark;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class Text {
private Document document = null;
// 第一种简单的文本
public void create1() {
Rectangle pageSize = new Rectangle(144, 720);
pageSize.setBackgroundColor(new Color(0xFF, 0xFF, 0xDE));
try {
document = new Document(pageSize);
PdfWriter.getInstance(document, new FileOutputStream("d:\\1.pdf"));
document.open();
// 横向输出
document.add(new Paragraph(
"sdfsdfsdfsdfsddsfsdfsdsdfsdfsdsdfsdfasfsdfhelloworld"));
} catch (Exception e) {
e.printStackTrace();
}
document.close();
}
// 第2种简单的文本
public void create2() {
document = new Document(PageSize.A4.rotate());
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0103.pdf"));
document.open();
for (int i = 0; i < 20; i++) {
// 竖向输出
document
.add(new Phrase(
"Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea,
Hello Land, Hello People. "));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// 第3种简单的文本
public void create3() {
document = new Document(PageSize.A5, 36, 72, 108, 180);
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0104.pdf"));
document.open();
Paragraph paragraph = new Paragraph();
paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
for (int i = 0; i < 20; i++) {
paragraph
.add("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land,
Hello People. ");
}
Date date = new Date();
SimpleDateFormat form = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
String mytime = form.format(date);
paragraph.add(mytime);
document.add(paragraph);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// 第4种,设置文件的头和标题等......
public void create4() {
document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0106.pdf"));
document.addTitle("Hello World example");
document.addSubject("This example explains step 6 in Chapter 1");
document.addKeywords("Metadata, iText, step 6, tutorial");
document.addCreator("My program using iText#");
document.addAuthor("Bruno Lowagie");
document.addHeader("Expires", "0");
document.open();
document.add(new Paragraph(
"Hello World sdfgds f sd fsd f sd fsd f sd f sd fs "));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// 第5种,向pdf中打印水印图片
public void create5() {
document = new Document();
try {
String url = System.getProperty("user.dir").toString() + "\\a.jpg";
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0107.pdf"));
// 放入图片并定坐标,以及图片的路径
Watermark watermark = new Watermark(Image.getInstance(url), 200,
420);
document.add(watermark);
// 设置头信息
HeaderFooter header = new HeaderFooter(new Phrase(
"This is a header"), false);
document.setHeader(header);
document.open();
document.setPageSize(PageSize.A4.rotate());
// Watermark watermark2 = new Watermark(Image.getInstance(url), 320,
// 200);
// document.add(watermark2);
// 设置结尾
HeaderFooter footer = new HeaderFooter(
new Phrase("This is page: "), true);
document.setFooter(footer);
// 设置内容
document.add(new Paragraph("Hello World"));
// 创建第2的页面
document.newPage();
document.add(new Paragraph("Hello Earth"));
document.resetHeader();
// 创建第3的页面
document.newPage();
document.add(new Paragraph("Hello Sun"));
document.add(new Paragraph("Remark: the header has vanished!"));
document.resetPageCount();
// 创建第4的页面
document.newPage();
document.add(new Paragraph("Hello Sun"));
document.add(new Paragraph("Remark: the header has vanished!"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// 第6种设置文本的布局
public void create6() {
String url = System.getProperty("user.dir").toString() + "\\a.jpg";
document = new Document();
try {
PdfWriter writerA = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0108a.pdf"));
// 设置布局
writerA.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);
PdfWriter writerB = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0108b.pdf"));
// 设置布局
writerB.setViewerPreferences(PdfWriter.HideMenubar
| PdfWriter.HideToolbar);
PdfWriter writerC = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0108c.pdf"));
// 设置布局
writerC.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft
| PdfWriter.PageModeFullScreen
| PdfWriter.NonFullScreenPageModeUseThumbs);
Watermark watermark = new Watermark(Image.getInstance(url), 200,
420);
document.add(watermark);
HeaderFooter header = new HeaderFooter(new Phrase(
"This is a header"), false);
document.setHeader(header);
document.open();
document.setPageSize(PageSize.A4.rotate());
HeaderFooter footer = new HeaderFooter(
new Phrase("This is page: "), true);
document.setFooter(footer);
document.add(new Paragraph("Hello World"));
document.newPage();
document.add(new Paragraph("Hello Earth"));
document.resetHeader();
document.newPage();
document.add(new Paragraph("Hello Sun"));
document.add(new Paragraph("Remark: the header has vanished!"));
document.resetPageCount();
document.newPage();
document.add(new Paragraph("Hello Moon"));
document
.add(new Paragraph("Remark: the pagenumber has been reset!"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// 第7种---------------
public void create7() {
try {
document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0109.pdf"));
// setEncryption方法中可以设置如下内容(这样打开pdf时需要输入口令!!!!!!!!!!!)
// PdfWriter.STRENGTH128BITS, "userpass", "ownerpass",
// PdfWriter.AllowCopy | PdfWriter.AllowPrinting
writer.setEncryption(PdfWriter.STRENGTH40BITS, "", "",
PdfWriter.AllowCopy);
document.open();
document.add(new Paragraph("This document is Top Secret!"));
document.open();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// 第8种
public void create8() {
String url = System.getProperty("user.dir").toString() + "\\a.jpg";
document = new Document();
try {
PdfWriter writerA = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0111a.pdf"));
PdfWriter writerB = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0111b.pdf"));
writerB.pause();
Watermark watermark = new Watermark(Image.getInstance(url), 200,
420);
document.add(watermark);
writerB.resume();
HeaderFooter header = new HeaderFooter(new Phrase(
"This is a header"), false);
document.setHeader(header);
document.open();
document.setPageSize(PageSize.A4.rotate());
HeaderFooter footer = new HeaderFooter(
new Phrase("This is page: "), true);
document.setFooter(footer);
document.add(new Paragraph("Hello World"));
document.newPage();
document.add(new Paragraph("Hello Earth"));
writerA.pause();
document.resetHeader();
writerA.resume();
document.newPage();
document.add(new Paragraph("Hello Sun"));
writerA.pause();
document.add(new Paragraph("Remark: the header has vanished!"));
writerA.resume();
writerB.pause();
document.resetPageCount();
writerB.resume();
document.newPage();
document.add(new Paragraph("Hello Moon"));
writerB.pause();
document
.add(new Paragraph("Remark: the pagenumber has been reset!"));
writerB.resume();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// ----------------------------第10种设置字体的样式和颜色--------------------------------------
public void create10() {
try {
document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0202.pdf"));
document.open();
Phrase phrase0 = new Phrase();
Phrase phrase1 = new Phrase("(1) this is a phrase\n");
Phrase phrase2 = new Phrase(
24,
"(2) this is a phrase with leading 24. You can only see the difference if the line is
long enough. Do you see it? There is more space between this line and the previous one.\n");
Phrase phrase3 = new Phrase(
"(3) this is a phrase with a red, normal font Courier, size 20. As you can see the
leading is automatically changed.\n",
FontFactory.getFont(FontFactory.COURIER, 20, Font.NORMAL,
new Color(255, 0, 0)));
Phrase phrase4 = new Phrase(new Chunk("(4) this is a phrase\n"));
Phrase phrase5 = new Phrase(
18,
new Chunk(
"(5) this is a phrase in Helvetica, bold, red and size 16 with a
given leading of 18 points.\n",
FontFactory.getFont(FontFactory.HELVETICA, 16,
Font.BOLD, new Color(255, 0, 0))));
Phrase phrase6 = new Phrase("(6)");
Chunk chunk = new Chunk(" This is a font: ");
phrase6.add(chunk);
phrase6.add(new Chunk("Helvetica", FontFactory.getFont(
FontFactory.HELVETICA, 12)));
phrase6.add(chunk);
phrase6.add(new Chunk("Times New Roman", FontFactory.getFont(
FontFactory.TIMES_NEW_ROMAN, 12)));
phrase6.add(chunk);
phrase6.add(new Chunk("Courier", FontFactory.getFont(
FontFactory.COURIER, 12)));
phrase6.add(chunk);
phrase6.add(new Chunk("Symbol", FontFactory.getFont(
FontFactory.SYMBOL, 12)));
phrase6.add(chunk);
phrase6.add(new Chunk("ZapfDingBats", FontFactory.getFont(
FontFactory.ZAPFDINGBATS, 12)));
phrase6.add("\n");
Phrase phrase7 = new Phrase(
"(7) if you don't Add a newline yourself, all phrases are glued to eachother!");
document.add(phrase1);
document.add(phrase2);
document.add(phrase3);
document.add(phrase4);
document.add(phrase5);
document.add(phrase6);
document.add(phrase7);
} catch (Exception e) {
e.printStackTrace();
}
document.close();
}
// -----------------------设置字体的样式--------------------------------
public void create11() {
document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0203.pdf"));
document.open();
document.add(new Phrase("What is the " + (char) 945
+ "-coefficient of the " + (char) 946 + "-factor in the "
+ (char) 947 + "-equation?\n"));
int i = 0;
for (i = 913; i < 970; i++) {
document.add(new Phrase(" " + i + ": " + (char) i));
}
document.add(new Phrase(
"-----------------------------------------\n"));
List l = new ArrayList();
l.add("aaaaaaaaaaaaa");
l.add("bbbbbbbbbbbbbbbbb");
document.add(new Phrase(16, "\n\n\n"));
document
.add(new Phrase(
-16,
"Hello, this is a very long phrase to show you the somewhat odd
effect of a negative leading. You can write from bottom to top. This is not fully supported. It's something between a feature
and a bug."));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// -------------------------------------创建表格---------------------------------------------
public void createtable() {
document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0501.pdf"));
document.open();
// 2 行,2列
Table aTable = new Table(4, 4);
aTable.setBorder(1);
// 第一行的列值
aTable.addCell("2.2");
aTable.addCell("3.3");
aTable.addCell("2.1");
aTable.addCell("1.3");
// 第2行的列值
aTable.addCell("2.2");
aTable.addCell("3.3");
aTable.addCell("2.1");
aTable.addCell("1.3");
document.add(aTable);
// 在下一页中建表格
document.newPage();
aTable = new Table(4, 4); // 4 rows, 4 columns
aTable.addCell("2.2");
aTable.addCell("3.3");
aTable.addCell("2.1");
aTable.addCell("1.3");
document.add(aTable);
// ------------------------下一页是从数据库中查询的数据显示到表格中--------------------------
document.newPage();
BookDAO b = new BookDAO();
List l = b.findbookall();
Table mytable = new Table(6, l.size());//6是列数,l.size()得到的是行数
// 设置格子的颜色
mytable.setDefaultCellBorderColor(Color.blue);
mytable.setBorderColor(Color.blue);
mytable.setCellpadding(0);
// 解决中文问题-------------
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 文本内容
Font FonttextChinese = new Font(bfChinese, 12, Font.NORMAL);
// 表头
Font FontheadChinese = new Font(bfChinese, 15, Font.BOLD);
// 设置表头
Cell h1 = new Cell(new Phrase("编号", FontheadChinese));
h1.setHorizontalAlignment(Element.ALIGN_CENTER);
h1.setBackgroundColor(Color.lightGray);
Cell h2 = new Cell(new Phrase("书籍名称", FontheadChinese));
h2.setHorizontalAlignment(Element.ALIGN_CENTER);
h2.setBackgroundColor(Color.lightGray);
Cell h3 = new Cell(new Phrase("价格", FontheadChinese));
h3.setHorizontalAlignment(Element.ALIGN_CENTER);
h3.setBackgroundColor(Color.lightGray);
Cell h4 = new Cell(new Phrase("开始时间", FontheadChinese));
h4.setHorizontalAlignment(Element.ALIGN_CENTER);
h4.setBackgroundColor(Color.lightGray);
Cell h5 = new Cell(new Phrase("结束时间", FontheadChinese));
h5.setHorizontalAlignment(Element.ALIGN_CENTER);
h5.setBackgroundColor(Color.lightGray);
Cell h6 = new Cell(new Phrase("书刊号", FontheadChinese));
h6.setHorizontalAlignment(Element.ALIGN_CENTER);
h6.setBackgroundColor(Color.lightGray);
mytable.addCell(h1);
mytable.addCell(h2);
mytable.addCell(h3);
mytable.addCell(h4);
mytable.addCell(h5);
mytable.addCell(h6);
for (int i = 0; i < l.size(); i++) {
Books book = (Books) l.get(i);
Cell cell0 = new Cell(new Phrase(String.valueOf(book.getBid()),
FonttextChinese));
cell0.setHorizontalAlignment(Element.ALIGN_CENTER);
Cell cell1 = new Cell(new Phrase(book.getBname(),
FonttextChinese));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
Cell cell2 = new Cell(new Phrase(String.valueOf(book
.getBprice()), FonttextChinese));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
Cell cell3 = new Cell(new Phrase(book.getStime(),
FonttextChinese));
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
Cell cell4 = new Cell(new Phrase(book.getEtime(),
FonttextChinese));
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
Cell cell5 = new Cell(new Phrase(
String.valueOf(book.getBnum()), FonttextChinese));
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
mytable.addCell(cell0);
mytable.addCell(cell1);
mytable.addCell(cell2);
mytable.addCell(cell3);
mytable.addCell(cell4);
mytable.addCell(cell5);
}
document.add(mytable);
} catch (Exception de) {
de.printStackTrace();
}
document.close();
}
/**
* @param args
*/
public static void main(String[] args) {
Text t = new Text();
t.createtable();
}
}
自己慢慢的看吧,根据上面,可以实现你自己实现的效果!
2。具体代码
package com.jw.text;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.awt.Color;
import java.awt.Point;
import java.io.*;
import java.net.MalformedURLException;
import sun.management.FileSystem;
import com.jw.dao.BookDAO;
import com.jw.po.Books;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.Watermark;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class Text {
private Document document = null;
// 第一种简单的文本
public void create1() {
Rectangle pageSize = new Rectangle(144, 720);
pageSize.setBackgroundColor(new Color(0xFF, 0xFF, 0xDE));
try {
document = new Document(pageSize);
PdfWriter.getInstance(document, new FileOutputStream("d:\\1.pdf"));
document.open();
// 横向输出
document.add(new Paragraph(
"sdfsdfsdfsdfsddsfsdfsdsdfsdfsdsdfsdfasfsdfhelloworld"));
} catch (Exception e) {
e.printStackTrace();
}
document.close();
}
// 第2种简单的文本
public void create2() {
document = new Document(PageSize.A4.rotate());
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0103.pdf"));
document.open();
for (int i = 0; i < 20; i++) {
// 竖向输出
document
.add(new Phrase(
"Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea,
Hello Land, Hello People. "));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// 第3种简单的文本
public void create3() {
document = new Document(PageSize.A5, 36, 72, 108, 180);
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0104.pdf"));
document.open();
Paragraph paragraph = new Paragraph();
paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
for (int i = 0; i < 20; i++) {
paragraph
.add("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land,
Hello People. ");
}
Date date = new Date();
SimpleDateFormat form = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
String mytime = form.format(date);
paragraph.add(mytime);
document.add(paragraph);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// 第4种,设置文件的头和标题等......
public void create4() {
document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0106.pdf"));
document.addTitle("Hello World example");
document.addSubject("This example explains step 6 in Chapter 1");
document.addKeywords("Metadata, iText, step 6, tutorial");
document.addCreator("My program using iText#");
document.addAuthor("Bruno Lowagie");
document.addHeader("Expires", "0");
document.open();
document.add(new Paragraph(
"Hello World sdfgds f sd fsd f sd fsd f sd f sd fs "));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// 第5种,向pdf中打印水印图片
public void create5() {
document = new Document();
try {
String url = System.getProperty("user.dir").toString() + "\\a.jpg";
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0107.pdf"));
// 放入图片并定坐标,以及图片的路径
Watermark watermark = new Watermark(Image.getInstance(url), 200,
420);
document.add(watermark);
// 设置头信息
HeaderFooter header = new HeaderFooter(new Phrase(
"This is a header"), false);
document.setHeader(header);
document.open();
document.setPageSize(PageSize.A4.rotate());
// Watermark watermark2 = new Watermark(Image.getInstance(url), 320,
// 200);
// document.add(watermark2);
// 设置结尾
HeaderFooter footer = new HeaderFooter(
new Phrase("This is page: "), true);
document.setFooter(footer);
// 设置内容
document.add(new Paragraph("Hello World"));
// 创建第2的页面
document.newPage();
document.add(new Paragraph("Hello Earth"));
document.resetHeader();
// 创建第3的页面
document.newPage();
document.add(new Paragraph("Hello Sun"));
document.add(new Paragraph("Remark: the header has vanished!"));
document.resetPageCount();
// 创建第4的页面
document.newPage();
document.add(new Paragraph("Hello Sun"));
document.add(new Paragraph("Remark: the header has vanished!"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// 第6种设置文本的布局
public void create6() {
String url = System.getProperty("user.dir").toString() + "\\a.jpg";
document = new Document();
try {
PdfWriter writerA = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0108a.pdf"));
// 设置布局
writerA.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);
PdfWriter writerB = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0108b.pdf"));
// 设置布局
writerB.setViewerPreferences(PdfWriter.HideMenubar
| PdfWriter.HideToolbar);
PdfWriter writerC = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0108c.pdf"));
// 设置布局
writerC.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft
| PdfWriter.PageModeFullScreen
| PdfWriter.NonFullScreenPageModeUseThumbs);
Watermark watermark = new Watermark(Image.getInstance(url), 200,
420);
document.add(watermark);
HeaderFooter header = new HeaderFooter(new Phrase(
"This is a header"), false);
document.setHeader(header);
document.open();
document.setPageSize(PageSize.A4.rotate());
HeaderFooter footer = new HeaderFooter(
new Phrase("This is page: "), true);
document.setFooter(footer);
document.add(new Paragraph("Hello World"));
document.newPage();
document.add(new Paragraph("Hello Earth"));
document.resetHeader();
document.newPage();
document.add(new Paragraph("Hello Sun"));
document.add(new Paragraph("Remark: the header has vanished!"));
document.resetPageCount();
document.newPage();
document.add(new Paragraph("Hello Moon"));
document
.add(new Paragraph("Remark: the pagenumber has been reset!"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// 第7种---------------
public void create7() {
try {
document = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0109.pdf"));
// setEncryption方法中可以设置如下内容(这样打开pdf时需要输入口令!!!!!!!!!!!)
// PdfWriter.STRENGTH128BITS, "userpass", "ownerpass",
// PdfWriter.AllowCopy | PdfWriter.AllowPrinting
writer.setEncryption(PdfWriter.STRENGTH40BITS, "", "",
PdfWriter.AllowCopy);
document.open();
document.add(new Paragraph("This document is Top Secret!"));
document.open();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// 第8种
public void create8() {
String url = System.getProperty("user.dir").toString() + "\\a.jpg";
document = new Document();
try {
PdfWriter writerA = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0111a.pdf"));
PdfWriter writerB = PdfWriter.getInstance(document,
new FileOutputStream("d:\\Chap0111b.pdf"));
writerB.pause();
Watermark watermark = new Watermark(Image.getInstance(url), 200,
420);
document.add(watermark);
writerB.resume();
HeaderFooter header = new HeaderFooter(new Phrase(
"This is a header"), false);
document.setHeader(header);
document.open();
document.setPageSize(PageSize.A4.rotate());
HeaderFooter footer = new HeaderFooter(
new Phrase("This is page: "), true);
document.setFooter(footer);
document.add(new Paragraph("Hello World"));
document.newPage();
document.add(new Paragraph("Hello Earth"));
writerA.pause();
document.resetHeader();
writerA.resume();
document.newPage();
document.add(new Paragraph("Hello Sun"));
writerA.pause();
document.add(new Paragraph("Remark: the header has vanished!"));
writerA.resume();
writerB.pause();
document.resetPageCount();
writerB.resume();
document.newPage();
document.add(new Paragraph("Hello Moon"));
writerB.pause();
document
.add(new Paragraph("Remark: the pagenumber has been reset!"));
writerB.resume();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// ----------------------------第10种设置字体的样式和颜色--------------------------------------
public void create10() {
try {
document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0202.pdf"));
document.open();
Phrase phrase0 = new Phrase();
Phrase phrase1 = new Phrase("(1) this is a phrase\n");
Phrase phrase2 = new Phrase(
24,
"(2) this is a phrase with leading 24. You can only see the difference if the line is
long enough. Do you see it? There is more space between this line and the previous one.\n");
Phrase phrase3 = new Phrase(
"(3) this is a phrase with a red, normal font Courier, size 20. As you can see the
leading is automatically changed.\n",
FontFactory.getFont(FontFactory.COURIER, 20, Font.NORMAL,
new Color(255, 0, 0)));
Phrase phrase4 = new Phrase(new Chunk("(4) this is a phrase\n"));
Phrase phrase5 = new Phrase(
18,
new Chunk(
"(5) this is a phrase in Helvetica, bold, red and size 16 with a
given leading of 18 points.\n",
FontFactory.getFont(FontFactory.HELVETICA, 16,
Font.BOLD, new Color(255, 0, 0))));
Phrase phrase6 = new Phrase("(6)");
Chunk chunk = new Chunk(" This is a font: ");
phrase6.add(chunk);
phrase6.add(new Chunk("Helvetica", FontFactory.getFont(
FontFactory.HELVETICA, 12)));
phrase6.add(chunk);
phrase6.add(new Chunk("Times New Roman", FontFactory.getFont(
FontFactory.TIMES_NEW_ROMAN, 12)));
phrase6.add(chunk);
phrase6.add(new Chunk("Courier", FontFactory.getFont(
FontFactory.COURIER, 12)));
phrase6.add(chunk);
phrase6.add(new Chunk("Symbol", FontFactory.getFont(
FontFactory.SYMBOL, 12)));
phrase6.add(chunk);
phrase6.add(new Chunk("ZapfDingBats", FontFactory.getFont(
FontFactory.ZAPFDINGBATS, 12)));
phrase6.add("\n");
Phrase phrase7 = new Phrase(
"(7) if you don't Add a newline yourself, all phrases are glued to eachother!");
document.add(phrase1);
document.add(phrase2);
document.add(phrase3);
document.add(phrase4);
document.add(phrase5);
document.add(phrase6);
document.add(phrase7);
} catch (Exception e) {
e.printStackTrace();
}
document.close();
}
// -----------------------设置字体的样式--------------------------------
public void create11() {
document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0203.pdf"));
document.open();
document.add(new Phrase("What is the " + (char) 945
+ "-coefficient of the " + (char) 946 + "-factor in the "
+ (char) 947 + "-equation?\n"));
int i = 0;
for (i = 913; i < 970; i++) {
document.add(new Phrase(" " + i + ": " + (char) i));
}
document.add(new Phrase(
"-----------------------------------------\n"));
List l = new ArrayList();
l.add("aaaaaaaaaaaaa");
l.add("bbbbbbbbbbbbbbbbb");
document.add(new Phrase(16, "\n\n\n"));
document
.add(new Phrase(
-16,
"Hello, this is a very long phrase to show you the somewhat odd
effect of a negative leading. You can write from bottom to top. This is not fully supported. It's something between a feature
and a bug."));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
// -------------------------------------创建表格---------------------------------------------
public void createtable() {
document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream(
"d:\\Chap0501.pdf"));
document.open();
// 2 行,2列
Table aTable = new Table(4, 4);
aTable.setBorder(1);
// 第一行的列值
aTable.addCell("2.2");
aTable.addCell("3.3");
aTable.addCell("2.1");
aTable.addCell("1.3");
// 第2行的列值
aTable.addCell("2.2");
aTable.addCell("3.3");
aTable.addCell("2.1");
aTable.addCell("1.3");
document.add(aTable);
// 在下一页中建表格
document.newPage();
aTable = new Table(4, 4); // 4 rows, 4 columns
aTable.addCell("2.2");
aTable.addCell("3.3");
aTable.addCell("2.1");
aTable.addCell("1.3");
document.add(aTable);
// ------------------------下一页是从数据库中查询的数据显示到表格中--------------------------
document.newPage();
BookDAO b = new BookDAO();
List l = b.findbookall();
Table mytable = new Table(6, l.size());//6是列数,l.size()得到的是行数
// 设置格子的颜色
mytable.setDefaultCellBorderColor(Color.blue);
mytable.setBorderColor(Color.blue);
mytable.setCellpadding(0);
// 解决中文问题-------------
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
// 文本内容
Font FonttextChinese = new Font(bfChinese, 12, Font.NORMAL);
// 表头
Font FontheadChinese = new Font(bfChinese, 15, Font.BOLD);
// 设置表头
Cell h1 = new Cell(new Phrase("编号", FontheadChinese));
h1.setHorizontalAlignment(Element.ALIGN_CENTER);
h1.setBackgroundColor(Color.lightGray);
Cell h2 = new Cell(new Phrase("书籍名称", FontheadChinese));
h2.setHorizontalAlignment(Element.ALIGN_CENTER);
h2.setBackgroundColor(Color.lightGray);
Cell h3 = new Cell(new Phrase("价格", FontheadChinese));
h3.setHorizontalAlignment(Element.ALIGN_CENTER);
h3.setBackgroundColor(Color.lightGray);
Cell h4 = new Cell(new Phrase("开始时间", FontheadChinese));
h4.setHorizontalAlignment(Element.ALIGN_CENTER);
h4.setBackgroundColor(Color.lightGray);
Cell h5 = new Cell(new Phrase("结束时间", FontheadChinese));
h5.setHorizontalAlignment(Element.ALIGN_CENTER);
h5.setBackgroundColor(Color.lightGray);
Cell h6 = new Cell(new Phrase("书刊号", FontheadChinese));
h6.setHorizontalAlignment(Element.ALIGN_CENTER);
h6.setBackgroundColor(Color.lightGray);
mytable.addCell(h1);
mytable.addCell(h2);
mytable.addCell(h3);
mytable.addCell(h4);
mytable.addCell(h5);
mytable.addCell(h6);
for (int i = 0; i < l.size(); i++) {
Books book = (Books) l.get(i);
Cell cell0 = new Cell(new Phrase(String.valueOf(book.getBid()),
FonttextChinese));
cell0.setHorizontalAlignment(Element.ALIGN_CENTER);
Cell cell1 = new Cell(new Phrase(book.getBname(),
FonttextChinese));
cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
Cell cell2 = new Cell(new Phrase(String.valueOf(book
.getBprice()), FonttextChinese));
cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
Cell cell3 = new Cell(new Phrase(book.getStime(),
FonttextChinese));
cell3.setHorizontalAlignment(Element.ALIGN_CENTER);
Cell cell4 = new Cell(new Phrase(book.getEtime(),
FonttextChinese));
cell4.setHorizontalAlignment(Element.ALIGN_CENTER);
Cell cell5 = new Cell(new Phrase(
String.valueOf(book.getBnum()), FonttextChinese));
cell5.setHorizontalAlignment(Element.ALIGN_CENTER);
mytable.addCell(cell0);
mytable.addCell(cell1);
mytable.addCell(cell2);
mytable.addCell(cell3);
mytable.addCell(cell4);
mytable.addCell(cell5);
}
document.add(mytable);
} catch (Exception de) {
de.printStackTrace();
}
document.close();
}
/**
* @param args
*/
public static void main(String[] args) {
Text t = new Text();
t.createtable();
}
}
自己慢慢的看吧,根据上面,可以实现你自己实现的效果!
相关推荐
然而,通过一些扩展和技巧,itext 也可以用来实现导出Word文档的功能。这个过程涉及到将PDF的内容转换为Microsoft Word(通常为.doc或.docx格式)可读的格式。下面,我们将深入探讨如何使用itext来实现这个功能。 ...
在这个场景中,"java iText 实现从数据库中导出数据到已有的PDF模板"是一个常见的需求,它涉及到多个关键知识点。 首先,我们需要了解iText库的基本用法。iText提供了一系列API,可以创建新的PDF文档,也可以在已有...
* 7、生成pdf目录(未具体形成工具类,待定制) * 8、批量pdf合成一份pdf * 9、批量pdf合成一份pdf并生成目录 pom引入 <groupId>com.itextpdf</groupId> <artifactId>itextpdf <version>5.5.12 </dependency>
标题中的“导出PDF (iText实现) PdfDownloadDS”是指使用iText库来生成PDF文件的Java编程技术。iText是一个开源的Java库,它允许程序员创建、修改、拆分和合并PDF文档。在本案例中,"PdfDownloadDS"可能是一个自定义...
至于提到的`AuditAction.java`文件,这可能是一个包含具体实现代码的Java类,用于利用iText库进行PDF或Excel的生成。审计行动(Audit Action)可能是指记录或报告生成过程中的关键操作,如文档创建、修改、保存等,...
你可以根据具体需求对代码进行扩展,以实现更复杂的转换功能。 需要注意的是,由于Apache POI处理大型Excel文件时可能会消耗大量内存,因此在处理大数据量时需谨慎,可以考虑使用低内存模式或者流式处理。此外,...
在此,我们将深入探讨如何使用Itext实现HTML到DOC的转换,并讨论在DOC文档中保留部分HTML格式的可能性。 Itext是一个Java库,主要用于创建和修改PDF文档。虽然其主要功能是处理PDF,但通过一定的技巧,可以借助HTML...
本篇文章将深入探讨如何利用iTextPDF库来实现PDF文档的页眉和页脚功能。 首先,了解iTextPDF库的基本概念是至关重要的。iTextPDF提供了丰富的API,允许开发者创建PDF文档,插入文本、图像,甚至复杂的表格和表单。...
在这个实例中,我们将深入探讨如何使用 `iText` 来实现 Java 中的报表打印。 1. **iText 概述** `iText` 是一个开源的 Java 库,支持创建、修改和读取 PDF 文件。它提供了一系列 API,允许开发者在 PDF 文档中添加...
IText 是一个强大的Java库,专门用于处理PDF文档的创建、编辑和...不过,需要注意的是,随着版本的更新,新版本的IText可能会包含更多的功能和改进,因此在选择使用哪个版本时,需要根据具体项目需求和兼容性来考虑。
iText是一款广泛使用的开源Java库,专为处理PDF文档而设计。这个库使得开发者能够方便地创建、修改、解析PDF文档...同时,由于iText的开源性质,开发者可以根据具体需求对其进行二次开发和扩展,以满足各种定制化需求。
在Java开发中,有时我们需要对PDF文件添加水印,以便保护文档内容或表明版权信息。在这种情况下,可以使用iTextPDF库...在实际开发中,你可能需要根据具体需求调整水印的位置、透明度、字体等属性,以达到理想的效果。
在实际应用中,你可能需要根据具体需求调整这些步骤,例如添加样式、调整布局,或者处理更复杂的文档结构。记住,`iText`和`iTextAsian`提供了丰富的API,可以满足大部分PDF生成的需求,但需要对库的使用有深入理解...
- 查看具体函数的实现,以便理解和解决遇到的问题。 - 扩展或修改iText的功能,以满足特定需求。 - 进行调试和性能优化,提高应用程序的效率。 总的来说,iText5.5.12源码工程和签名文档为开发者提供了一套完整的...
在实际开发中,你可以根据具体需求选择使用iText 7的特定模块,如`itextpdf-7.0.5.jar`是核心库,`itext-layout-7.0.5.jar`关注布局,`itext-forms-7.0.5.jar`处理表单,还有其他如`itext-xmlworker-7.0.5.jar`用于...
通过查阅API,你可以了解到IText中的每一个类和方法的具体用途,这对于编写代码和解决具体问题至关重要。 在实际开发中,IText的一些关键功能包括: 1. **创建PDF**:使用`Document`类创建新的PDF文档,并通过`...
在实际开发中,根据具体需求,开发者可以灵活运用iText提供的API实现各种定制化功能。 总的来说,iText 2.17版本是开发者处理PDF文档的强大工具,无论是在生成新的PDF,还是在已有PDF上进行编辑,它都能提供高效且...
itext7是一款功能强大的PDF处理库,支持Java等多种语言,能够帮助开发者轻松实现PDF的创建、修改等功能。 #### 二、需求分析 本案例的核心需求是根据已有的PDF模板来填充图片,并最终生成完整的PDF文档。这在实际...
- **旋转页面**:通过`rotate()`函数可以轻松实现页面的横向布局,这对于某些特殊布局需求非常有用。 - **页边距调整**:创建Document时,可以设定上、下、左、右页边距,以满足不同设计需求。例如,设置左页边距为...