输出带中文字体的文字,表格,图片的java程序如下:
程序环境为JDK1.7,itextpdf-5.5.3.jar
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfUtil {
public static void main(String[] args) throws DocumentException, IOException {
getDoc();
}
public static String getDoc() throws DocumentException, IOException {
Document document = new Document();
String filename="Helloworld.PDF";
PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("D:\\Helloworld.PDF"));
// BaseFont baseFont = BaseFont.createFont("/SIMKAI.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
//方法二:使用iTextAsian.jar中的字体
// BaseFont baseFont = BaseFont.createFont("STSong-Light",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
//方法一:使用Windows系统字体(TrueType)
//BaseFont baseFont = BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
//方法三:使用资源字体(ClassPath) ,将字体文件拷贝放入ClassPath目录下
BaseFont baseFont = BaseFont.createFont("/SIMKAI.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); //楷体
BaseFont baseFont2 = BaseFont.createFont("/SIMLI.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); //隶书
BaseFont baseFont3 = BaseFont.createFont("/SIMFANG.TTF",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); //仿宋
Font font = new Font(baseFont);
Font font2 = new Font(baseFont, 22, Font.UNDERLINE|Font.BOLD|Font.ITALIC); //字体大小12,加粗,斜体,下划线
document.open();
Paragraph paragraph1=new Paragraph("Hello World中文标题\r",font);
Chunk chunk1 = new Chunk("中文测试This text is underlined \r",font2);
Chunk chunk2 =new Chunk("getDoc() is creadby \r"+DateTools.getCurrentTimeString(),font);
paragraph1.add(chunk1);
paragraph1.add(chunk2);
paragraph1.setAlignment(1); //setAlignment的参数:1为居中对齐、2为右对齐、3为左对齐,默认为左对齐。
//paragraph1.setLeading(45f);//设置行间距
document.add(paragraph1);
//生成表格加入文档
PdfPTable table = new PdfPTable(4);
table.setTotalWidth(800f);//设置表格的总宽度
table.addCell(new Paragraph("XX网用户名",font));
table.addCell(new Paragraph("出借金额",font));
table.addCell(new Paragraph("借款期限(月)",font));
table.addCell(new Paragraph("每月应收本息",font));
table.addCell(new Paragraph("duanxinfu(姓名:好哥们;身份证号:11000000000000000 )",font));
table.addCell("5000.00");
table.addCell("10");
table.addCell("510.00");
document.add(table);
//生成图片加入文档
String imagePath = "D:/test2.jpg";
//String pdfPath = "D:\\Helloworld.PDF";
BufferedImage img = ImageIO.read(new File(imagePath));
//FileOutputStream fos = new FileOutputStream(pdfPath);
//Document doc = new Document(null, 0, 0, 0, 0);
//document.setPageSize(new Rectangle(400, 200));
Image image = Image.getInstance(imagePath);
//PdfWriter.getInstance(document, fos);
//document.open();
image.scaleAbsolute(400f, 200f);
document.add(image);
document.add(paragraph1);
document.close();
return filename;
}
}
字体文件是从window系统字体文件夹下拷贝出来的。