生成pdf报表

之前从未接触过pdf的使用,这次由于工作的需要,学习了一下,弄个简单的例子纪念一下!

package com.lowagie.text;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

public class Pdf {

 /**
  * @param args
  */
 public static void main(String[] args) {
  //1.新建document对象
  //第一个参数是页面大小。接下来的参数分别是左、右、上 和下页边距
  Document document=new Document(PageSize.A4,50,50,50,50);
       
  try {
   //2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以写入到磁盘中
   PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("f:\\firstPdf.pdf"));
   //3.打开文档
   document.open();
   //4.向文档中添加内容
   //通过Paragraph来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
   document.add(new Paragraph("First page of the document @ dongxy"));
   document.add(new Paragraph("Some more text on the first page with different color and font type."));
   FontFactory.getFont(FontFactory.COURIER,14,Font.BOLD,new BaseColor(255,150,200));
   //5.关闭文档
   document.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (DocumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}
截图:


生成pdf报表_第1张图片
 

你可能感兴趣的:(生成pdf报表)