服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

use itext create a pdf file

import java.awt.color;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.net.malformedurlexception;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
/*
 * created on 2004-11-10
 *
 * todo to change the template for this generated file go to
 * window - preferences - java - code style - code templates
 */

/**
 * @author administrator
 *
 * todo to change the template for this generated type comment go to
 * window - preferences - java - code style - code templates
 */
public class pdfcreate {

 public static void main(string[] args) throws malformedurlexception, ioexception {
  pdfcreate pdfcreate = new pdfcreate();
  try {
   pdfcreate.createpdf();
  } catch (filenotfoundexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  } catch (documentexception e) {
   // todo auto-generated catch block
   e.printstacktrace();
  }
 }
 public void createpdf() throws documentexception, badelementexception, malformedurlexception, ioexception
 {
  document document = new document(pagesize.a4);
  //document.addtitle("title");
  //document.addheader("header","header");
  
  pdfwriter.getinstance(document, new fileoutputstream("c:/helloworld.pdf"));
  
  document.open();
  //add a word
  document.add(new paragraph("hello world!您好!hehe!"));
  //add a table
  table table = new table(3);
  table.setborderwidth(1);
  table.setbordercolor(new color(0, 0, 255));
  table.setpadding(5);
  table.setspacing(5);
  cell cell = new cell("header");
  cell.setheader(true);
  cell.setcolspan(3);
  table.addcell(cell);
  table.endheaders();
  cell = new cell("example cell with colspan 1 and rowspan 2");
  cell.setrowspan(2);
  cell.setbordercolor(new color(255, 0, 0));
  table.addcell(cell);
  table.addcell("1.1");
  table.addcell("2.1");
  table.addcell("1.2");
  table.addcell("2.2");
  table.addcell("cell test1");
  cell = new cell("big cell");
  cell.setrowspan(2);
  cell.setcolspan(2);
  table.addcell(cell);
  table.addcell("cell test2");
  
  document.add(table);
  
  image png = image.getinstance("workswithmysql.png");
  document.add(png);
  
  //basefont bfchinese = basefont.createfont("stsong-light", "unigb-ucs2-h", basefont.not_embedded);
  //com.lowagie.text.font fontchinese = new com.lowagie.text.font(bfchinese, 12, com.lowagie.text.font.normal);
  //paragraph pragraph=new paragraph("你好", fontchinese);
  //document.add(pragraph);
  
  document.close();
 }
}

扫描关注微信公众号