use itext create a PDF file

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

import Java.awt.Color;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.net.MalformedURLException;

import com.lowagie.text.*;

import com.lowagie.text.pdf.*;

/*

* Created on 2004-11-10

*

* TODO To change the template for this generated file go to

* Window - Preferences - Java - Code Style - Code Templates

*/

/**

* @author Administrator

*

* TODO To change the template for this generated type comment go to

* Window - Preferences - Java - Code Style - Code Templates

*/

public class PDFCreate {

public static void main(String[] args) throws MalformedURLException, IOException {

PDFCreate pdfCreate = new PDFCreate();

try {

pdfCreate.createPDF();

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (DocumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public void createPDF() throws DocumentException, BadElementException, MalformedURLException, IOException

{

Document document = new Document(PageSize.A4);

//document.addTitle("Title");

//document.addHeader("header","Header");

PdfWriter.getInstance(document, new FileOutputStream("C:/Helloworld.PDF"));

document.open();

//add a Word

document.add(new Paragraph("Hello World!您好!hehe!"));

//add a table

Table table = new Table(3);

table.setBorderWidth(1);

table.setBorderColor(new Color(0, 0, 255));

table.setPadding(5);

table.setSpacing(5);

Cell cell = new Cell("header");

cell.setHeader(true);

cell.setColspan(3);

table.addCell(cell);

table.endHeaders();

cell = new Cell("example cell with colspan 1 and rowspan 2");

cell.setRowspan(2);

cell.setBorderColor(new Color(255, 0, 0));

table.addCell(cell);

table.addCell("1.1");

table.addCell("2.1");

table.addCell("1.2");

table.addCell("2.2");

table.addCell("cell test1");

cell = new Cell("big cell");

cell.setRowspan(2);

cell.setColspan(2);

table.addCell(cell);

table.addCell("cell test2");

document.add(table);

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航