iText官方教程

来源:互联网 发布:云记事本 php源码 编辑:程序博客网 时间:2024/06/14 15:02

如果要导出PDF文件,首选当然是iText,从网上也找了些资料,有很多,也有些乱。

自己做了个例子,感觉还是很浅,深入的内容还是研究不透。下载了官方的jar包,本来想看看

里面的Demo,结果只有api文档。资料还真是不好找。

今天发现官方还是有很多例子代码的。

下载地址:http://itextpdf.com/examples/

iText官网: http://itextpdf.com/

在首页下面有个Online resources

下面有个 example code 的链接,点击进入后,发现里面是个很华丽的教程,配备实例代码!

http://itextpdf.com/book/examples.php

这比网上其他人写的博客要权威多了,发现这个好东西,和大家分享一下。

里面有16章,内容相当全,还有演示视频。

 

 

 

这是HelloWord例子,其他自己研究吧:

[html] view plaincopy
  1. /*  
  2.  * This class is part of the book "iText in Action - 2nd Edition"  
  3.  * written by Bruno Lowagie (ISBN: 9781935182610)  
  4.  * For more info, go to: http://itextpdf.com/examples/  
  5.  * This example only works with the AGPL version of iText.  
  6.  */  
  7.    
  8. package part1.chapter01;  
  9.    
  10. import java.io.FileOutputStream;  
  11. import java.io.IOException;  
  12.    
  13. import com.itextpdf.text.Document;  
  14. import com.itextpdf.text.DocumentException;  
  15. import com.itextpdf.text.Paragraph;  
  16. import com.itextpdf.text.pdf.PdfWriter;  
  17.    
  18. /**  
  19.  * First iText example: Hello World.  
  20.  */  
  21. public class HelloWorld {  
  22.    
  23.     /** Path to the resulting PDF file. */  
  24.     public static final String RESULT  
  25.         = "results/part1/chapter01/hello.pdf";  
  26.    
  27.     /**  
  28.      * Creates a PDF file: hello.pdf  
  29.      * @param    args    no arguments needed  
  30.      */  
  31.     public static void main(String[] args)  
  32.         throws DocumentException, IOException {  
  33.         new HelloWorld().createPdf(RESULT);  
  34.     }  
  35.    
  36.     /**  
  37.      * Creates a PDF document.  
  38.      * @param filename the path to the new PDF document  
  39.      * @throws    DocumentException   
  40.      * @throws    IOException   
  41.      */  
  42.     public void createPdf(String filename)  
  43.     throws DocumentException, IOException {  
  44.         // step 1  
  45.         Document document = new Document();  
  46.         // step 2  
  47.         PdfWriter.getInstance(document, new FileOutputStream(filename));  
  48.         // step 3  
  49.         document.open();  
  50.         // step 4  
  51.         document.add(new Paragraph("Hello World!"));  
  52.         // step 5  
  53.         document.close();  
  54.     }  
  55. }  
0 0