java itext动态生成模板_一个用Java iText包动态生成PDF文件的代码例子

package com.sunrise.psmis.util;

import java.awt.Color;

import java.io.File;

import java.io.FileOutputStream;

import org.apache.commons.lang.StringUtils;

import com.lowagie.text.Cell;

import com.lowagie.text.Chapter;

import com.lowagie.text.Chunk;

import com.lowagie.text.Document;

import com.lowagie.text.Font;

import com.lowagie.text.FontFactory;

import com.lowagie.text.Image;

import com.lowagie.text.PageSize;

import com.lowagie.text.Paragraph;

import com.lowagie.text.Section;

import com.lowagie.text.Table;

import com.lowagie.text.pdf.BaseFont;

import com.lowagie.text.pdf.PdfWriter;

public class TestMain {

/**

* @param args

*/

public static void main(String[] args) {

// TODO

TestMain test=new TestMain();

test.genPDF();

}

private Font smallFont;

public void genPDF() {

try {

//声明中文字体,使iText支持中文

BaseFont bfChinese = BaseFont.createFont("STSong-Light",

"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

smallFont = new Font(bfChinese, 12, Font.NORMAL);

} catch (Exception e) {

smallFont = FontFactory.getFont(FontFactory.HELVETICA, 7,

Font.NORMAL, new Color(0, 0, 0));

e.printStackTrace();

}

try{

Document document = new Document(PageSize.A4, 50, 50, 50, 50);

PdfWriter writer = PdfWriter.getInstance(document,

new FileOutputStream("E:\\ITextTest.pdf"));

document.open();

//输出Paragraph段落

document.add(new Paragraph("First page of the document."));

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 Color(255, 150, 200))));

Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(

FontFactory.HELVETICA, 18, Font.BOLDITALIC,

new Color(0, 0, 255)));

//输出章节Chapter

Chapter chapter1 = new Chapter(title1, 1);

chapter1.setNumberDepth(0);

Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1",

FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD,

new Color(255, 0, 0)));

Section section1 = chapter1.addSection(title11);

Paragraph someSectionText = new Paragraph(

"This text comes as part of section 1 of chapter 1.");

section1.add(someSectionText);

someSectionText = new Paragraph("Following is a 3 X 2 table.");

section1.add(someSectionText);

//输出表格Table

Table t = new Table(3, 2);

t.setBorderColor(new Color(220, 255, 100));

//   t.setPadding(5);

//   t.setSpacing(5);

t.setBorderWidth(1);

Cell c1 = new Cell(new Chunk("中文",smallFont));

c1.setHeader(true);

t.addCell(c1);

c1 = new Cell(new Chunk("中国汉字",smallFont));

t.addCell(c1);

c1 = new Cell("Header3");

t.addCell(c1);

t.endHeaders();

t.addCell("1.1");

t.addCell("1.2");

t.addCell("1.3");

section1.add(t);

document.add(chapter1);

//输出图片image,支持GIF, Jpeg, PNG, wmf等格式

Image image = Image.getInstance("C:/Documents and Settings/Administrator/My Documents/My Pictures/aishwarya_rai3.jpg");

document.add(image);

document.close();

}catch(Exception e){

e.printStackTrace();

}

}

}

iText Project description

iText is a library that allows you to generate PDF files on the fly.

The iText classes are very useful for people who need to generate read-only, platform independent documents containing text, lists, tables and images. The library is especially useful in combination with Java(TM) technology-based Servlets: The look and feel of HTML is browser dependent; with iText and PDF you can control exactly how your servlet's output will look.

iText requires JDK 1.4. It's available for free under a multiple license: MPL and LGPL.

Introduction

You will find lots of code samples in the online tutorial iText by Example, but for a nice and simple introduction to the tool, please read the articles about iText that was published on the site of PDFDev, AdobePress and IBM (Note that the IBM article was translated into Japanese Chinese, and Korean). More experienced iText users will certainly benefit from the article published on O'Reilly Network.

Technical Requirements

In order to be able to use these classes you should be acquainted with JAVA (TM) programming.

You'll also need the following Software:

The Java Development Kit (JDK) 1.4 (or any later version) from Sun Microsystems Incorporated.

If for some reason the library should have to work with an earlier version of the JDK, you will have to compile a slightly altered version of the source yourself (with JDK 1.2 or 1.3).

Adobe Acrobat Reader from Adobe Systems Incorporated:

getacro.gif

Getting started with iText

There are three pages that are extremely important if you want to work with iText.

The first is the tutorial. Not all functionality available in iText is explained, but it's a good place to start. If you click on one of the features in the overview, you get some explanation and some examples on how to use these features. There's also the Javadoc pages for more detail on every method (again: not every method is documented, but we're working on it).

If the examples don't work or you experience some typical newbie problems, please consult the Frequently Asked Questions page.

If you need something that isn't on the feature list, please check the mailing-list archives. Lots of features are yet to be documented, but most of the iText functionality has been commented in the mailing-list. If you have a problem that has not been dealt with yet (or that should be in the FAQ), you can post it to the mailing-list. But it's always a good idea to check archives first.

IT瘾于2007年4月7日 下午03时17分51秒发布

#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值