随着网络的发展,网络上需要处理的业务种类也越来越多了。比如说报表的制作,经常是用pdf来处理的。下面介绍一个写pdf文件的简单例子: 本来想着,输出pdf文件的操作比较困难,在网上找了个网站:http://www.lowagie.com/iText/。是专门介绍处理pdf文件的网站。如果你访问了这个网站,其实也没有必要往下看了。 如果有兴趣的话,请继续看一个简单的例子。
/*
pdftest.java
打开一个pdf文件,然后在文件里画几个框,每个框里涂上点颜色。
*/
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;
public class pdftest {
public static void main(String[] args) {
System.out.println("Chapter 5 example 7: borders");
Document document = new Document();
try {
PdfWriter.getInstance(document, new FileOutputStream("pdftest.pdf"));
document.open();
Table table = new Table(3);
table.setBorderWidth(1);
table.setBorderColor(new Color(0, 0, 255));
table.setBorder(Rectangle.TOP | Rectangle.BOTTOM);
table.setPadding(5);
table.setSpacing(5);
Cell cell = new Cell("header");
cell.setHeader(true);
cell.setBorderWidth(3);
cell.setBorder(Rectangle.TOP | Rectangle.BOTTOM);
cell.setColspan(3);
table.addCell(cell);
cell = new Cell("example cell with colspan 1 and rowspan 2");
cell.setRowspan(2);
cell.setBorderColor(new Color(255, 0, 0));
cell.setBorder(Rectangle.LEFT | Rectangle.BOTTOM);
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);
cell.setBorder(Rectangle.NO_BORDER);
cell.setGrayFill(0.9f);
table.addCell(cell);
table.addCell("cell test2");
document.add(table);
}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}
document.close();
}
}
这是输出的文件样本:)pdftest.pdf。比较复杂的处理,可以在上面介绍的那个网站上下载相关的文档,还有一些图的操作,还是比较有意思的。 |