日期:2014-05-19  浏览次数:20747 次

关于使用IText打印PDF 的问题
Java code
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;
    }
}


以上是在网上copy的代码,我想问的时,象这种代码是不是不能直接打印pdf,
还是要手工点pdf文件的打印?

如果要直接打印pdf,上面的代码要怎么改?

目前我的打印需求是,打印已经存在的pdf文件。

------解决方案--------------------
不能直接打印pdf

------解决方案--------------------
目前我的打印需求是,打印已经存在的pdf文件。

如果是这个需求和java 有什么关系,直接点击pdf文件的打印按钮。