java出力文字和图片到PDF中

/* * date: 2008/12/02 * * TODO : * */ package cn.sh.dgt.dgj.pdf; /** * @author dgt * * TODO itext-2.0.3.jar‚参照 */ 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.Image; import com.lowagie.text.Paragraph; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.PdfWriter; public class PDF_File { public static void main(String[] args) throws IOException { // 建立一个文档对象 Document doc = new Document(); try { // 确定输出PDF物理位置并把文档对象装入PDF流对象中 PdfWriter.getInstance(doc, new FileOutputStream("c:/hello.pdf")); // 打开文档对象 doc.open(); // 设置字体 // 在输出到PDF // 可用字体提示"Font 'STSong-Light' with 'UniGB-UCS2-H' is not recognized" // BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // // Font FontChinese = new Font(bfChinese, 12, Font.NORMAL); // BaseFont basefont = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", false); // 根据经验,建议使用windos自带字体 BaseFont basefont = BaseFont.createFont("c://windows//fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(basefont); // 加入文字"HelloWorld ------ 中国北京2008" String str = "HelloWorld ------ 中国北京2008"; String str1 = "HelloWorld ------ 中国北京2008中日日本語"; Paragraph tt = new Paragraph(str, FontChinese); Paragraph tt1 = new Paragraph(str1, FontChinese); doc.add(tt); doc.add(tt1); // 加入图片GetImage.jpg Image jpg = Image.getInstance("c:/GetImage.jpg"); jpg.setAlignment(Image.ALIGN_CENTER); doc.add(jpg); // 多个图片文件的话,一个一个的输出 Image jpg2 = Image.getInstance("c:/342401198209027437_000.jpg"); jpg2.setAlignment(Image.ALIGN_TOP); doc.add(jpg2); // 释放文档对象 doc.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } System.out.println("OK"); } }

 

一个处理文字和图片到PDF文件中的方法,用到itext-2.0.3.jar

你可能感兴趣的:(java,Date,image,String,Class,文档)