使用IText打印PDF

IText是开源的,对与打印PDF做的还不错,下边一个例子关于IText的,列出了大部份常用的功能。
None.gif import  java.io.FileOutputStream;
None.gif
None.gif
import  com.lowagie.text.Cell;
None.gif
import  com.lowagie.text.Document;
None.gif
import  com.lowagie.text.Font;
None.gif
import  com.lowagie.text.PageSize;
None.gif
import  com.lowagie.text.Paragraph;
None.gif
import  com.lowagie.text.Table;
None.gif
import  com.lowagie.text.pdf.BaseFont;
None.gif
import  com.lowagie.text.pdf.PdfWriter;
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  Example  dot.gif {
InBlock.gif    
private static Font FONT_11;
InBlock.gif    
private static BaseFont msgothic;
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) throws Exceptiondot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
trydot.gif{
InBlock.gif            
//定义字体
InBlock.gif
            msgothic = BaseFont.createFont("D:\\workspace\\UseIText\\MSMINCHO.TTC,0",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
InBlock.gif            FONT_11
=new Font(msgothic);
InBlock.gif            FONT_11.setSize(
11);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e)dot.gif{
InBlock.gif            e.printStackTrace();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//为了测试方便,加入自动关闭打开acrord32
InBlock.gif
        Runtime.getRuntime().exec("tskill acrord32").waitFor();
InBlock.gif        Thread.sleep(
1000);
InBlock.gif        Document document
=new Document(PageSize.A4.rotate());
InBlock.gif        
//写文件
InBlock.gif
        PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("d:/temp.pdf"));
InBlock.gif        document.open();
InBlock.gif        makeDocument(document);
InBlock.gif        document.close();
InBlock.gif        
//为了测试方便,加入自动关闭打开acrord32
InBlock.gif
        Runtime.getRuntime().exec("\"c:\\Program Files\\Adobe\\Acrobat 7.0\\Reader\\acrord32.exe\" d:/temp.pdf").waitFor();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void makeDocument(Document document)throws Exceptiondot.gif{
InBlock.gif        
//table大部份操做类似与html,下边是一些常用的参数
InBlock.gif        
//3是总的列数,也可以同时指定行数和列数new Table(3,4)
InBlock.gif
        Table table=new Table(3);
InBlock.gif        
//table的宽度
InBlock.gif
        table.setWidth(98);
InBlock.gif        
//类似html的cellSpaceing
InBlock.gif
        table.setSpacing(2);
InBlock.gif        
//每一列的宽度,是比例不是固定宽度
ExpandedSubBlockStart.gifContractedSubBlock.gif
        table.setWidths(new int[]dot.gif{10,30,60});
InBlock.gif        
//对齐方式
InBlock.gif
        table.setAlignment("CENTER");
InBlock.gif        
//table是否有边框
InBlock.gif
        table.setBorder(0);
InBlock.gif        
//cell默认是否有边框
InBlock.gif
        table.setDefaultCellBorder(0);
InBlock.gif        
//自动填充空白
InBlock.gif
        table.setAutoFillEmptyCells(true);
InBlock.gif        
int n=10;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(int i=0;i<n;i++)dot.gif{
InBlock.gif            makeContent(table);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
//新的一页要加上这句
InBlock.gif
        document.newPage();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(int i=0;i<n;i++)dot.gif{
InBlock.gif            makeContent(table);
ExpandedSubBlockEnd.gif        }

InBlock.gif        document.add(table);
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void  makeContent(Table table)throws Exception dot.gif{
InBlock.gif        
int len = table.columns();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
for(int i=0;i<len;i++)dot.gif{
InBlock.gif            String testStr 
= "test"+i+"xxxxxxxxxxxxxxxxxxxxxx";
InBlock.gif            Cell cell
=new Cell(testStr);
InBlock.gif            
//max lines ,设置成1防止换行,配合cell宽度,可以实现自动截取
InBlock.gif
            cell.setMaxLines(1);
InBlock.gif            table.addCell(cell);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
//构造一个自定义的cell
ExpandedSubBlockStart.gifContractedSubBlock.gif
    public static Cell makeCell(int colspan,String align,int maxLines,Font font)dot.gif{
InBlock.gif        Cell cell 
= null;
InBlock.gif        Paragraph paragraph 
=null;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
trydot.gif{
InBlock.gif            
//使用自定义字体
InBlock.gif
            paragraph=new Paragraph("testxxx",font);
InBlock.gif            cell
=new Cell(paragraph);
InBlock.gif            
//设置colspan,同样的方法可以设置rowspan
ExpandedSubBlockStart.gifContractedSubBlock.gif
            if(colspan >1)dot.gif{
InBlock.gif                cell.setColspan(colspan);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//设置对齐
ExpandedSubBlockStart.gifContractedSubBlock.gif
            if((align != null&& (!align.equals("")))dot.gif{
InBlock.gif                cell.setHorizontalAlignment(align);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//设置maxlines
InBlock.gif
            cell.setMaxLines(maxLines);
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
catch(Exception e)dot.gif{
InBlock.gif            e.printStackTrace();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
return cell;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值