DANCE WITH JAVA

开发出高质量的系统

常用链接

统计

积分与排名

好友之家

最新评论

使用IText打印PDF

IText是开源的,对与打印PDF做的还不错,下边一个例子关于IText的,列出了大部份常用的功能。
import java.io.FileOutputStream;

import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;


public class Example {
    
private static Font FONT_11;
    
private static BaseFont msgothic;
    
public static void main(String[] args) throws Exception{
        
try{
            
//定义字体
            msgothic = BaseFont.createFont("D:\\workspace\\UseIText\\MSMINCHO.TTC,0",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);
            FONT_11
=new Font(msgothic);
            FONT_11.setSize(
11);
        }
catch(Exception e){
            e.printStackTrace();
        }

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

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

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

        document.add(table);
    }

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

    }

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

            
//设置对齐
            if((align != null&& (!align.equals(""))){
                cell.setHorizontalAlignment(align);
            }

            
//设置maxlines
            cell.setMaxLines(maxLines);
        }
catch(Exception e){
            e.printStackTrace();
        }

        
return cell;
    }

}

posted on 2007-01-29 11:55 dreamstone 阅读(11929) 评论(14)  编辑  收藏 所属分类: 利器其它开源框架

评论

# re: 使用IText打印PDF 2007-01-29 16:42 寸土



不错!!!  回复  更多评论   

# re: 使用IText打印PDF 2007-01-30 08:40 robin0925

不知道,對於中文的打印,有沒有解決換行的問題,即:如果換行的時候,行首是標點的情況的處理?  回复  更多评论   

# re: 使用IText打印PDF[未登录] 2007-01-30 21:25 dreamstone

标点的问题我当时也查了一下,没有找到IText提供的解决方案,有个临时办法是取一下列宽,然后算一下,如果位置刚好是标点就在标点前的一个字加一个空格,挤下一个字来,但这样的问题造成要多次算。也在找解决方案  回复  更多评论   

# re: 使用IText打印PDF 2007-03-02 09:28 dreamstone

补充几个问题的答案:
如何取得一个空的Cell
Cell cell = Cell.getDummyCell();空格的情况会被忽略
  回复  更多评论   

# re: 使用IText打印PDF 2007-03-02 09:29 dreamstone

如何调整行高
调整字体大小就可以
  回复  更多评论   

# re: 使用IText打印PDF 2007-03-02 09:30 dreamstone

如何调整空行的行高
也是用字体,但是要用\n来构造空行
Paragraph paragraph=new Paragraph("\n",FONT_44);
Cell cell=new Cell(paragraph);
cell.setColspan(len);
cell.setMaxLines(1);
table.addCell(cell);   回复  更多评论   

# re: 使用IText打印PDF 2007-03-02 09:30 dreamstone

如何打印一条线:
用DummyCell来构造空行,同时设置边框颜色
Cell cell = Cell.getDummyCell();
cell.setColspan(len);
cell.setMaxLines(1);
cell.setBorderColorBottom(Color.black);
cell.setBorderWidthBottom(1);
table.addCell(cell);   回复  更多评论   

# re: 使用IText打印PDF 2007-03-02 09:30 dreamstone

强行调整行高,缩小行间距
Paragraph paragraph=new Paragraph(content,font);
Cell cell=new Cell(paragraph);
cell.setColspan(colspan);
cell.setHorizontalAlignment(align);
//主要是这个
cell.setLeading(font.size());
cell.setMaxLines(1);
table.addCell(cell);  回复  更多评论   

# re: 使用IText打印PDF 2008-05-21 17:42 流浪的水仙

请问有办法把某一列的宽度固定么?  回复  更多评论   

# re: 使用IText打印PDF[未登录] 2008-08-07 15:52 新人

新手问个问题:
这代码是不是只能生成pdf文件,再手动打印,能不能再程序中实现打印,
即自动打印生成的pdf文件。  回复  更多评论   

# re: 使用IText打印PDF 2008-10-06 16:09 sdsd

ok,thank you!  回复  更多评论   

# re: 使用IText打印PDF[未登录] 2012-11-06 16:11 玲玲

标点符号在首行的问题有相关的解决方案吗  回复  更多评论   

# re: 使用IText打印PDF[未登录] 2013-05-06 19:18 lg

@dreamstone
赶紧寻找pdf中中文标点符号显示在首字母处的问题。  回复  更多评论   

# re: 使用IText打印PDF[未登录] 2014-03-05 17:16 x

请问如何在已有的pdf中添加表格呢?  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: