码迷,mamicode.com
首页 > 编程语言 > 详细

Java三方---->pdf框架之IText的使用

时间:2016-05-09 20:36:04      阅读:1312      评论:0      收藏:0      [点我收藏+]

标签:

  在企业的信息系统中,报表处理一直占比较重要的作用t。通过在服务器端使用Jsp或JavaBean生成PDF报表,客户端采用超链接显示或下载得到生成的报表,这样就很好的解决了B/S系统的报表处理问题。今天我们就开始Itext的学习。我们这里主要通过Java代码来使Itext的学习更加形象。测试的代码地址: http://pan.baidu.com/s/1kVwBDx9

 

项目结构如下,我们使用了junit的方式进行测试。

技术分享

一、 简单的Itext实例

@Test
public void createPDF_1() throws Exception {
    Document doc = new Document();// 创建一个PDF文档,我们的PDF内容都写在上面
    PdfWriter.getInstance(doc, new FileOutputStream(new File("pdf/Linux1.pdf"))); // 将pdf文档写入文件
    doc.open();
    doc.add(new Paragraph("Hello World"));// 在文档上写“Hello World”
    doc.close();
}

 生成的pdf效果图:

技术分享

二、 设置页面的大小

@Test
public void createPDF_2() throws Exception {
    Rectangle pageSize = new Rectangle(216f, 720f);
    Document doc = new Document(pageSize, 30, 30, 50, 50);// 设置页面的大小
    PdfWriter.getInstance(doc, new FileOutputStream(new File("pdf/Linux2.pdf"))); // 将pdf文档写入文件
    doc.open();
    doc.add(new Paragraph("Hello World"));// 在文档上写“Hello World”
    doc.close();
}

技术分享

三、 设置使用底层方法创建PDF

@Test
public void createPDF_3() throws Exception {
    OutputStream stream = new FileOutputStream(new File("pdf/Linux3.pdf"));
    Document doc = new Document();
    PdfWriter writer = PdfWriter.getInstance(doc, stream);
    doc.open();
    PdfContentByte canvas = writer.getDirectContent();// 获得PDF文本流,用于在上面画图
    writer.setCompressionLevel(0);
    canvas.saveState();
    canvas.beginText();
    canvas.moveText(36, 788);// 从这个位置开始画
    canvas.setFontAndSize(BaseFont.createFont(), 12);// 设置字体
    canvas.showText("Hello Wrold");// 输出内容
    canvas.endText();
    canvas.restoreState();
    doc.close();
}F

四、 设置背景色和偏移量

@Test
public void createPDF_4() throws Exception {
    OutputStream stream = new FileOutputStream(new File("pdf/Linux4.pdf"));
    BaseFont baseFont = BaseFont.createFont("font/SIMKAI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    // 设置16号倾斜字体
    Font font = new Font(baseFont, 16, Font.ITALIC);
    // 设置字体颜色为红色
    font.setColor(255, 0, 0);
    // 创建一个PDF文档
    Document doc = new Document();
    // 将pdf文档写入什么地方
    PdfWriter writer = PdfWriter.getInstance(doc, stream);
    // 设置行间距
    writer.setInitialLeading(30f);
    // 打开pdf文档
    doc.open();
    // 在pdf文档中写入内容,并指定字体
    doc.add(new Chunk("输出中文", font));
    // 换行
    doc.add(Chunk.NEWLINE);
    doc.add(new Chunk("I love you."));

    // 设置背景色
    doc.add(Chunk.NEWLINE);
    Chunk chunk = new Chunk("This is Chunk");
    chunk.setBackground(BaseColor.YELLOW);
    doc.add(chunk);

    // 向上偏移6个单位,上标
    Chunk chunk1 = new Chunk("This is rise chunk");
    chunk1.setTextRise(6);
    doc.add(chunk1);
    // 关闭pdf文档
    doc.close();
}

技术分享

五、 设置行间距

Test
public void createPDF_5() throws Exception {
    Document doc = new Document();// 创建一个PDF文档,我们的PDF内容都写在上面
    PdfWriter.getInstance(doc, new FileOutputStream(new File("pdf/Linux5.pdf"))); // 将pdf文档写入文件
    doc.open();

    Phrase phrase = new Phrase();
    // 设置行间距,默认为1.5倍
    phrase.setLeading(70);
    // 这种phrase的内容
    phrase.add(new Chunk("This is frist chunk"));
    phrase.add(Chunk.NEWLINE);
    phrase.add(new Chunk("This is second chunk"));
    doc.add(phrase);

    new Paragraph();
    doc.add(new Paragraph("This is first paragraph"));
    doc.add(new Paragraph("This is second paragraph"));
    doc.close();
}

技术分享

六、 设置段间距

@Test
public void createPDF_6() throws Exception {
    Document doc = new Document(PageSize.A5);
    PdfWriter.getInstance(doc, new FileOutputStream(new File("pdf/Linux6.pdf"))); // 将pdf文档写入文件
    doc.open();
    Paragraph paragraph1 = new Paragraph("This is first paragraph");
    // 居中显示
    paragraph1.setAlignment(Element.ALIGN_CENTER);
    Paragraph paragraph2 = new Paragraph("This is second paragraph");
    paragraph2.setAlignment(Element.ALIGN_RIGHT);
    // 段落2与段落2的间距加大50个单位
    paragraph2.setSpacingAfter(50);
    // 段落2与段落1的间距加大100个单位
    paragraph2.setSpacingBefore(100);
    Paragraph paragraph3 = new Paragraph("This is third paragraph,It‘s too long and show in new line");
    // 段落3距离左边100个单位显示
    paragraph3.setIndentationLeft(100);
    // 段落3的第一行距离左边50个单位显示
    paragraph3.setFirstLineIndent(50);
    doc.add(paragraph1);
    doc.add(paragraph2);
    doc.add(paragraph3);
    doc.close();
}

技术分享

七、 从List中写入数据

@Test
public void createPDF_7() throws Exception {
    Document doc = new Document(PageSize.A5);
    PdfWriter.getInstance(doc, new FileOutputStream(new File("pdf/Linux7.pdf"))); // 将pdf文档写入文件
    doc.open();
    // 创建有序List
    // 如果是有序,前面会出现序号
    // 将前面的序号改为字母,默认为数组
    List items = new List(List.ORDERED, List.ALPHABETICAL);
    // 前面的标示符小写,默认为大写
    items.setLowercase(List.LOWERCASE);

    for (int i = 0; i < 10; i++) {
        ListItem item = new ListItem("This is " + (i + 1) + " listItem");
        items.add(item);
    }
    doc.add(items);
    doc.close();
}

技术分享

八、 更改列表的标识

@Test
public void createPDF_8() throws Exception {
    Document doc = new Document();
    PdfWriter.getInstance(doc, new FileOutputStream(new File("pdf/Linux8.pdf"))); // 将pdf文档写入文件
    doc.open();
    List items = new List(List.UNORDERED);
    // 设置为false后,可以更改标识的宽度
    items.setAutoindent(false);
    // 标识宽度100
    items.setSymbolIndent(100);
    // 标识的前半部分显示内容
    items.setPreSymbol("Data");
    // 标识后半部分显示内容
    items.setPostSymbol(":");

    for (int i = 0; i < 10; i++) {
        ListItem item = new ListItem("This is " + (i + 1) + " listItem");
        items.add(item);
    }
    doc.add(items);
    doc.close();
}

技术分享

九、 插入横线

@Test
public void createPDF_9() throws Exception {
    Document doc = new Document();
    PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(new File("pdf/Linux9.pdf"))); // 将pdf文档写入文件
    doc.open();
    PdfContentByte canvas = writer.getDirectContentUnder();// 获取画布
    canvas.beginText();// 开始输入内容
    canvas.setFontAndSize(BaseFont.createFont(), 50);// 设置字体
    canvas.showTextAligned(Element.ALIGN_CENTER, "<", 200f, 200f, 0);
    // 通过选择180度
    canvas.showTextAligned(Element.ALIGN_CENTER, "<", 200f, 100f, 180);
    canvas.endText();// 结束输入内容

    // 参数
    // 1.线宽度
    // 2.直线长度,是个百分百,0-100之间
    // 3.直线颜色
    // 4.直线位置
    // 5.上下移动位置
    LineSeparator line = new LineSeparator(2f, 100, BaseColor.RED, Element.ALIGN_CENTER, -5f);
    doc.add(line);

    DottedLineSeparator dottedLine = new DottedLineSeparator();
    // 下移5个单位
    dottedLine.setOffset(-5);
    // 设置点之间的距离
    dottedLine.setGap(20);
    new Paragraph();
    doc.add(new Paragraph("This is first paragraph"));
    doc.add(dottedLine);

    // 设置一个坐标点,用于goTop定位
    Anchor topLine = new Anchor("This name is US");
    topLine.setName("US");
    doc.add(topLine);
    doc.add(line);
    // 链接到百度
    Anchor anchor = new Anchor("This is Anchor");
    anchor.setReference("http://www.baidu.com");

    for (int i = 0; i < 100; i++) {
        doc.add(new Paragraph("This is " + (i + 1) + "line"));
    }

    // 点击后,跳到topLine的位置
    Anchor goTop = new Anchor("Go Top");
    goTop.setReference("#US");
    doc.add(goTop);
    doc.add(anchor);

    // 关闭pdf文档
    doc.close();
}

十、 插入段落

@Test
    public void createPDF_10() throws Exception {
        Document doc = new Document();
        PdfWriter.getInstance(doc, new FileOutputStream(new File("pdf/Linux10.pdf"))); // 将pdf文档写入文件
        doc.open();
        Chunk top = new Chunk("This is top");
        top.setLocalDestination("Top");
        Paragraph topParagraph = new Paragraph(top);
        doc.add(topParagraph);

        for (int i = 0; i < 100; i++) {
            doc.add(new Paragraph("This is " + (i + 1) + "line"));
        }

        Chunk goTop = new Chunk("Go to top");
        goTop.setLocalGoto("Top");
        Paragraph bottomParagraph = new Paragraph(goTop);
        doc.add(bottomParagraph);
        // 关闭pdf文档
        doc.close();
    }

十一、 插入图片:

@Test
    public void createPDF_11() throws Exception {
        Document doc = new Document();
        PdfWriter.getInstance(doc, new FileOutputStream(new File("pdf/Linux11.pdf"))); // 将pdf文档写入文件
        doc.open();
        Image image = Image.getInstance("photo/button_default.png");

        image.setAlignment(Image.ALIGN_RIGHT);
        // 设置边框
        image.setBorder(Image.BOX);
        image.setBorderWidth(2);
        image.setBorderColor(BaseColor.RED);
        // 设置图片大小
        /*
         * ScaleToFit有两个参数,指的是长和宽的最大值,但是图片的长宽比还是不会变的
         * ScaleAbsoluteHeight设置图片的高度,不管长宽比 ScaleAbsoluteWidth设置图片的宽度,不管长宽比
         * ScalePercent等比例缩放
         */
        // image.ScaleToFit(1000, 1000);
        // image.ScaleAbsoluteHeight(100f);
        // image.ScaleAbsoluteWidth(100f);

        image.scalePercent(50f);
        // 图片旋转30度
        image.setRotationDegrees(30);

        doc.add(image);
        // 关闭pdf文档
        doc.close();
    }

技术分享

十二、 插入表格:

@Test
    public void createPDF_12() throws Exception {
        Document doc = new Document();
        PdfWriter.getInstance(doc, new FileOutputStream(new File("pdf/Linux12.pdf"))); // 将pdf文档写入文件
        doc.open();
        PdfPTable table = new PdfPTable(3);
        PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1"));
        cell1.setUseBorderPadding(true);
        //
        // Setting cell‘s border width and color
        //
        cell1.setBorderWidth(5f);
        cell1.setBorderColor(BaseColor.BLUE);
        table.addCell(cell1);

        PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2"));
        cell2.setUseBorderPadding(true);
        //
        // Setting cell‘s background color
        //
        cell2.setBackgroundColor(BaseColor.GRAY);
        //
        // Setting cell‘s individual border color
        //
        cell2.setBorderWidthTop(1f);
        cell2.setBorderColorTop(BaseColor.RED);
        cell2.setBorderColorRight(BaseColor.GREEN);
        cell2.setBorderColorBottom(BaseColor.BLUE);
        cell2.setBorderColorLeft(BaseColor.BLACK);
        table.addCell(cell2);

        PdfPCell cell3 = new PdfPCell(new Phrase("Cell 3"));
        cell3.setUseBorderPadding(true);
        //
        // Setting cell‘s individual border width
        //
        cell3.setBorderWidthTop(2f);
        cell3.setBorderWidthRight(1f);
        cell3.setBorderWidthBottom(2f);
        cell3.setBorderWidthLeft(1f);
        table.addCell(cell3);
        table.completeRow();

        doc.add(table);
        // 关闭pdf文档
        doc.close();
    }

技术分享

 

Java三方---->pdf框架之IText的使用

标签:

原文地址:http://www.cnblogs.com/huhx/p/framework_iText.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!