java导出pdf(←_←也可以说制作)需要com.lowagie.text_2.1.7.jar←这货
package test;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class CreatePDFI {
public static void main(String[] args) {
try {
// 创建一个Document对象
Document document = new Document();
// 生成文档
PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\Administrator\\Desktop\\Test.pdf"));
/** 新建一个字体,iText的方法
* STSongStd-Light 是字体,在iTextAsian.jar 中以property为后缀
* UniGB-UCS2-H 是编码,在iTextAsian.jar 中以cmap为后缀
* H 代表文字版式是 横版, 相应的 V 代表 竖版
*/
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.RED);
// 打开文档,将要写入内容
document.open();
// 插入一个段落
Paragraph par = new Paragraph("阿列路亚", fontChinese);
document.add(par);
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
上面是基本的~下面的有图片和表格……_(:з」∠)_
package test;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
public class CreatePDFII {
public static void main(String[] args) {
// try {
//
// Document document = new Document();
// PdfWriter.getInstance(document, new FileOutputStream("Helloworld.PDF"));
// document.open();
// document.add(new Paragraph("Hello World"));
// document.close();
//
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// } catch (DocumentException e) {
// e.printStackTrace();
// }
// 创建一个Document对象
Document document = new Document();
try {
// 生成名为 HelloWorld.pdf 的文档
PdfWriter.getInstance(document, new FileOutputStream("C:\\Users\\Administrator\\Desktop\\HelloWorld.pdf"));
// 添加PDF文档的一些信息
document.addTitle("Hello World example");
document.addAuthor("Bruno Lowagie");
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello World, step 3, metadata");
document.addCreator("My program using iText");
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.RED);
// 打开文档,将要写入内容
document.open();
// 插入一个段落
// document.add(new Paragraph("Hello World!中文"));
Paragraph par = new Paragraph("Hello World! 呵呵",fontChinese);
document.add(par);
// 可以是绝对路径,也可以是URL
Image img = Image.getInstance("C:\\Users\\Administrator\\Desktop\\夜\\49359878_p0.jpg");
// Image image = Image.getInstance(new URL(http://xxx.com/493598.jpg));
img.setAbsolutePosition(0, 0);
document.add(img);
//换页
document.newPage();
// 设置 Table
Table aTable = new Table(3);
int width[] = {25,25,50};
aTable.setWidths(width);
aTable.setWidth(80); // 占页面宽度 80%
// aTable.setDefaultHorizontalAlignment(Element.ALIGN_LEFT);
// aTable.setDefaultVerticalAlignment(Element.ALIGN_MIDDLE);
aTable.setAutoFillEmptyCells(true); //自动填满
aTable.setPadding(1);
aTable.setSpacing(1);
// aTable.setDefaultCellBorder(0);
aTable.setBorder(0);
Cell cell = new Cell(new Phrase("这是一个测试的 3*3 Table 数据", fontChinese ));
cell.setVerticalAlignment(Element.ALIGN_TOP);
cell.setRowspan(3);
aTable.addCell(cell);
aTable.addCell(new Cell("#1"));
aTable.addCell(new Cell("#2"));
aTable.addCell(new Cell("#3"));
aTable.addCell(new Cell("#4"));
aTable.addCell(new Cell("#5"));
aTable.addCell(new Cell("#6"));
document.add(aTable);
}
catch (DocumentException de) {
System.err.println(de.getMessage());
}
catch (FileNotFoundException ioe) {
System.err.println(ioe.getMessage());
} catch (IOException e) {
e.printStackTrace();
}
// 关闭打开的文档
document.close();
}
}
具体效果↓