pdf解决方案——itext 简介

一,简介:

什么是IText,来看 IText in action的解释:

世界领先PDF类库,包括两个平台的版本,java和.net(iTextSharp),这利器的功能如图:

 

限制,要求,建议:

1,  不能创建内容太大的pdf文件(不知道上限)

主要类:

类名类

用途

类比html

PdfWriter

书写器,document和stream的沟通者。

 

Document

Pdf文档

 

Chunk

 

文本块、这是可以写入文档的最小的组件。

label

Phrase

短语、由一系列Chunk组成。

span

paragraph

段落、有一系列的Phrase或者Chunk组成。

div

PdfPTable

表格、处理复杂表格的组件

Table

Image

图形处理,多为使用其静态方法

Image

Rectangle,circle,

Line

Dotted line

矩形,圆,实线,虚线。

还有可以指定point以贝塞尔曲线画闭合线的方法。各种形状都支持指定颜色填充。几乎没有画不出的图,已实现基金分析里各种图。

Canvas

PdfContentByte

剪贴板一样,随意贴页面的任何指定位置,分页面之上和页面之下(背景)

决对定位的div

ColumnText

自适应创建新页面的利器,尽管往一个columntext对象扔内容,它可以算出本页是否已经写满,然后决定是否创建新页。

 

PdfAction

支持一些互动,比如超链接,锚点,甚至javascript

 

BaseColor

定义颜色组件、预装了很多常用颜色

 

BaseFont

定义字体

 

Event

cell,table,page,document,各种元素基本都有事件。

Body.onload. body.unload

 

 

 

       

二,入门五步法:

public static void main(String[] args)

  throws DocumentException, IOException {

  Document document = new Document();                       // Step  1               

  PdfWriter.getInstance(document, new FileOutputStream(“c:\\helloworld.pdf”));  // Step  2

  document.open();                                         // Step  3  

  document.add(new Paragraph("Hello World!"));                 // Step  4

  document.close();                                         // Step  5

}

■ Step 1—创建一个document对象

■ Step 2—绑定文档到一份stream上

■ Step 3—打开文档

■ Step 4—添加内容文档

■ Step 5—关闭文档

1,2,3,5必须的,第四步是实现各自逻辑的地方。So easy 有木有?所以不会对这个类库继续做深入的介绍,因为iText in action已经说的很好了,目前还没找到中文版,真正要做出贡献应该是去翻译这本书。除了itext in action有实例下载外,这个站点也不错:http://www.java2s.com/Code/Java/PDF-RTF/CatalogPDF-RTF.htm

三,再说说iTextSharp。

对于用了好几年c#和vs20XX的我来说,用java写东西总是觉得便秘:netbeans 6.8半智能提示(我现在用到的是要写完整个词再type in . 时,才会出提示,是我太傻?),各种get,set function~~~~~~,方法名首词小写~~~~,遇到无数次reflection重命名,现在想格式化文档前,得脑子停留下确认自己在用什么IDE。然后有机会去维护itextsharp写的pdf应用,光是用属性来替换那些get,set就有种一泄千里的舒畅感。以上纯属个人爱好,不要评论语言平台优劣。公司已经在java和.net版挣扎了好几年,最近做出决定以后只用java版,慢慢把sharp版的替换到java,最大的原因听闻说有些功能,itextsharp没有迁移过来。其他team的事情,还远在国外,具体原因不得而知。不过想强调的是itext真的很强大。

四,一个小小工具,一个小小实例:

10年兴起电子书时,买了个纽曼的电子阅读器,坑爹的只能旋转text文本,pdf不支持。放到了字体后,老要横滚,于是想写个工具把pdf旋转90°,现在的pdf阅读器都有这功能,不过好像免费版不能保存下来。于是用itext java和sharp都写了段小小的代码如下,真的很简单:

Java:

public void rotationPdf (String savePath) throws IOException {

        Document document = new Document();

        try {

            String existPdfPath="D:\\Pro-Android-3.pdf";

             // step 2

        PdfWriter writer

            = PdfWriter.getInstance(document, new FileOutputStream(savePath));

        // step 3

        document.open();

        // step 4

        PdfReader reader = new PdfReader(existPdfPath);

        int n = reader.getNumberOfPages();

        PdfImportedPage page;

        PdfPTable table = new PdfPTable(1);

        for (int i = 1; i <= n; i++) {

            page = writer.getImportedPage(reader, i);

            table.getDefaultCell().setRotation(90);

            table.addCell(Image.getInstance(page));

        }

        document.add(table);

        // step 5

        document.close();

        } catch (java.io.FileNotFoundException e) {

            e.printStackTrace();

        } catch (DocumentException e) {

            e.printStackTrace();

        } finally {

            document.close();

        }

}

 

////////////////////////////////////////////////

C#:

public static void RotationPdf (string sourceFile, string savePath, int rotation)

        {

            //step1

            Document document = new Document();

            // step 2

            using (FileStream saveStream = new FileStream(savePath, FileMode.Create, FileAccess.Write))

            {

                PdfWriter writer = PdfWriter.GetInstance(document, saveStream);

                // step 3

                document.Open();

                // step 4

                PdfReader reader = new PdfReader(sourceFile);

                int n = reader.NumberOfPages;

                PdfImportedPage page;

                PdfPTable table = new PdfPTable(1);

                for (int i = 1; i <= n; i++)

                {

                    page = writer.GetImportedPage(reader, i);

                    table.DefaultCell.Rotation = rotation;

                    table.AddCell(Image.GetInstance(page));

                }

                document.Add(table);

                document.Close();

            }

        }

 

posted on 2012-03-04 16:32  arg  阅读(1869)  评论(0编辑  收藏  举报

导航