import java.io.FileOutputStream; |
import java.io.IOException; |
import com.lowagie.text.Document; |
import com.lowagie.text.DocumentException; |
import com.lowagie.text.Paragraph; |
import com.lowagie.text.pdf.PdfWriter; |
public class HelloWorld { |
/** |
* 生成一个简单的PDF文件:HelloWorld.pdf |
* |
* 作者:小段 |
*/ |
public static void main(String[] args) { |
System.out.println( "Hello World" ); |
//第一步:创建一个document对象。 |
Document document = new Document(); |
try { |
// 第二步: |
// 创建一个PdfWriter实例, |
// 将文件输出流指向一个文件。 |
PdfWriter.getInstance(document, new FileOutputStream( "C:\\HelloWorld.pdf" )); |
// 第三步:打开文档。 |
document.open(); |
// 第四步:在文档中增加一个段落。 |
document.add( new Paragraph( "Hello World" + "," + "Hello iText" + "," + "Hello xDuan" )); |
} catch (DocumentException de) { |
System.err.println(de.getMessage()); |
} catch (IOException ioe) { |
System.err.println(ioe.getMessage()); |
} |
// 第五步:关闭文档。 |
document.close(); |
// 检验程序是否正常运行到这里。 |
System.out.println( "快去看看吧" ); |
} |
} |