Java itext pdf

Java itext pdf

iTextAsian.jar
itextpdf-5.1.3.jar

import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class Test {

  /**
   * @author jzh
   * @param args
   */
  public static void main(String[] args) {
    try {
      // 获取当前类生成class所在的路径
      String strPath = Test.class.getClassLoader().getResource("").toString();
      // 获取file:/后面的路径
      strPath = strPath.substring(6);
      String strFileName = strPath + "test.pdf";
      Document doc = new Document();
      PdfWriter.getInstance(doc, new FileOutputStream(strFileName));
      doc.open();

      // 字体
      BaseFont bf = BaseFont.createFont("STSong-Light",
          "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
      Font font = new Font(bf, 9, Font.NORMAL);

      Paragraph title = new Paragraph("蒋智昊", font);
      title.setAlignment(title.ALIGN_CENTER);
      doc.add(title);

      doc.close();
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
}

 

发表回复

您的电子邮箱地址不会被公开。