Java开发实例大全提高篇——操作PDF篇-程序员宅基地

技术标签: java  内存管理  

第4篇  操作PDF篇
第13章  操作PDF文档
13.1  文档和文档属性
实例380  创建PDF文档
    public static void main(String[] args) {
        try {
            Document document = new Document();                // 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream("c:\\创建第一个PDF文档.pdf"));// 关联文档对象与输出流
            document.open();                            // 打开文档
            document.add(new Paragraph("First Document."));        // 向文档中添加内容
            document.add(new Paragraph("Success."));            // 向文档中添加内容
            document.close();                            // 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例381  添加PDF文档标题
    public static void main(String[] args){
        Document document=new Document();//创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("C:\\添加标题.pdf"));// 关联文档对象与输出流
            document.addTitle("Java编程词典");// 向文档中添加标题
            document.open();// 打开文档
            document.add(new Paragraph("Add Title"));// 向文档中添加内容
            
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {    
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }      
    }

实例382  添加PDF文档主题
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\添加PDF文档主题.pdf"));// 关联文档对象与输出流
            document.addSubject("学习iText的使用");// 向文档中添加主题
            document.open();// 打开文档
            document.add(new Paragraph("Subject"));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例383  添加PDF文档关键词
    public static void main(String[] args){
        Document document=new Document();// 创建文档对象
         try {
            PdfWriter.getInstance(document, new FileOutputStream("C:\\Java编程词典.pdf"));//关联文档对象与输出流
            document.addKeywords("这是一套Java开发人员必备的学习资源库!");// 向文档中添加关键字    
            document.open();// 打开文档
            document.add(new Paragraph("Keywords"));// 向文档中添加内容    
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
       }

实例384  添加PDF文档作者
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\添加PDF文档作者.pdf"));// 关联文档对象与输出流
            document.addAuthor("Zhenkun Zhang");// 向文档中添加作者
            document.open();// 打开文档
            document.add(new Paragraph("Add Author."));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例385  添加PDF文档创建者
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\Java编程词典.pdf"));// 关联文档对象与输出流
            document.addCreator("明日科技"); // 添加创建者
            document.open();// 打开文档
            document.add(new Paragraph("Creator"));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例386  添加PDF文档制作者
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\添加PDF文档制作者.pdf"));// 关联文档对象与输出流
            document.addProducer();// 向文档中添加制作者
            document.open();// 打开文档
            document.add(new Paragraph("Add Producer."));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例387  添加PDF文档创建日期
    public static void main(String args[]){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\Java编程词典.pdf"));// 关联文档对象与输出流
            document.addAuthor("明日科技");    //添加作者
            document.addCreationDate();// 创建日期
            document.open();// 打开文档
            document.add(new Paragraph("CreateDate"));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {            
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

13.2  初始化操作
实例388  设置页面大小
    public static void main(String[] args) {    
        Document document = new Document();// 创建文档对象设置文档大小
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\Java资料库.pdf"));// 关联文档对象与输出流
            Rectangle pageSize= new Rectangle(300, 100);// 设置页面大小
            document.setPageSize(pageSize); // 设置页面大小
            document.open();// 打开文档
            document.add(new Paragraph("Page Size: 300*100"));// 向文档中添加内容   
            document.close();// 关闭文档  
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (DocumentException e1) {
            e1.printStackTrace();
        }                      
    }

实例389  横向显示页面
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象设置文档大小
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\横向显示页面.pdf"));// 关联文档对象与输出流
            Rectangle pageSize= new Rectangle(150,220);// 设置页面大小
            pageSize = pageSize.rotate();
            document.setPageSize(pageSize); // 设置页面大小
            document.open();// 打开文档
            document.add(new Paragraph("Page Size"));// 向文档中添加内容   
            document.close();// 关闭文档  
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (DocumentException e1) {
            e1.printStackTrace();
        }                      
    }

实例390  纵向显示页面
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象设置文档大小
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "c:\\纵向显示页面.pdf"));// 关联文档对象与输出流
            Rectangle pageSize = new Rectangle(220, 150);// 创建表示页面大小的矩形对象,该矩形对象是横向显示的
            pageSize = pageSize.rotate();// 转换为纵向
            document.setPageSize(pageSize); // 设置页面大小
            document.open();// 打开文档
            document.add(new Paragraph("Page Size"));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (DocumentException e1) {
            e1.printStackTrace();
        }
    }


实例391  添加水印
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "c:\\tempWatermark.pdf"));// 关联文档对象与临时文件的输出流
            document.open();// 打开文档
            document.add(new Paragraph(" ")); // 向文档中添加内容
            document.close();// 关闭文档对象
            PdfReader reader = new PdfReader("c:\\tempWatermark.pdf");// 创建“tempWatermark.pdf”的PdfReader对象
            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(
                    "c:\\添加水印.pdf"));// 创建PdfStamper对象
            Image img = Image.getInstance("image/watermark.jpg");// 创建图像对象
            img.setAbsolutePosition(50, 385);// 定位图片对象
            PdfContentByte under = stamp.getUnderContent(1);// 获得第一页的内容
            under.addImage(img);// 添加图片,完成水印功能
            BaseFont chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义字体
            under.beginText();// 标记文本开始
            under.setFontAndSize(chinese, 42);// 设置字体和字号
            under.setTextMatrix(70, 550);// 设置添加内容的显示位置
            under.showText("下面是添加的水印图片.");// 添加内容
            under.endText();// 标记文本结束
            stamp.close();// PdfStamper对象,将从“tempWatermark.pdf”中读取的文档添加水印后写入“添加水印.pdf”
            File file = new File("c:\\tempWatermark.pdf");// 创建临时文件的File对象
            file.delete();// 删除临时文件
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例392  添加页眉和页脚
    public static void main(String[] args){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\页眉页脚.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            document.add(new Paragraph("Add Page Top And Foot.")); // 向文档中添加内容
            document.close();// 关闭文档对象
            PdfReader reader = new PdfReader("c:\\页眉页脚.pdf");// 创建“页眉页脚.pdf”的PdfReader对象
            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("c:\\添加页眉页脚.pdf"));// 创建PdfStamper对象
            PdfContentByte over = stamp.getOverContent(1);// 获得第一页的内容
            over.setTextRise(810);// 文本上移到810的位置
            over.beginText();// 标记文本开始
            BaseFont chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义字体
            over.setFontAndSize(chinese, 18);// 设置字体和字号
            over.showText("                                           页眉的内容");// 添加页眉
            over.endText();// 标记文本结束
            stamp.insertPage(2, PageSize.A4);// 增加新的一页,为新页添加页脚
            PdfContentByte under = stamp.getUnderContent(2);// 获得第二页的内容
            under.setTextRise(15);// 文本上移到15的位置
            under.beginText();// 标记文本开始
            under.setFontAndSize(chinese, 18);// 设置字体和字号
            under.showText("                                          页脚的内容");// 添加页脚
            under.endText();// 标记文本结束
            stamp.close();// PdfStamper对象,将从“页眉页脚.pdf”中读取的文档添加页眉页脚后写入“添加页眉页脚.pdf”
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例393  创建新页
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\创建新页.pdf"));// 关联文档与输出流
            document.open();// 打开文档
            document.add(new Paragraph("Old Page"));// 为第一页添加内容
            document.newPage();// 创建新的页
            document.add(new Paragraph("New Page"));// 为新页添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例394  为PDF文档添加页码
    public PdfTemplate pdfTemplate;// 声明模板对象
    public BaseFont baseFont;// 声明基础字体对象

    public static void main(String[] args) {
        Document document = new Document(PageSize.A4);// 创建A4纸张大小的PDF文档对象
        try {
            PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("C:\\添加页码.pdf"));// 关联文档对象与输出流
            writer.setPageEvent(new AddPageNumber());// 添加页面事件监听器
            document.open();// 打开文档
            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Paragraph pargraph = new Paragraph("添加页码和总页数。", new Font(bf, 15,
                    Font.BOLD, BaseColor.BLUE));// 创建段落对象并指定中文
            document.add(pargraph);// 向文档中添加段落
            document.newPage();// 创建新页
            document.add(pargraph);// 添加段落
            document.newPage();// 创建新页
            document.add(pargraph);// 添加段落
            document.close();// 关闭文档
        } catch (IllegalPdfSyntaxException de) {
            de.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void onOpenDocument(PdfWriter writer, Document document) {// 打开文档时执行该方法
            pdfTemplate = writer.getDirectContent().createTemplate(180, 180);// 创建模板对象
            try {
                baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                        BaseFont.NOT_EMBEDDED);// 创建基础字体
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    public void onEndPage(PdfWriter writer, Document document) {// 结束页时执行该方法
        PdfContentByte cb = writer.getDirectContent();// 创建内容对象
        cb.saveState();// 保存状态
        cb.beginText();// 文本开始标记
        cb.setFontAndSize(baseFont, 12);// 设置字体和字号
        cb.setTextMatrix(260, 800);// 设置文本显示位置
        String page = "第" + writer.getPageNumber() + "页/共";// 定义表示页码信息的变量
        cb.showText(page);// 显示文本
        cb.endText();// 文本结束标记
        cb.addTemplate(pdfTemplate, 305, 800);// 添加模板对象
        cb.stroke();// 确认并保存操作
        cb.restoreState();// 恢复标记
        cb.closePath();// 关闭内容通道
    }

    public void onCloseDocument(PdfWriter writer, Document document) {// 关闭文档时执行该方法
        pdfTemplate.beginText();// 模板文本开始标记
        pdfTemplate.setFontAndSize(baseFont, 12);// 设置模板字体和字号
        int totalPages = writer.getPageNumber() - 1;// 获得总页数
        pdfTemplate.showText(totalPages + "页");// 显示总页数信息
        pdfTemplate.endText();// 模板文本结束标记
        pdfTemplate.closePath();// 关闭模板通道
    }

13.3  字体与中文处理
实例395  设置特殊的文本字体
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c://Java类库.pdf"));    //关联文档对象与输出流
            document.open();                            //打开文档
            BaseFont bfChinese = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);//定义基本字体
            Font contentFont = new Font(bfChinese, 20);    //定义普通字体和大小
            document.add(new Paragraph("Java Function Classes", contentFont));    //向文档中添加内容并指定普通字体
            document.close();                            //关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }    
    }

实例396  加粗字体
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\Java.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont bfChinese = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);// 定义基本字体
            Font contentFont = new Font(bfChinese, 12, Font.BOLD);// 定义加粗字体
            document.add(new Paragraph("www.mingribccd.com",contentFont));// 向文档中添加内容并指定加粗字体
            document.add(new Paragraph("www.mingribook.com"));
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例397  添加下划线
    public static void main(String[] args){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("C:\\明日科技简介.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            document.add(new Paragraph("Generalize"));// 向文档中添加内容
            document.add(new Paragraph("Welcome to GuangZhou!", FontFactory.getFont(FontFactory.HELVETICA,15,Font.UNDERLINE)));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例398  添加删除线
    public static void main(String[] args){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("C:\\公司简介.pdf"));//关联文档对象与输出流
            document.open();// 打开文档
            document.add(new Paragraph("The company was builded in 1998!", FontFactory.getFont(FontFactory.COURIER, 15, Font.NORMAL | Font.STRIKETHRU)));    //向文档中添加内容
            document.add(new Paragraph("The company was builded in 1999!"));    //向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例399  在PDF文档中显示中文
    public static void main(String[] args){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("C:\\亚运速递.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体   
            Font FontChinese = new Font(Chinese, 20, Font.NORMAL);// 实例化字体类与设置字体大小属性           
            document.add(new Paragraph("中国再一次实现了金牌数和奖牌数第一的目标", FontChinese));// 向文档中添加内容并指定中文
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例400  设置PDF文档密码
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\设置密码.pdf"));// 关联文档对象与输出流
            writer.setEncryption("zzk".getBytes(), "123".getBytes(),
                    PdfWriter.ALLOW_COPY, PdfWriter.STANDARD_ENCRYPTION_128);// 设置密码参数和常量
            document.open();// 打开文档
            document.add(new Paragraph("Set Encryption"));// 向文档添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }


13.4  块、短语、段落、章节和区域
实例401  添加和创建块
CreateAddChunk.java
    public static void main(String args[]){
        Document document = new Document();    //创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("C:\\Java.pdf"));// 关联文档对象与输出流
            document.open();                //打开文档
            Chunk chunk1 = new Chunk("Text chunk1",FontFactory.getFont(FontFactory.COURIER_BOLD,15,Font.ITALIC));// 创建块定并义字体属性和添加内容
            document.add(chunk1);
            Chunk chunk2 = new Chunk("Text chunk2",FontFactory.getFont(FontFactory.COURIER_BOLD,30,Font.BOLD));// 创建块定并义字体属性和添加内容
            document.add(chunk2);
            document.close();                //关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例402  设置上标和下标
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置上标和下标.pdf"));// 关联文档对象与输出流
            document.open();
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font fontChinese = new Font(Chinese, 20, Font.NORMAL);// 实例化字体类与设置字体大小属性
            document.add(new Paragraph("下面是使用上标的效果:", fontChinese));// 添加段落
            Chunk chunk = new Chunk("X");// 创建块
            document.add(chunk);// 向文档添加内容
            chunk = new Chunk("2");// 创建块
            chunk.setTextRise(4.0f);// 提升块文本
            document.add(chunk);// 添加块
            chunk = new Chunk("+Y");// 创建块
            document.add(chunk);// 添加块
            chunk = new Chunk("2");// 创建块
            chunk.setTextRise(4.0f);// 提升块文本
            document.add(chunk);// 添加块
            document.add(new Paragraph("下面是使用下标的效果:", fontChinese));// 添加段落
            chunk = new Chunk("M");// 创建块
            document.add(chunk);// 添加块
            chunk = new Chunk("2");// 创建块
            chunk.setTextRise(-3.0f);// 降低块文本
            document.add(chunk);// 添加块
            chunk = new Chunk("+N");// 创建块
            document.add(chunk);// 添加块
            chunk = new Chunk("2");// 创建块
            chunk.setTextRise(-3.0f);// 降低块文本
            document.add(chunk);// 添加块
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例403  设置文本背景颜色
    public static void main(String[] args){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\Java编程全能词典.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Chunk chunk = new Chunk("Compile once, Run all!");// 定义块并添加内容
            chunk.setBackground(BaseColor.LIGHT_GRAY); // 设置背景颜色
            document.add(chunk);// 添加背景颜色
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例404  添加和创建短语
    public static void main(String args[]){
        Document document = new Document();        //创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\book.pdf"));// 关联对象与输出流
            document.open();// 打开文档    
            Phrase phrase1 = new Phrase("BeiJing Olympics");// 创建短语并添加内容
            document.add(phrase1);
            Phrase phrase2 = new Phrase("One world, one dream!");// 创建短语并添加内容
            document.add(phrase2);
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例405  添加和创建段落
    public static void main(String[] args){
        Document document = new Document();    //创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\Java编程词典.pdf"));    //关联文档对象与输出流
            document.open();                //打开文档
            Paragraph P1 = new Paragraph("Java programming dictionary");    
            //创建段落并添加内容
            document.add(P1);        //向文档添加段落
            Paragraph P2 = new Paragraph("The richest resource for learning");
            document.add(P2);        //向文档添加段落        
        } catch (FileNotFoundException e) {
                e.printStackTrace();
        } catch (DocumentException e) {
                e.printStackTrace();
        }
        finally{
            document.close();        //关闭文档    
        }
    }

实例406  设置段落首行缩进
    public static void main(String[] args){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\公司简介.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Paragraph P1 = new Paragraph("MR company was builded in 1999!");// 创建段落并添加内容
            P1.setFirstLineIndent(20);         //设置段落首先缩进
            document.add(new Paragraph(P1));//向文档添加段落
            Paragraph P2 = new Paragraph("Company own about for fifty employees.");//创建段落并添加内容
            document.add(P2);        //向文档添加段落
            document.close();        //关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例407  设置段落的上下间距
    public static void main(String[] args){
        Document document = new Document();        //创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\Java.pdf"));// 关联文档对象与输出流
            document.open();                    //打开文档
            Paragraph paragraph1 = new Paragraph("Java resource");    //创建段落添加内容
            paragraph1.setSpacingBefore(10);     //设置段落上边距
            paragraph1.setSpacingAfter(30);     //设置段落下边距
            document.add(paragraph1);            //向文档添加段落
            Paragraph paragraph2 = new Paragraph("Java classes introduce");            //创建段落添加内容
            paragraph2.setSpacingAfter(30);     //设置段落下边距
            document.add(paragraph2);            //向文档添加段落
            document.close();                    //关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例408  设置段落左右缩进
    public static void main(String[] args){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("C:\\Java编程词典.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Paragraph paragraph1 = new Paragraph("The Paragraph IndentLeft");// 实例化段落并添加内容
            paragraph1.setIndentationLeft(100);// 段落左缩进
            document.add(paragraph1);// 向文档中添加段落
            Paragraph paragraph2 = new Paragraph("The Paragraph IndentRight");// 实例化段落并添加内容
            paragraph2.setIndentationRight(100);// 段落右缩进
            document.add(paragraph2);// 向文档中添加段落
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例409  设置段落的对齐方式
    public static void main(String[] args){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\Java.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Paragraph paragraph1 = new Paragraph("www.mingrosoft.com");// 创建段落并添加内容
            paragraph1.setAlignment(Element.ALIGN_LEFT); // 左对齐
            document.add(new Paragraph(paragraph1));     // 向文档添加段落    
            Paragraph paragraph2 = new Paragraph("www.mingribook.com");// 创建段落并添加内容
            paragraph2.setAlignment(Element.ALIGN_RIGHT);// 右对齐
            document.add(new Paragraph(paragraph2));     // 向文档添加段落
                    
            Paragraph paragraph3 = new Paragraph("www.mingribccd.com"); // 创建段落并添加内容
            paragraph3.setAlignment(Element.ALIGN_CENTER);// 居中对齐
            document.add(new Paragraph(paragraph3));      // 向文档添加段落    
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
     }

实例410  设置段落字体大小
    public static void main(String[] args){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\个人简历.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            document.add(new Paragraph("Personal Resumes",FontFactory.getFont(FontFactory.HELVETICA,50,Font.BOLDITALIC)));// 定义段落字体属性并添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例411  设置段落文本颜色
    public static void main(String[] args){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\Java类库参考手册.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Paragraph paragraph = new Paragraph(new Paragraph("JFrame Class Member List",FontFactory.getFont(FontFactory.HELVETICA, 30, BaseColor.BLUE )));// 创建段落定义字体并添加内容
            paragraph.setFirstLineIndent(100); // 设置段落首先缩进
            document.add(paragraph);// 向文档添加段落
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例412  添加章节
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\添加章节.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Chapter chapter = new Chapter("This is chapter 1", 1);// 创建与新章节对象关联的内容
            document.add(chapter);// 向文档中添加章节
            chapter = new Chapter("This is chapter 2", 2);// 创建与新章节对象关联的内容
            document.add(chapter);// 向文档中添加章节
            chapter = new Chapter("This is chapter 3", 3);// 创建与新章节对象关联的内容
            document.add(chapter);// 向文档中添加章节
            chapter = new Chapter("This is chapter 4", 4);// 创建与新章节对象关联的内容
            document.add(chapter);// 向文档中添加章节
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例413  在章节中添加小节
    public static void main(String[] args){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("C:\\在章节中添加小节.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体   
            Font fontChinese1 = new Font(Chinese, 18, Font.BOLDITALIC,BaseColor.RED);// 实例化字体类、设置字体大小和颜色
            Font fontChinese2 = new Font(Chinese, 15, Font.BOLDITALIC,BaseColor.BLUE);// 实例化字体类、设置字体大小和颜色
            Paragraph paragraph = new Paragraph("章节",fontChinese1);// 创建段落对象
            Chapter chapter = new Chapter(paragraph,1);// 创建章节对象
            paragraph = new Paragraph("小节一",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            paragraph = new Paragraph("小节二",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            document.add(chapter);// 向文档添加章节
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例414  在小节中添加列表
    public static void main(String[] args){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("C:\\在小节中添加列表.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体   
            Font fontChinese1 = new Font(Chinese, 18, Font.BOLDITALIC,BaseColor.RED);// 实例化字体类、设置字体大小和颜色
            Font fontChinese2 = new Font(Chinese, 15, Font.BOLDITALIC,BaseColor.BLUE);// 实例化字体类、设置字体大小和颜色
            Font fontChinese3 = new Font(Chinese, 12, Font.NORMAL,BaseColor.BLACK);// 实例化字体类、设置字体大小和颜色
            Paragraph paragraph = new Paragraph("章节",fontChinese1);// 创建段落对象
            Chapter chapter = new Chapter(paragraph,1);// 创建章节对象
            paragraph = new Paragraph("小节",fontChinese2);// 创建段落对象
            Section section = chapter.addSection(paragraph);// 创建并加入小节对象
            paragraph = new Paragraph("\n小节中添加的列表如下:\n\n",fontChinese3);// 创建段落对象
            section.add(paragraph);// 向小节添加段落
            List list = new List(true, false, 10);// 创建列表
            list.add(new ListItem("小节中的列表一",fontChinese3));// 向列表添加内容
            list.add(new ListItem("小节中的列表二",fontChinese3));
            list.add(new ListItem("小节中的列表三",fontChinese3));
            section.add(list);// 向小节添加列表
            document.add(chapter);// 向文档添加章节
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例415  在小节中添加段落
    public static void main(String[] args){
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("C:\\在小节中添加段落.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体   
            Font fontChinese1 = new Font(Chinese, 18, Font.BOLDITALIC,BaseColor.RED);// 实例化字体类、设置字体大小和颜色
            Font fontChinese2 = new Font(Chinese, 15, Font.BOLDITALIC,BaseColor.BLUE);// 实例化字体类、设置字体大小和颜色
            Font fontChinese3 = new Font(Chinese, 12, Font.NORMAL,BaseColor.BLACK);// 实例化字体类、设置字体大小和颜色
            Paragraph paragraph = new Paragraph("章节",fontChinese1);// 创建段落对象
            Chapter chapter = new Chapter(paragraph,1);// 创建章节对象
            paragraph = new Paragraph("小节",fontChinese2);// 创建段落对象
            Section section = chapter.addSection(paragraph);// 创建并加入小节对象
            paragraph = new Paragraph("小节中添加的内容",fontChinese3);// 创建段落
            section.add(paragraph);// 向小节添加段落内容
            paragraph = new Paragraph("小节中添加的另一部分内容",fontChinese3);// 创建段落
            section.add(paragraph);// 向小节添加段落内容
            document.add(chapter);// 向文档添加章节
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例416  在小节中添加表格
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\在小节中添加表格.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font fontChinese1 = new Font(Chinese, 18, Font.BOLDITALIC,
                    BaseColor.RED);// 实例化字体类、设置字体大小和颜色
            Font fontChinese2 = new Font(Chinese, 15, Font.BOLDITALIC,
                    BaseColor.BLUE);// 实例化字体类、设置字体大小和颜色
            Font fontChinese3 = new Font(Chinese, 12, Font.NORMAL,
                    BaseColor.BLACK);// 实例化字体类、设置字体大小和颜色
            Paragraph paragraph = new Paragraph("章节", fontChinese1);// 创建段落对象
            Chapter chapter = new Chapter(paragraph, 1);// 创建章节对象
            paragraph = new Paragraph("小节", fontChinese2);// 创建段落对象
            Section section = chapter.addSection(paragraph);// 创建并加入小节对象
            PdfPTable table = new PdfPTable(3);// 创建表格对象
            table.addCell("1,1");// 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("1,2");
            table.addCell("1,3");
            table.addCell("2,1");
            table.addCell("2,2");
            table.addCell("2,3");
            table.addCell("3,1");
            table.addCell("3,2");
            table.addCell("3,3");
            paragraph = new Paragraph("\n下面是小节中添加的表格:\n\n", fontChinese3);// 创建段落
            section.add(paragraph);// 向小节添加段落内容
            section.add(table);// 将表格添加到小节
            document.add(chapter);// 向文档添加章节
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例417  在小节中添加图片
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\在小节中添加图片.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font fontChinese1 = new Font(Chinese, 18, Font.BOLDITALIC,
                    BaseColor.RED);// 实例化字体类、设置字体大小和颜色
            Font fontChinese2 = new Font(Chinese, 15, Font.BOLDITALIC,
                    BaseColor.BLUE);// 实例化字体类、设置字体大小和颜色
            Font fontChinese3 = new Font(Chinese, 12, Font.NORMAL,
                    BaseColor.BLACK);// 实例化字体类、设置字体大小和颜色
            Paragraph paragraph = new Paragraph("章节", fontChinese1);// 创建段落对象
            Chapter chapter = new Chapter(paragraph, 1);// 创建章节对象
            paragraph = new Paragraph("小节", fontChinese2);// 创建段落对象
            Section section = chapter.addSection(paragraph);// 创建并加入小节对象
            paragraph = new Paragraph("\n小节中添加的图片如下:\n\n", fontChinese3);// 创建段落对象
            section.add(paragraph);// 向小节添加段落
            Image image = Image.getInstance("image/image.jpg");// 定义图片信息
            image.scalePercent(40);// 设置图片的显示百分比
            section.add(image);// 向小节添加图片
            document.add(chapter);// 向文档添加章节
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

13.5  读取PDF文档
实例418  文本文件转换为PDF文档
TextFileToPdf.java
    public static void main(String[] args) {
        // 将文本文件oldTextFile.txt转换为PDF文件newPdfFile.pdf
        txtFileToPdfFile("textFile\\oldTextFile.txt", "C:\\newPdfFile.pdf");
    }

    /**
     * 将文本文件转换为PDF文件的方法
     * @param txtFile 原文本文件的路径
     * @param pdfFile 生成pdf文件的路径
     */
    private static void txtFileToPdfFile(String txtFile, String pdfFile) {
        Document doc = new Document();// 创建文档对象
        try {
            FileReader fileRead = new FileReader(txtFile);// 创建字符流对象
            BufferedReader read = new BufferedReader(fileRead);// 创建字符缓冲流对象
            PdfWriter.getInstance(doc, new FileOutputStream(pdfFile));// 关联文档和输出流对象
            doc.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font fontChinese = new Font(Chinese, 18, Font.BOLDITALIC,
                    BaseColor.BLUE);// 实例化字体类、设置字体大小和颜色
            String line = null;// 存储从文本文件中读取的内容
            while ((line = read.readLine()) != null) {// 读取一行信息
                doc.add(new Paragraph(line, fontChinese));// 将读取的信息添加到文档中
            }
            doc.close();// 关闭文档对象
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
demo.txt
这是一个美丽的故事
这是一个神奇的传说
在很久很久以前,有一个......

实例419  读取PDF文档
    public static void main(String[] args) throws MalformedURLException {
        Document document = new Document();// 创建文档对象
        File file = new File("c:\\创建第一个PDF文档.pdf");// 创建File对象
        try {
            FileInputStream in = new FileInputStream(file);// 创建输入流对象
            PDFParser parser = new PDFParser(in);// 创建PDF解析器
            parser.parse();// 解析PDF文档
            PDDocument pdfdocument = parser.getPDDocument();// 获得解析后的PDF文档
            PDFTextStripper stripper = new PDFTextStripper();// 创建PDF文本剥离器
            String msg = stripper.getText(pdfdocument);// 使用剥离器从PDF文档中剥离文本信息
            System.out.println("请取到的PDF文本信息如下:\n" + msg);// 输出信息
            in.close();// 关闭输入流对象
        } catch (Exception e) {
            e.printStackTrace();
        }
        document.close();// 关闭文档
    }

实例420  读取加密的PDF文档
    public static void main(String[] args) throws MalformedURLException {
        try {
            PdfReader reader = new PdfReader("c:\\设置密码.pdf", "123".getBytes());// 创建“水印.pdf”的PdfReader对象
            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(
                    "c:\\TempFile.pdf"));// 创建PdfStamper对象
            stamp.close();// 关闭PdfStamper对象,并将从“设置密码.pdf”中读取的内容写入“TempFile.pdf”
            Document document = new Document();// 创建文档对象
            File file = new File("c:\\TempFile.pdf");// 创建File对象
            try {
                FileInputStream in = new FileInputStream(file);// 创建输入流对象
                PDFParser parser = new PDFParser(in);// 创建PDF
                parser.parse();// 解析PDF文档
                PDDocument pdfdocument = parser.getPDDocument();// 获得解析后的PDF文档
                PDFTextStripper stripper = new PDFTextStripper();// 创建PDF文本剥离器
                String msg = stripper.getText(pdfdocument);// 使用剥离器从PDF文档中剥离文本信息
                System.out.println("请取到加密的PDF文本信息如下:\n" + msg);// 输出信息
                in.close();// 关闭输入流对象
            } catch (Exception e) {
                e.printStackTrace();
            }
            document.close();// 关闭文档
            file.delete();// 删除“TempFile.pdf”
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例421  编辑PDF文档
    public static void main(String[] args) {
        createOldFile();// 创建原文件
        editOldFile();// 编辑原文件
    }

    public static void createOldFile() {// 创建原文件的方法
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter
                    .getInstance(document, new FileOutputStream("c:\\原文档.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            document.add(new Paragraph("First")); // 向文档中添加内容
            document.newPage();
            document.add(new Paragraph("Second")); // 向文档中添加内容
            document.newPage();
            document.add(new Paragraph("Third")); // 向文档中添加内容
            document.close();// 关闭文档对象
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public static void editOldFile() {// 编辑原文件的方法
        try {
            PdfReader reader = new PdfReader("c:\\原文档.pdf");// 创建“原文档.pdf”的PdfReader对象
            int totalPages = reader.getNumberOfPages();// 获得总页数
            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(
                    "c:\\编辑后文档的临时文件.pdf"));// 创建PdfStamper对象
            BaseFont chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义字体
            PdfContentByte under = null;
            for (int i = 1; i <= totalPages; i++) {
                under = stamp.getUnderContent(i);// 获得每一页的内容
                under.beginText();// 标记文本开始
                under.setFontAndSize(chinese, 18);// 设置字体和字号
                under.setTextMatrix(200, 810);// 设置页码的显示位置
                under.showText("第" + i + "页");// 添加页脚
                under.endText();// 标记文本结束
                under.beginText();// 标记文本开始
                under.setFontAndSize(chinese, 32);// 设置字体和字号
                under.setTextMatrix(100, 750);// 设置文本的显示位置
                under.showText("新添加的内容" + i);// 添加新文本
                under.endText();// 标记文本结束
            }
            stamp.close();// PdfStamper对象,将从“原文档.pdf”中读取的文档添加页码后写入“编辑后文档的临时文件.pdf”
            File oldFile = new File("c:\\原文档.pdf");// 创建原文件的File对象
            oldFile.delete();// 删除原文件
            File tempFile = new File("c:\\编辑后文档的临时文件.pdf");// 创建临时文件的File对象
            tempFile.renameTo(oldFile);// 重命名临时文件为原文件名
            tempFile.delete();// 删除临时文件
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例422  导入已有文档
    public static void main(String[] args) {
        try {
            PdfReader reader = new PdfReader("c:\\newPdfFile.pdf");// 创建已有文档的PdfReader对象
            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(
                    "c:\\导入已有文档.pdf"));// 关联已有文档与输出流
            stamp.close();// 关闭PdfStamper对象,完成文档导入功能
            JOptionPane.showMessageDialog(null, "导入成功...\n完成已有文档的导入。");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例423  导入并添加页码
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\页码.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            document.add(new Paragraph("No. 1 page")); // 向文档中添加内容
            document.newPage();
            document.add(new Paragraph("No. 2 page")); // 向文档中添加内容
            document.close();// 关闭文档对象
            PdfReader reader = new PdfReader("c:\\页码.pdf");// 创建“页码.pdf”的PdfReader对象
            int totalPages = reader.getNumberOfPages();
            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(
                    "c:\\导入并添加页码.pdf"));// 创建PdfStamper对象
            BaseFont chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义字体
            PdfContentByte under = null;
            for (int i = 1; i <= totalPages; i++) {
                under = stamp.getUnderContent(i);// 获得每一页的内容
                under.beginText();// 标记文本开始
                under.setFontAndSize(chinese, 18);// 设置字体和字号
                under.setTextMatrix(280, 15);// 设置页码的显示位置
                under.showText("第" + i + "页");// 添加页脚
                under.endText();// 标记文本结束
            }
            stamp.close();// PdfStamper对象,将从“页码.pdf”中读取的文档添加页码后写入“添加页码.pdf”
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例424  导入并添加水印
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("c:\\水印.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            document.add(new Paragraph("No. One.")); // 向文档中添加内容
            document.add(new Paragraph("No. Two.")); // 向文档中添加内容
            document.add(new Paragraph("No. Three.")); // 向文档中添加内容
            document.add(new Paragraph("No. Four.")); // 向文档中添加内容
            document.add(new Paragraph("No. Five.")); // 向文档中添加内容
            document.close();// 关闭文档对象
            PdfReader reader = new PdfReader("c:\\水印.pdf");// 创建“水印.pdf”的PdfReader对象
            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("c:\\导入并添加水印.pdf"));// 创建PdfStamper对象
            Image img = Image.getInstance("image/watermark.jpg");// 写上内容
            img.setAbsolutePosition(30, 385);// 定位图片对象
            PdfContentByte under = stamp.getUnderContent(1);// 获得第一页的内容
            under.addImage(img);// 添加图片,完成水印功能
            stamp.close();// PdfStamper对象,将从“水印.pdf”中读取的文档添加水印后写入“导入并添加水印.pdf”
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例425  导入并添加新页和内容
    public static void main(String[] args) {
        try {
            PdfReader reader = new PdfReader("c:\\创建第一个PDF文档.pdf");// 导入文档
            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(
                    "c:\\导入并添加新页和内容.pdf"));// 关联文档与输出流
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            PdfContentByte cb = stamp.getOverContent(1);// 获取第一页内容
            cb.beginText();// 写内容
            cb.setFontAndSize(Chinese, 25);// 设置字体属性
            cb.setTextMatrix(15, 15);// 设置矩阵(坐标)
            cb.showText("第一页");// 矩阵处显示文本
            cb.showTextAligned(Element.ALIGN_CENTER, "新增的内容。", 180, 760, 0);// 设置文本对齐,内容,位置和旋转角度
            cb.endText();// 内容结束
            stamp.insertPage(2, PageSize.A4);// 增加新页
            cb = stamp.getOverContent(2);// 获取第2页内容
            cb.beginText();// 写内容
            cb.setFontAndSize(Chinese, 20);// 设置字体属性
            cb.showTextAligned(Element.ALIGN_LEFT, "在新增的页中添加的内容。", 100, 600, 0);// 设置文本对齐,内容,位置和旋转角度
            cb.endText();// 内容结束
            stamp.close();// 关闭
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例426  拆分PDF文档
    public static void main(String[] args) {
        String filePathFile = "c:\\原文档.pdf ";// 需要拆分的原文档
        PdfReader reader = null;// 声明PdfReader对象
        try {
            reader = new PdfReader(filePathFile);// 创建PdfReader对象
        } catch (IOException e) {
            e.printStackTrace();
        }
        int pageN = reader.getNumberOfPages();// 获取文件内的页数
        for (int i = 0; i < pageN; i++) {// 循环向外拆分页
            Document document = new Document(reader
                    .getPageSizeWithRotation(i + 1));// 创建文档 同时获得前面循环的页
            PdfCopy copy = null;
            try {
                int len = filePathFile.length();// 获得文件完整路径的长度
                String noExt = filePathFile.substring(0, len - 5);// 去除文件扩展名后的路径
                String fileName = noExt + "-" + (i + 1) + ".pdf";// 拆分后生成的文件名称
                copy = new PdfCopy(document, new FileOutputStream(fileName));// 创建拷贝并关联文档与输出流对象
                document.open();// 打开文档
                copy.addPage(copy.getImportedPage(reader, i + 1));// 根据获得的页创建新文档
                document.close();// 关闭文档
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

实例427  合并PDF文档
    public static void main(String[] args) {
        String[] subFiles = { "c:\\原文档-1.pdf", "c:\\原文档-2.pdf", "c:\\原文档-3.pdf" }; // 待合并的PDF文档
        String newFile = "C:\\合并结果.pdf";// 合并后的新文档
        Document document = new Document();// 创建文本文档
        try {
            PdfCopy copy = new PdfCopy(document, new FileOutputStream(newFile));// 创建copy对象关联文档与输出流
            document.open();// 打开文档
            for (int i = 0; i < subFiles.length; i++) {// 做循环 获取待合并文件长度
                PdfReader reader = new PdfReader(subFiles[i]);// 读取待合并文件长度
                int totalPages = reader.getNumberOfPages();// 获得每个子文档的总页数
                for (int p = 1; p <= totalPages; p++) {// 遍历子文档的每一页
                    copy.addPage(copy.getImportedPage(reader, p));// 将子文档的每一页都添加到新文档中
                }
            }
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例428  打印PDF文档
    public static void main(String args[]) throws IOException,
            IllegalArgumentException, PrinterException {
        PrinterJob job = PrinterJob.getPrinterJob();// 创建打印作业
        PDDocument document = PDDocument.load("创建表格.pdf");// 获取待打印的文档
        Printable printable = new PDPageable(document);// 创建Printable对象
        job.setPrintable(printable);// 设置打印工作
        job.print();// 打印
    }

第14章  绘制PDF图形和图像
14.1  绘制图形
实例429  在PDF文档中绘制直线
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\绘制直线.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            PdfContentByte cb = writer.getDirectContent();// 获取内容
            cb.moveTo(50, 780); // 绘制起点坐标
            cb.lineTo(260, 780); // 绘制终点坐标
            cb.stroke(); // 确认直线的绘制
            cb.moveTo(50, 750);
            cb.lineTo(260, 750);
            cb.stroke();
            cb.moveTo(50, 720);
            cb.lineTo(260, 720);
            cb.stroke();
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例430  在PDF文档中绘制矩形
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\绘制矩形.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            PdfContentByte cb = writer.getDirectContent();// 获取内容
            cb.rectangle(50, 650, 200, 150); // 绘制矩形
            cb.stroke();// 确认绘制的矩形
            cb.rectangle(70, 675, 160, 100); // 绘制矩形
            cb.stroke();// 确认绘制的矩形
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例431  在PDF文档中绘制圆
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\绘制圆形.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            PdfContentByte cb = writer.getDirectContent();// 获取内容
            cb.circle(120, 720, 80); // 绘制圆形
            cb.stroke();// 确认绘制的圆形
            cb.circle(120, 720, 40); // 绘制圆形
            cb.fill();// 填充圆形
            cb.fillStroke();// 确认绘制的填充圆形
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例432  使用Graphics2D绘制图形
    public static void main(String[] args) throws MalformedURLException {
        Document document = new Document(); // 创建文档对象
        try {
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\使用Graphics2D绘制图形.pdf"));// 关联文档对象与输出流
            document.open(); // 打开文档
            PdfContentByte cb = writer.getDirectContent(); // 获取文档内容
            Graphics2D g = cb.createGraphics(850, 850); // 创建Graphics和坐标
            Rectangle2D rect1 = new Rectangle2D.Double(50, 50, 200, 150); // 创建矩形对象
            g.draw(rect1); // 绘制矩形
            Rectangle2D rect2 = new Rectangle2D.Double(70, 70, 160, 110); // 创建矩形对象
            g.fill(rect2); // 绘制填充矩形
            g.dispose(); // 部署
            cb.stroke(); // 确认绘制的图形
            document.close(); // 关闭文档
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例433  使用PdfGraphics2D绘制文本
   public static void main(String[] args) throws MalformedURLException {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\使用PdfGraphics2D绘制文本.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            PdfContentByte cb = writer.getDirectContent();// 获取文档内容
            PdfGraphics2D g = (PdfGraphics2D) cb.createGraphics(700, 800);// 获得PdfGraphics2D对象
            g.drawString("draw text. ", 54, 10);// 绘制文本
            g.drawString("second row text. ", 54, 30);// 绘制文本
            g.drawString("third row text. ", 54, 50);// 绘制文本
            g.dispose();// 部署
            cb.stroke();// 确认绘制的内容
            document.close();// 关闭文档
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例434  使用PdfGraphics2D绘制图形
    public static void main(String[] args) throws MalformedURLException {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\使用PdfGraphics2D绘制图形.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            PdfContentByte cb = writer.getDirectContent();// 获取文档内容
            PdfGraphics2D g = (PdfGraphics2D) cb.createGraphics(700, 800);// 创建Graphics和坐标
            Rectangle2D rect = new Rectangle2D.Double(120, 100, 200, 100);// 创建矩形对象
            Ellipse2D circle = new Ellipse2D.Double();// 创建圆
            circle.setFrameFromCenter(220, 80, 370, 150);// 设置圆形的中心点坐标和角点坐标
            g.draw(rect);// 绘制矩形对象
            g.draw(circle);// 绘制圆形对象
            g.dispose();// 部署
            cb.stroke();// 关闭
            document.close();// 关闭文档
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例435  使用PdfGraphics2D绘制有填充色的
    public static void main(String[] args) throws MalformedURLException {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\使用PdfGraphics2D绘制有填充色的图形.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            PdfContentByte cb = writer.getDirectContent();// 获取文档内容
            PdfGraphics2D g = (PdfGraphics2D) cb.createGraphics(700, 800);// 创建Graphics和坐标
            Rectangle2D rect = new Rectangle2D.Double(120, 130, 200, 100);// 创建矩形对象
            Ellipse2D circle = new Ellipse2D.Double();// 创建椭圆对象
            circle.setFrameFromCenter(220, 80, 370, 120);// 设置椭圆形的中心点坐标和角点坐标
            g.setColor(Color.BLUE);// 设置颜色
            g.fill(rect);// 绘制填充的矩形
            g.setColor(Color.PINK);// 设置颜色
            g.fill(circle);// 绘制填充的圆形
            g.dispose();// 部署
            cb.stroke();// 确认绘制图形
            document.close();// 关闭文档
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

图形
实例436  使用PdfGraphics2D旋转绘制的图形
    public static void main(String[] args) throws MalformedURLException {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\使用PdfGraphics2D旋转绘制的图形.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            PdfContentByte cb = writer.getDirectContent();// 获取文档内容
            PdfGraphics2D g = (PdfGraphics2D) cb.createGraphics(700, 800);// 创建PdfGraphics2D对象
            Rectangle2D rect = new Rectangle2D.Double(200, 200, 150, 200);// 创建矩形对象
            g.setColor(Color.BLUE);// 设置图形颜色
            g.rotate(20, 380, 150);// 旋转图形
            g.draw(rect);// 绘制矩形对象
            g.dispose();// 部署
            document.close();// 关闭文档
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例437  使用PdfGraphics2D缩放绘制的图形
    public static void main(String[] args) throws MalformedURLException {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\使用PdfGraphics2D缩放绘制的图形.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            PdfContentByte cb = writer.getDirectContent();// 获取文档内容
            PdfGraphics2D g = (PdfGraphics2D) cb.createGraphics(700, 800);// 创建Graphics和坐标
            Rectangle2D rect = new Rectangle2D.Double(50, 30, 120, 150);// 创建原矩形对象
            g.setColor(Color.BLUE);// 设置颜色
            g.fill(rect);// 绘制有填充色的图形
            rect = new Rectangle2D.Double(150, 30, 120, 150);// 创建与原矩形大小相同的矩形对象
            g.scale(1.4, 1.2f);// 缩放矩形对象
            g.setColor(Color.PINK);// 设置颜色
            g.fill(rect);// 绘制有填充色的图形
            g.dispose();// 部署
            cb.stroke();// 确认绘制内容
            document.close();// 关闭文档
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例438  使用PdfGraphics2D平移绘制的图形
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\使用PdfGraphics2D平移绘制的图形.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            PdfContentByte cb = writer.getDirectContent();// 获取文档内容
            PdfGraphics2D g = (PdfGraphics2D) cb.createGraphics(1200, 800);// 创建Graphics和坐标
            Rectangle2D rect = new Rectangle2D.Double(50, 30, 120, 150);// 创建原矩形对象
            g.setColor(Color.BLUE);// 设置颜色
            g.fill(rect);// 绘制有填充色的图形
            g.translate(150.0f, 1.0f);// 平移矩形对象
            g.setColor(Color.PINK);// 设置颜色
            g.fill(rect);// 绘制有填充色的图形
            g.dispose();// 部署
            cb.stroke();// 确认绘制内容
            document.close();// 关闭文档
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

14.2  绘制图像
实例439  添加图片
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\添加图片.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Image image = Image.getInstance("image/picture.jpg");// 创建图像对象
            document.add(image);// 向文档添加图片
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例440  设置图片对齐方式
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置图片对齐方式.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Image image = Image.getInstance("image/picture.jpg");// 创建图像对象
            image.setAlignment(Image.LEFT);// 设置图片居左
            image.scalePercent(25);// 设置原图像的比例
            document.add(image);// 向文档添加图片
            image = Image.getInstance("image/picture.jpg");// 创建图像对象
            image.setAlignment(Image.MIDDLE);// 设置图片居中
            image.scalePercent(30);// 设置原图像的比例
            document.add(image);// 向文档添加图片
            image = Image.getInstance("image/picture.jpg");// 创建图像对象
            image.setAlignment(Image.RIGHT);// 设置图片居右
            image.scalePercent(20);// 设置原图像的比例
            document.add(image);// 向文档添加图片
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例441  将图片设置为背景
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\将图片设置为背景.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font FontChinese = new Font(bfChinese, 50, Font.BOLD,
                    BaseColor.BLUE);// 实例化字体类与设置字体大小属性
            Paragraph p = new Paragraph("下面是背景图片", FontChinese);// 创建段落对象
            p.setSpacingBefore(60); // 设置段落上边距
            p.setAlignment(com.itextpdf.text.Element.ALIGN_CENTER);
            Image image = Image.getInstance("image/gb.jpg");// 定义图片对象
            image.setAlignment(Image.UNDERLYING);// 将图片设置为背景
            document.add(image);// 向文档添加如片
            document.add(p);// 添加段落
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例442  设置文字环绕
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置文字环绕.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Image image = Image.getInstance("image/picture.jpg");// 创建图像对象
            image.scalePercent(33);// 设置原图像的比例
            image.setAlignment(Image.TEXTWRAP);// 将图片设置为文字环绕
            document.add(image);// 向文档添加图片
            StringBuffer sb = new StringBuffer();// 创建字符串缓存
            for (int i = 1; i <= 200; i++) {
                sb.append(i + " ");// 向字符串缓存中添加内容
            }
            Paragraph p = new Paragraph(sb.toString());// 创建段落对象
            document.add(p);// 将段落添加到文档中
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例443  设置图片大小
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置图片大小.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Image image = Image.getInstance("image/picture.jpg");// 创建图像对象
            image.setAlignment(Image.MIDDLE);// 居中显示图片
            image.scaleAbsolute(180, 120);// 设置图片新的宽度和高度
            document.add(image);// 向文档添加图片
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例444  调整图片比例
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\调整图片比例.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Image image = Image.getInstance("image/picture.jpg");// 创建图像对象
            image.scalePercent(30);// 调整图片的比例,使其大小为原图片的30%
            document.add(image);// 向文档添加图片
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例445  设置高度和宽度的比例
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置高度和宽度的比例.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Image image = Image.getInstance("image/picture.jpg");// 创建图像对象
            image.scalePercent(50, 40);// 设置宽度和高度比例分别为原图片的50%和40%
            document.add(image);// 向文档添加图片
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例446  旋转图片
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\旋转图片.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Image image = Image.getInstance("image/picture.jpg");// 创建图像对象
            image.setRotation(320);// 设置旋转弧度
            document.add(image);// 向文档添加图片
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例447  使用PdfGraphics2D绘制图片
    public static void main(String[] args) {
        Document document = new Document(); // 打开文档
        try {
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("c:\\使用PdfGraphics2D绘制图片.pdf"));// 关联文档与输出流
            document.open();// 打开文档
            PdfContentByte cb = writer.getDirectContent();// 获取文档内容
            PdfGraphics2D g = (PdfGraphics2D) cb.createGraphics(700, 800);// 创建PdfGraphics2D对象
            BufferedImage image = ImageIO.read(new File("image/picture.jpg"));// 获取图片
            g.drawImage(image, 50, 10, null);// 绘制图片
            g.dispose();// 部署
            cb.stroke();// 确认绘制的内容
            document.close();// 关闭文档
        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

第15章  绘制PDF表格
15.1  Table表格
实例448  创建具有指定列数的表格
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\创建具有指定列数的表格.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基本字体
            Font fontChinese = new Font(bfChinese, 28, Font.NORMAL);// 实例化字体
            document.add(new com.lowagie.text.Paragraph("这是一个具有5列的表格",
                    fontChinese));// 向文档中添加内容
            Table table = new Table(5);// 创建一个5列的表格
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("1,1");
            table.addCell("1,2");
            table.addCell("1,3");
            table.addCell("1,4");
            table.addCell("1,5");
            table.addCell("2,1");
            table.addCell("2,2");
            table.addCell("2,3");
            table.addCell("2,4");
            table.addCell("2,5");
            document.add(table);// 将表格添加到文档中
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例449  创建具有指定行列数的表格
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\创建具有指定行列数的表格.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基本字体
            Font fontChinese = new Font(bfChinese, 28, Font.NORMAL);// 实例化字体
            document.add(new Paragraph("        这是一个3行3列的表格", fontChinese));
            Table table = new Table(3, 3);// 创建一个3行3列的表格
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("1,1");
            table.addCell("1,2");
            table.addCell("1,3");
            table.addCell("2,1");
            table.addCell("2,2");
            table.addCell("2,3");
            table.addCell("3,1");
            table.addCell("3,2");
            table.addCell("3,3");
            document.add(table);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例450  设置表格的边框宽度
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置表格的边框宽度.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基本字体
            Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);// 实例化字体
            Table table1 = new Table(3);// 定义表格
            document.add(new Paragraph("                默认边框的表格", fontChinese));// 创建段落
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table1.addCell("Cell1.1");
            table1.addCell("Cell1.2");
            table1.addCell("Cell1.3");
            table1.addCell("Cell2.1");
            table1.addCell("Cell2.2");
            table1.addCell("Cell2.3");
            Table table2 = new Table(3);// 定义表格
            table2.setBorderWidth(3);// 设置表格的外边框宽度
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table2.addCell("Cell1.1");
            table2.addCell("Cell1.2");
            table2.addCell("Cell1.3");
            table2.addCell("Cell2.1");
            table2.addCell("Cell2.2");
            table2.addCell("Cell2.3");
            document.add(table1);// 将表格添加到文档
            document.add(new Paragraph("                改变外边框宽度为3的表格",
                    fontChinese));// 创建段落
            document.add(table2);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例451  设置表格的边框颜色
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置表格的边框颜色.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);// 实例化字体
            Table table = new Table(3);// 定义表格
            document.add(new Paragraph("                  默认边框颜色的表格",
                    fontChinese));// 向文档添加内容
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("Cell1.1");
            table.addCell("Cell1.2");
            table.addCell("Cell1.3");
            table.addCell("Cell2.1");
            table.addCell("Cell2.2");
            table.addCell("Cell2.3");
            Table table1 = new Table(3);// 定义表格
            table1.setBorderColor(Color.BLUE);// 设置表格边框颜色
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table1.addCell("Cell1.1");
            table1.addCell("Cell1.2");
            table1.addCell("Cell1.3");
            table1.addCell("Cell2.1");
            table1.addCell("Cell2.2");
            table1.addCell("Cell2.3");
            document.add(table);// 将表格添加到文档
            document.add(new Paragraph("                  设置边框颜色后的表格",
                    fontChinese));// 向文档添加内容
            document.add(table1);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例452  设置单元格间距
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置单元格的间距.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);// 实例化字体
            Table table = new Table(3);// 定义表格
            document.add(new Paragraph("                默认的表格", fontChinese));// 向文档添加内容
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("Cell1.1");
            table.addCell("Cell1.2");
            table.addCell("Cell1.3");
            table.addCell("Cell2.1");
            table.addCell("Cell2.2");
            table.addCell("Cell2.3");
            Table table1 = new Table(3);// 定义表格
            table1.setSpacing(3);// 设置表格边框与单元格的间距
            table1.setBorderColor(Color.GREEN);// 设置表格边框颜色
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table1.addCell("Cell1.1");
            table1.addCell("Cell1.2");
            table1.addCell("Cell1.3");
            table1.addCell("Cell2.1");
            table1.addCell("Cell2.2");
            table1.addCell("Cell2.3");
            document.add(table);
            document.add(new Paragraph("                设置表格边框颜色与单元格的间距值为3",
                    fontChinese));// 向文档添加内容"
            document.add(table1);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例453  设置单元格填距
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置单元格填距.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);// 实例化字体
            Table table = new Table(3);// 定义表格
            document.add(new Paragraph("                 默认的表格", fontChinese));// 向文档添加内容
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("Cell1.1");
            table.addCell("Cell1.2");
            table.addCell("Cell1.3");
            table.addCell("Cell2.1");
            table.addCell("Cell2.2");
            table.addCell("Cell2.3");
            Table table1 = new Table(3);// 定义表格
            table1.setPadding(10f);// 设置表格边框与单元格的填距
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table1.addCell("Cell1.1");
            table1.addCell("Cell1.2");
            table1.addCell("Cell1.3");
            table1.addCell("Cell2.1");
            table1.addCell("Cell2.2");
            table1.addCell("Cell2.3");
            document.add(table);// 将表格添加到文档
            document
                    .add(new Paragraph(
                            "                 设置单元格的填距(即单元格边框与内容的间距)值为10",
                            fontChinese));// 向文档添加内容"
            document.add(table1);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例454  设置表格的表头
    public static void main(String[] args) {
        Cell cell0 = null;// 定义单元格
        Cell cell1 = null;
        Cell cell2 = null;
        Cell cell3 = null;
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置表格的表头.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font FontChinese = new Font(Chinese, 12, Font.NORMAL);// 实例化字体
            Table table = new Table(4);// 定义表格
            cell0 = new Cell(new Paragraph("编号", FontChinese));// 创建单元格
            cell0.setHorizontalAlignment(Element.ALIGN_CENTER);// 设置横向居中对齐
            cell0.setVerticalAlignment(Element.ALIGN_CENTER);// 设置垂直居中对齐
            cell0.setBackgroundColor(Color.GRAY);// 设置背景颜色
            cell0.setHeader(true);// 将单元格设置为表头
            cell1 = new Cell(new Paragraph("姓名", FontChinese)); // 创建单元格
            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);// 设置横向居中对齐
            cell1.setVerticalAlignment(Element.ALIGN_CENTER);// 设置垂直居中对齐
            cell1.setBackgroundColor(Color.GRAY);// 设置背景颜色
            cell1.setHeader(true);// 将单元格设置为表头
            cell2 = new Cell(new Paragraph("年龄", FontChinese)); // 创建单元格
            cell2.setHorizontalAlignment(Element.ALIGN_CENTER);// 设置横向居中对齐
            cell2.setVerticalAlignment(Element.ALIGN_CENTER);// 设置垂直居中对齐
            cell2.setBackgroundColor(Color.GRAY);// 设置背景颜色
            cell2.setHeader(true);// 将单元格设置为表头
            cell3 = new Cell(new Paragraph("电话", FontChinese)); // 创建单元格
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);// 设置横向居中对齐
            cell3.setVerticalAlignment(Element.ALIGN_CENTER);// 设置垂直居中对齐
            cell3.setBackgroundColor(Color.GRAY);// 设置背景颜色
            cell3.setHeader(true);// 将单元格设置为表头
            // 向表格添加单元格
            table.addCell(cell0);
            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);
            table.setPadding(4);// 设置内容与单元格间距
            for (int i = 1; i <= 3; i++) {// 向表格的单元格添加内容
                table.addCell(new Paragraph("95**0" + i));
                table.addCell(new Paragraph("李*辉", FontChinese));
                table.addCell(new Paragraph("30"));
                table.addCell(new Paragraph("0431-2222****"));
            }
            document.add(table);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例455  设置单元格所占的列数
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置单元格所占的列数.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font FontChinese = new Font(Chinese, 10, Font.NORMAL, Color.red);// 实例化字体
            Table table = new Table(5);// 定义表格
            table.addCell("1,1");// 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("2,1");
            Cell cell = new Cell(new Paragraph("我占据2列", FontChinese));// 定义一个表格单元
            cell.setColspan(2);// 设置表格列跨度(合并两个单元格)
            table.addCell(cell); // 将单元加入到表格
            table.addCell("2,1");// 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("2,2");
            table.addCell("3,2");
            table.addCell("1,3");
            table.addCell("2,3");
            table.addCell("3,3");
            Cell cell2 = new Cell(new Paragraph("我占据4列 ", FontChinese));// 定义一个表格单元
            cell2.setColspan(4);// 设置表格列跨度(合并4个单元格)
            table.addCell(cell2); // 将单元加入到表格
            table.addCell("3,1"); // 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("2,1");
            table.addCell("2,2");
            table.addCell("3,2");
            table.addCell("1,3");
            table.addCell("3,3");
            table.addCell("1,3");
            Cell cell3 = new Cell(new Paragraph("我占据3列 ", FontChinese));// 定义一个表格单元
            cell3.setColspan(3);// 设置表格列跨度(合并3个单元格)
            table.addCell(cell3); // 将单元加入到表格
            table.addCell("2,3");
            document.add(table);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例456  设置单元格所占的行数
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置单元格所占的行数.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font FontChinese = new Font(Chinese, 10, Font.NORMAL, Color.red);// 实例化字体
            Table table = new Table(5);// 定义表格
            table.addCell("1.1");// 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("2.1");
            Cell cell = new Cell(new Paragraph("我占据4行", FontChinese));// 定义一个表格单元
            cell.setRowspan(4);// 设置表格行跨度(合并4个单元格)
            table.addCell(cell); // 将单元加入到表格
            table.addCell("2.1"); // 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("2.2");
            table.addCell("3.2");
            table.addCell("1.3");
            table.addCell("2.3");
            table.addCell("3.3");
            Cell cell2 = new Cell(new Paragraph("我占据3行 ", FontChinese));// 定义一个表格单元
            cell2.setRowspan(3);// 设置表格行跨度(合并3个单元格)
            table.addCell(cell2); // 将单元加入到表格
            table.addCell("3.1"); // 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("2.1");
            table.addCell("2.2");
            table.addCell("3.2");
            table.addCell("1.3");
            table.addCell("2.3");
            table.addCell("3.3");
            table.addCell("1.3");
            table.addCell("2.3");
            table.addCell("3.3");
            document.add(table);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例457  设置单元格的背景色
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置单元格的背景色.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font FontChinese = new Font(Chinese, 12, Font.NORMAL);// 实例化字体
            document
                    .add(new Paragraph("                 为单元格填充颜色", FontChinese));
            Table table = new Table(5);// 定义表格
            table.addCell("1.1");// 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("2.1");
            table.addCell("3.1");
            table.addCell("2.1");
            table.addCell("2.2");
            Cell cell = new Cell();// 创建单元格
            cell.setBackgroundColor(Color.yellow);// 为单元格填充背景色
            table.addCell(cell); // 将单元格填入到表格
            table.addCell("1.3");
            table.addCell("2.3");
            Cell cell2 = new Cell();// 创建单元格
            cell2.setBackgroundColor(Color.red);// 为单元格填充背景色
            table.addCell(cell2);
            table.addCell("3.0");
            table.addCell("3.1");
            table.addCell("2.1");
            Cell cell3 = new Cell();// 创建单元格
            cell3.setBackgroundColor(Color.green);// 为单元格填充背景色
            table.addCell(cell3);
            table.addCell("3.2");
            table.addCell("1.3");
            table.addCell("2.3");
            Cell cell4 = new Cell();// 创建单元格
            cell4.setBackgroundColor(Color.red);// 为单元格填充背景色
            table.addCell(cell4); // 将单元格填入到表格
            table.addCell("2.1");
            table.addCell("2.2");
            Cell cell5 = new Cell();// 创建单元格
            cell5.setBackgroundColor(Color.blue);// 为单元格填充背景色
            table.addCell(cell5); // 将单元格填入到表格
            table.addCell("1.3");
            table.addCell("2.3");
            table.addCell("3.3");
            table.addCell("3.0");
            document.add(table);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例458  嵌套表格
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\嵌套表格.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font FontChinese = new Font(Chinese, 10, Font.BOLDITALIC,
                    Color.BLUE);// 实例化字体
            Font font = new Font(Chinese, 10, Font.NORMAL);
            Table table1 = new Table(3);// 创建表格
            Cell cell = new Cell(new Paragraph("嵌入的表一", FontChinese));// 创建单元格
            cell.setColspan(3);// 设置列跨度
            // 单元格添入到表格行满自动换行
            table1.addCell(cell);
            table1.addCell(new Paragraph("表一 0.0", FontChinese));
            table1.addCell(new Paragraph("表一 0.1", FontChinese));
            table1.addCell(new Paragraph("表一 0.2", FontChinese));
            table1.addCell(new Paragraph("表一 1.0", FontChinese));
            table1.addCell(new Paragraph("表一 1.1", FontChinese));
            table1.addCell(new Paragraph("表一 1.2", FontChinese));
            Table table2 = new Table(2);// 创建表格
            // 单元格添入到表格,行满自动换行
            table2.addCell(new Paragraph("表二 0.0", FontChinese));
            table2.addCell(new Paragraph("表二0.1", FontChinese));
            table2.addCell(new Paragraph("表二 1.0", FontChinese));
            table2.addCell(new Paragraph("表二 1.1", FontChinese));
            Cell tableCell = new Cell(new Paragraph("使用Cell嵌入的表二", FontChinese));// 创建一个单元格
            tableCell.add(table2);// 将表格添加到单元格
            Table table3 = new Table(5, 5);// 创建5行5列的原表
            table3.insertTable(table1); // 将第一个表格嵌入到原表中第一列
            // 单元格添入到表格行满自动换行
            table3.addCell(new Paragraph("原表1.1", font));
            table3.addCell(new Paragraph("原表1.2", font));
            table3.addCell(new Paragraph("原表1.3", font));
            table3.setPadding(5);// 设置填充值为5
            table3.addCell(tableCell);// 添加单元格,实现第二个表格的嵌入
            document.add(table3);// 向文档中添加原表
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例459  偏移表格
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\偏移表格.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font FontChinese = new Font(Chinese, 10, Font.NORMAL);// 实例化字体
            Table table = new Table(3);// 创建表格
            table.setBorderWidth(1);// 设置表格边框宽度
            table.setBorderColor(Color.blue);// 设置表格边框颜色
            table.setSpacing(5);// 设置表格与单元格的间距
            table.setPadding(5);// 设置单元格与内容的间距
            table.addCell("1.1");// 添加单元格
            table.addCell("1.2");
            table.addCell("1.3");
            document
                    .add(new Paragraph("                     原表格。", FontChinese));// 向文档添加内容
            document.add(table);// 向文档添加表格
            document.add(new Paragraph("                     默认的距离。",
                    FontChinese));
            document.add(table);
            document.add(new Paragraph("                     设置表格偏移值为0的距离。",
                    FontChinese));
            table.setOffset(0);// 设置表格偏移数值
            document.add(table);
            document.add(new Paragraph("                     设置表格偏移值为-15的距离。",
                    FontChinese));
            table.setOffset(-15);// 设置表格偏移数值
            document.add(table);
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

15.2  PdfPTable表格
实例460  创建表格
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\创建表格.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font FontChinese = new Font(Chinese, 12, Font.NORMAL);// 实例化字体
            PdfPTable table = new PdfPTable(3);// 创建表格
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("1.1");
            table.addCell("1.2");
            table.addCell("1.3");
            table.addCell("2.1");
            table.addCell("2.2");
            table.addCell("2.3");
            table.addCell("3.1");
            table.addCell("3.2");
            table.addCell("3.3");
            document.add(new Paragraph("                    这是一个3行3列的表格\n.",
                    FontChinese));// 向文档添加内容
            document.add(table);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例461  设置表格宽度
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置表格宽度.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            float[] widths = { 0.05f, 0.10f, 0.30f, 0.55f };// 设置列宽相关比率为5%,10%,30%,55%
            PdfPTable table = new PdfPTable(widths);// 创建表格关联列宽
            table.setWidthPercentage(60);// 为表格设置百分比宽度
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("5%");
            table.addCell("10%");
            table.addCell("30%");
            table.addCell("55%");
            table.addCell("w");
            table.addCell("1");
            table.addCell("2");
            table.addCell("3");
            table.addCell("h");
            table.addCell("1");
            table.addCell("2");
            table.addCell("3");
            table.addCell("s");
            table.addCell("1");
            table.addCell("2");
            table.addCell("3");
            document.add(table);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例462  设置表格对齐方式
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置表格对齐方式.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font FontChinese = new Font(Chinese, 10, Font.BOLD);// 实例化字体
            PdfPTable table = new PdfPTable(3);// 定义表格
            table.setTotalWidth(200);// 设置表格宽度为200
            table.setLockedWidth(true);
            PdfPCell cell = new PdfPCell(new Paragraph("new table colspan 3"));// 定义一个表格单元
            cell.setColspan(3);// 设置表格跨度
            table.addCell(cell);// 将单元加入到表格
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell("1.1");
            table.addCell("2.1");
            table.addCell("3.1");
            table.setHorizontalAlignment(Element.ALIGN_LEFT);// 设置水平对齐方式 居左
            
            PdfPTable table1 = new PdfPTable(3);// 定义表格
            table1.setTotalWidth(200);// 设置表格宽度为200
            table1.setLockedWidth(true);
            PdfPCell cell2 = new PdfPCell(new Paragraph("new table colspan 3"));// 定义一个表格单元
            cell2.setColspan(3);// 设置表格跨度
            table1.addCell(cell2);// 将单元加入到表格
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table1.addCell("1.1");
            table1.addCell("2.1");
            table1.addCell("3.1");
            table1.setHorizontalAlignment(Element.ALIGN_CENTER);// 设置水平对齐方式 居中
            PdfPTable table2 = new PdfPTable(3);// 定义表格
            table2.setTotalWidth(200);// 设置表格宽度为200
            table2.setLockedWidth(true);
            PdfPCell cell3 = new PdfPCell(new Paragraph("new table colspan 3"));// 定义一个表格单元
            cell3.setColspan(3);// 设置表格跨度
            table2.addCell(cell2);// 将单元加入到表格
            // 将单元格顺次的加入到表格,当一行充满时自动换行
            table2.addCell("1.1");
            table2.addCell("2.1");
            table2.addCell("3.1");
            table2.setHorizontalAlignment(Element.ALIGN_RIGHT);// 设置水平对齐方式 居右
            Paragraph p = new Paragraph("表格居左对齐\n\n", FontChinese);
            p.setAlignment(Element.ALIGN_LEFT);
            document.add(p);// 向文档添加内容
            document.add(table);// 将表格添加到文档
            Paragraph p2 = new Paragraph("表格居中对齐\n\n", FontChinese);
            p2.setAlignment(Element.ALIGN_CENTER);
            document.add(p2);// 向文档添加内容
            document.add(table1);// 将表格添加到文档
            Paragraph p3 = new Paragraph("表格居右对齐\n\n", FontChinese);
            p3.setAlignment(Element.ALIGN_RIGHT);
            document.add(p3);// 向文档添加内容
            document.add(table2);// 将表格添加到文档
            
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例463  设置表格的列宽
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置表格的列宽.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font FontChinese = new Font(Chinese, 10, Font.NORMAL,
                    new BaseColor(90, 90, 90));// 实例化字体
            float[] widths = { 0.05f, 0.10f, 0.30f, 0.55f };// 设置列宽相对比例为
                                                            // 5%,10%,30%,55%
            PdfPTable table = new PdfPTable(widths);// 创建表格关联列宽
            table.addCell(new Paragraph("列宽为5%", FontChinese));// 将单元格顺次的加入到表格,当一行充满时自动换行
            table.addCell(new Paragraph("列宽为10%", FontChinese));
            table.addCell(new Paragraph("列宽为30%", FontChinese));
            table.addCell(new Paragraph("列宽为55%", FontChinese));
            table.addCell("w");
            table.addCell("1");
            table.addCell("2");
            table.addCell("3");
            table.addCell("h");
            table.addCell("1");
            table.addCell("2");
            table.addCell("3");
            table.addCell("s");
            table.addCell("1");
            table.addCell("2");
            table.addCell("3");
            document.add(table);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例464  设置绝对宽度
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置绝对宽度.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font FontChinese = new Font(Chinese, 10, Font.NORMAL);// 实例化字体
            PdfPTable table1 = new PdfPTable(3);// 定义表格
            PdfPCell cell1 = new PdfPCell(new Paragraph("new table colspan 3"));// 定义一个表格单元
            cell1.setColspan(3);// 设置表格跨度
            table1.addCell(cell1);// 将单元加入到表格
            table1.addCell("1.1");// 将单元格顺次的加入到表格,当一行充满时自动换行
            table1.addCell("2.1");
            table1.addCell("3.1");
            table1.addCell("1.2");
            table1.addCell("2.2");
            table1.addCell("3.2");
            table1.addCell("1.3");
            table1.addCell("2.3");
            table1.addCell("3.3");
            PdfPTable table2 = new PdfPTable(3);// 定义表格
            PdfPCell cell2 = new PdfPCell(new Paragraph("new table colspan 3"));// 定义一个表格单元
            cell2.setColspan(3);// 设置表格跨度
            table2.addCell(cell2);// 将单元加入到表格
            table2.addCell("1.1");// 将单元格顺次的加入到表格,当一行充满时自动换行
            table2.addCell("2.1");
            table2.addCell("3.1");
            table2.addCell("1.2");
            table2.addCell("2.2");
            table2.addCell("3.2");
            table2.addCell("1.3");
            table2.addCell("2.3");
            table2.addCell("3.3");
            table2.setTotalWidth(200);// 设置表格宽度为200
            table2.setLockedWidth(true);// 锁定宽度
            document.add(new Paragraph("                  默认的表格\n\n", FontChinese));
            document.add(table1);// 将表格添加到文档
            document.add(new Paragraph("                  设置表格宽度200",
                    FontChinese));
            document.add(table2);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例465  嵌套表格
    public static void main(String[] args) {
        
        try {
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(
                    "c:\\Pdf嵌套表格.pdf"));
            document.open();// 打开文档
            PdfPTable table = new PdfPTable(4);// 创建表格对象
            PdfPTable table1 = new PdfPTable(2);// 创建表格对象
            table1.addCell("1.1");// 添加单元格内容
            table1.addCell("1.2");// 添加单元格内容
            PdfPTable table2 = new PdfPTable(1);// 创建表格对象
            table2.addCell("2.1");// 添加单元格内容
            table2.addCell("2.2");// 添加单元格内容
            table.addCell("table1");// 添加单元格内容
            table.addCell("table2");// 添加单元格内容
            table.addCell("text");// 添加单元格内容
            table.addCell("text");// 添加单元格内容
            table.addCell(table1);// 添加嵌套的表格
            table.addCell(table2);// 添加嵌套的表格
            table.addCell("cell");// 添加单元格内容
            table.addCell("cell");// 添加单元格内容
            document.add(table);// 将表格添加到文档中
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例466  在表格中添加图片
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\在表格中添加图片.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            Image image = Image.getInstance("image/picture.jpg");// 创建图像对象
            PdfPTable table = new PdfPTable(3);// 定义表格
            table.addCell("Text");// 添加单元格内容
            table.addCell("Picture");// 添加单元格内容
            table.addCell("Text");// 添加单元格内容
            table.addCell("This is a cell.");// 添加单元格内容
            table.addCell(image);// 向单元格中添加图像对象
            table.addCell("This is a cell.");// 添加单元格内容
            document.add(table);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例467  设置单元格的高度
    public static void main(String[] args) {
        
        try {
            Font font = FontFactory.getFont("COURIER", 10, Font.BOLD);// 定义一个字体
            Font xfont = FontFactory.getFont("HELVETICA", 10, Font.BOLD);// 定义一个字体
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置单元格的高度.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            PdfPTable table = new PdfPTable(2);// 定义表格
            table.getDefaultCell().setBackgroundColor(BaseColor.ORANGE);
            table.addCell(new Paragraph("default height", xfont));// 向表格添加单元格
            PdfPCell cell = new PdfPCell(new Paragraph("AAA", font));// 定义一个表格单元
            table.addCell(cell);// 向表格添加单元格
            table.addCell(new Paragraph("set height", xfont));
            PdfPCell cell2 = new PdfPCell(new Paragraph("ABC", font));// 定义单元格
            cell2.setFixedHeight(60);// 设置单元格高度为60
            table.addCell(cell2);// 将单元加入到表格
            table.addCell(new Paragraph("minimum height", xfont));
            PdfPCell cell3 = new PdfPCell(new Paragraph(
                    "A cat may look at a king.", font));// 定义单元格
            cell3.setMinimumHeight(40);// 设置单元格高度为40
            table.addCell(cell3);// 将单元加入到表格
            document.add(table);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例468  设置单元格的对齐方式
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置单元格的对齐方式.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            PdfPTable table = new PdfPTable(2);// 定义表格
            Paragraph p = new Paragraph("I think Bale will win");// 定义段落和内容
            table.addCell("alignment left");// 向单元格添加内容
            PdfPCell cell = new PdfPCell(p);// 定义单元格
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);// 设置单元格水平向左对齐
            table.addCell(cell);
            table.addCell("alignment right");// 向单元格添加内容
            PdfPCell cell1 = new PdfPCell(p);// 定义单元格
            cell1.setHorizontalAlignment(Element.ALIGN_RIGHT);// 设置单元格水平向右对齐
            table.addCell(cell1);
            table.addCell("alignment justified");// 向单元格添加内容
            PdfPCell cell2 = new PdfPCell(p);// 定义单元格
            cell2.setHorizontalAlignment(Element.ALIGN_JUSTIFIED);// 设置单元格为合理的对齐方式
            table.addCell(cell2);
            table.addCell("alignment center");// 向单元格添加内容
            PdfPCell cell3 = new PdfPCell(p);// 定义单元格
            cell3.setHorizontalAlignment(Element.ALIGN_CENTER);// 设置单元格水平向中间对齐
            table.addCell(cell3);
            document.add(table);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例469  设置单元格的填充和行间距
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\设置单元格的填充和行间距.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            PdfPTable table = new PdfPTable(2);// 定义表格
            table.addCell("no Padding");// 向单元格添加内容
            table.addCell("MingRiCompany MingRi MingRiCompany");// 单元格填充前内容
            table.addCell("Set Padding");// 向单元格添加内容
            table.getDefaultCell().setPadding(24);// 向单元格设置填充24
            table.addCell("MingRi MingRiCompany");// 单元格填充后内容
            document.add(table);// 将表格添加到文档
            PdfPTable table1 = new PdfPTable(2);// 定义表格
            table1.addCell("no Leading");// 向单元格添加内容
            table1
                    .addCell("MingRi MingRi MingRiCompanyMingRiCompany MingRiCompany");// 添加行间距前内容
            table1.getDefaultCell().setLeading(12, 1);// 添加行间距
            table1.addCell("Set Leading");// 向单元格添加内容
            table1
                    .addCell("MingRi MingRi MingRiCompanyMingRiCompany MingRiCompany");// 添加行间距后内容
            document.add(table1);// 将表格添加到文档
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例470  行优先分页
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter.getInstance(document, new FileOutputStream(
                    "C:\\行优先分页.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            String[] data = { "C033010", "MX", "980", "350", "800", "999",
                    "655", "800", "23", "860" };// 定义数据信息
            PdfPTable table = new PdfPTable(10);// 定义表格
            int columnwidths[] = { 8, 3, 11, 10, 8, 6, 8, 12, 3, 6 };// 定义列宽
            table.setWidths(columnwidths);// 向表格添加列宽
            table.setWidthPercentage(100);// 向表格添加绝对宽度
            table.getDefaultCell().setPadding(3);// 设置单元格填充为3
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);// 设置单元格居中对齐
            table.addCell("Number");// 将单元格内容顺次的加入到表格,当一行充满时自动换行
            table.addCell("Name");
            table.addCell("aggression");
            table.addCell("defend");
            table.addCell("reaction");
            table.addCell("shoot");
            table.addCell("header");
            table.addCell("bodybalance");
            table.addCell("age");
            table.addCell("speed");
            table.setHeaderRows(1);// 为表格每一页设置表头
            for (int i = 1; i < 100; i++) {// 循环向表格中添加100条记录
                if (i % 2 == 1) {
                    table.getDefaultCell().setBackgroundColor(
                            BaseColor.LIGHT_GRAY);// 填充颜色
                } else {
                    table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);// 填充颜色
                }
                for (int x = 0; x < 10; x++) {
                    String var = data[x];// 获得数组中的数据
                    for (int y = 0; y < i; y++) {
                        var += "\n" + y;// 连接字符串生成单元格内容
                    }
                    table.addCell(var);// 为单元格添加内容
                }
            }
            document.add(table);// 向文档添加表格
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        
    }

实例471  页优先分页
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("C:\\页优先分页.pdf"));// 关联文档对象与输出流    
            document.open();// 打开文档
            String[] data = { "C033010", "MX", "980", "350", "800", "999",
                    "655", "800", "23", "860" };// 定义数据信息
            PdfPTable table = new PdfPTable(10);// 定义表格
            int columnwidths[] = { 8, 3, 11, 10, 8, 6, 8, 12, 3, 6 };// 定义列宽
            table.setWidths(columnwidths);// 向表格添加列宽
            table.setWidthPercentage(100);// 向表格添加绝对宽度
            table.getDefaultCell().setPadding(3);// 设置单元格填充为3
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);// 设置单元格居中对齐
            table.addCell("Number");// 将单元格内容顺次的加入到表格,当一行充满时自动换行
            table.addCell("Name");
            table.addCell("aggression");
            table.addCell("defend");
            table.addCell("reaction");
            table.addCell("shoot");
            table.addCell("header");
            table.addCell("bodybalance");
            table.addCell("age");
            table.addCell("speed");
            table.setHeaderRows(1);// 为表格每一页设置表头
            for (int i = 1; i < 100; i++) {// 循环向表格中添加100条记录
                if (i % 2 == 1) {
                    table.getDefaultCell().setBackgroundColor(
                            BaseColor.LIGHT_GRAY);// 填充颜色
                } else {
                    table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);// 填充颜色
                }
                for (int x = 0; x < 10; x++) {
                    String var = data[x];// 获得数组中的数据
                    for (int y = 0; y < i; y++) {
                        var += "\n" + y;// 连接字符串生成单元格内容
                    }
                    table.addCell(var);// 为单元格添加内容
                }
            }
             table.setSplitLate(false);// 设置表格行以页优先的方式显示
             document.add(table);// 向文档添加表格
             document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        
    }

实例472  强行在一页显示
    public static void main(String[] args) {
        
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream("C:\\强行在一页显示(可能会丢失数据).pdf"));// 关联文档对象与输出流    
            document.open();// 打开文档
            String[] data = { "C033010", "MX", "980", "350", "800", "999",
                    "655", "800", "23", "860" };// 定义数据信息
            PdfPTable table = new PdfPTable(10);// 定义表格
            int columnwidths[] = { 8, 3, 11, 10, 8, 6, 8, 12, 3, 6 };// 定义列宽
            table.setWidths(columnwidths);// 向表格添加列宽
            table.setWidthPercentage(100);// 向表格添加绝对宽度
            table.getDefaultCell().setPadding(3);// 设置单元格填充为3
            table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);// 设置单元格居中对齐
            table.addCell("Number");// 将单元格内容顺次的加入到表格,当一行充满时自动换行
            table.addCell("Name");
            table.addCell("aggression");
            table.addCell("defend");
            table.addCell("reaction");
            table.addCell("shoot");
            table.addCell("header");
            table.addCell("bodybalance");
            table.addCell("age");
            table.addCell("speed");
            table.setHeaderRows(1);// 为表格每一页设置表头
            for (int i = 1; i < 100; i++) {// 循环向表格中添加100条记录
                if (i % 2 == 1) {
                    table.getDefaultCell().setBackgroundColor(
                            BaseColor.LIGHT_GRAY);// 填充颜色
                } else {
                    table.getDefaultCell().setBackgroundColor(BaseColor.WHITE);// 填充颜色
                }
                for (int x = 0; x < 10; x++) {
                    String var = data[x];// 获得数组中的数据
                    for (int y = 0; y < i; y++) {
                        var += "\n" + y;// 连接字符串生成单元格内容
                    }
                    table.addCell(var);// 为单元格添加内容
                }
            }
             table.setSplitRows(false);// 使行强行在一页显示,但是可能会丢失数据
             document.add(table);// 向文档添加表格
             document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        
    }

实例473  绝对定位表格
    public static void main(String[] args) {
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("c:\\绝对定位表格.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            float[] columnSize = { 21F, 21F, 21F };// 设置列宽
            PdfPTable table = null;
            PdfPCell cell = null;
            table = new PdfPTable(columnSize);// 定义新表格
            table.getDefaultCell().setBorder(1);// 设置表格边框宽度
            table.setHorizontalAlignment(Element.ALIGN_CENTER);// 设置居中对齐
            table.setTotalWidth(500); // 设置总宽度500
            table.setLockedWidth(true); // 解锁
            cell = new PdfPCell(new Phrase("Add table"));// 定义单元格
            cell.setColspan(3);// 设置单元格跨度3
            table.addCell(cell);// 向表格添加单元格
            table.addCell(new PdfPCell(new Phrase("Add 001")));// 向表格添加内容
            table.addCell(new PdfPCell(new Phrase("Add 002")));// 向表格添加内容
            table.addCell(new PdfPCell(new Phrase("Add 003")));// 向表格添加内容
            document.add(table);// 向文档添加表格
            table = new PdfPTable(columnSize);
            // 定义新表格
            table.getDefaultCell().setBorder(1);// 设置表格边框宽度
            table.setHorizontalAlignment(Element.ALIGN_CENTER);// 设置居中对齐
            table.setTotalWidth(500);// 设置总宽度500
            table.setLockedWidth(true);// 解锁
            cell = new PdfPCell(new Phrase("Table writeSelectedRows"));// 定义单元格
            cell.setColspan(columnSize.length);// 设置单元格跨度3
            table.addCell(cell); // 向表格添加单元格
            table.addCell(new PdfPCell(new Phrase("Add 004")));// 向表格添加内容
            table.addCell(new PdfPCell(new Phrase("Add 005")));// 向表格添加内容
            table.addCell(new PdfPCell(new Phrase("Add 006")));// 向表格添加内容
            table.writeSelectedRows(0, 2, 50, 750, writer.getDirectContent());// 在指定位置添加表格内容
            document.close();// 关闭文档
        } catch (DocumentException de) {
        } catch (IOException ioe) {
            
        }
    }

实例474  大表格的内存处理
    public static void main(String args[]) {
        int bigtablesize = 5;
        Document document = new Document();// 创建文档对象
        try {
            PdfWriter.getInstance(document, new FileOutputStream(
                    "c:\\大表格的内存处理.pdf"));// 关联文档对象与输出流
            document.open();// 打开文档
            
            BaseFont chinese = BaseFont.createFont("STSong-Light",
                    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体
            Font fontChinese1 = new Font(chinese, 10, Font.NORMAL,
                    new BaseColor(90, 90, 90));// 实例化字体类与设置字体大小属性
            Font fontChinese2 = new Font(chinese, 15, Font.NORMAL,
                    BaseColor.BLUE);// 实例化字体类与设置字体大小属性
            document.add(new Paragraph("大表格的内存管理\n\n", fontChinese2));
            float[] hw = { 0.1f, 0.2f, 0.1f, 0.2f, 0.1f, 0.3f };// 设置列宽
            PdfPTable table = new PdfPTable(hw);// 创建表格
            table.setHeaderRows(2);// 设置头排
            table.addCell("10%");// 将单元格内容顺次的加入到表格,当一行充满时自动换行
            table.addCell("20%");
            table.addCell("10%");
            table.addCell("20%");
            table.addCell("10%");
            table.addCell("30%");
            for (int i = 1; i <= 500; i++) {// 循环向表格中添加500条记录
                if (i % bigtablesize == 4) {// 求余
                    document.add(table);// 向文档添加表格
                    table.deleteBodyRows();// 删除多余行
                    table.setSkipFirstHeader(true);// 使表头始终保持在首行
                }
                PdfPCell cell0 = new PdfPCell(new Paragraph(String.valueOf(i),
                        fontChinese1));// 向单元格添加内容
                table.addCell(cell0);// 向表格添加单元格
                PdfPCell cell1 = new PdfPCell(new Paragraph("明日科技",
                        fontChinese1));// 向单元格添加内容
                table.addCell(cell1);// 向表格添加单元格
                PdfPCell cell2 = new PdfPCell(new Paragraph(String.valueOf(i),
                        fontChinese1));// 向单元格添加内容
                table.addCell(cell2);// 向表格添加单元格
                PdfPCell cell3 = new PdfPCell(new Paragraph("明日科技",
                        fontChinese1));// 向单元格添加内容
                table.addCell(cell3);// 向表格添加单元格
            }
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

第16章  设置阅读器参数
16.1  设置页面参数
实例475  只显示一个页面
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\只显示一个页面.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.PageLayoutSinglePage);// 设置阅读器只显示一个页面
            document.open();// 打开文档
            document.add(new Paragraph("PageLayoutSinglePage 1"));// 向文档中添加内容
            document.newPage();// 添加新页
            document.add(new Paragraph("PageLayoutSinglePage 2"));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }        
    }

实例476  单列显示
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\单列显示.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.PageLayoutOneColumn); // 设置阅读器单列显示
            document.open();// 打开文档
            document.add(new Paragraph("ShowOneColumn Page 1."));// 向文档中添加内容
            document.newPage();// 增加新页
            document.add(new Paragraph("ShowOneColumn Page 2."));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        
    }

实例477  双列显示奇页在左
    public static void main(String args[]) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\双列显示奇页在左.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.PageLayoutTwoColumnLeft);// 设置阅读器双列显示奇页在左
            document.open();// 打开文档
            document.add(new Paragraph("This is Odd Page 1"));// 向文档中添加内容
            document.newPage();// 新增第二页
            document.add(new Paragraph("this is Even Page 2"));// 向文档中添加内容
            document.newPage();// 新增第三页
            document.add(new Paragraph("This is Odd Page 3"));// 向文档中添加内容
            document.newPage();// 新增第四页
            document.add(new Paragraph("This is Even Page 4"));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例478  双列显示奇页在右
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\双列显示奇页在右.pdf"));// 关联文档与输出流
            writer.setViewerPreferences(PdfWriter.PageLayoutTwoColumnRight);// 设置阅读器双列显示奇页在左
            document.open();// 打开文档
            document.add(new Paragraph("This is Odd Page 1"));// 向文档中添加内容
            document.newPage();// 新增第二页
            document.add(new Paragraph("this is Even Page 2"));// 向文档中添加内容
            document.newPage();// 新增第三页
            document.add(new Paragraph("This is Odd Page 3"));// 向文档中添加内容
            document.newPage();// 新增第四页
            document.add(new Paragraph("This is Even Page 4"));// 向文档中添加内容
            document.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }        
    }

实例479  显示大纲
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\显示大纲.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.PageModeUseOutlines);// 设置阅读器显示大纲
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体   
            Font fontChinese1 = new Font(Chinese, 18, Font.BOLDITALIC,BaseColor.RED);// 实例化字体类、设置字体大小和颜色
            Font fontChinese2 = new Font(Chinese, 15, Font.BOLDITALIC,BaseColor.BLUE);// 实例化字体类、设置字体大小和颜色
            Paragraph paragraph = new Paragraph("章节",fontChinese1);// 创建段落对象
            Chapter chapter = new Chapter(paragraph,1);// 创建章节对象
            paragraph = new Paragraph("小节一",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            paragraph = new Paragraph("小节二",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            document.add(chapter);// 向文档添加章节
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }

实例480  显示缩略图
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\显示缩略图.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.PageModeUseThumbs);// 设置阅读器显示缩略图
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体   
            Font fontChinese1 = new Font(Chinese, 18, Font.BOLDITALIC,BaseColor.RED);// 实例化字体类、设置字体大小和颜色
            Font fontChinese2 = new Font(Chinese, 15, Font.BOLDITALIC,BaseColor.BLUE);// 实例化字体类、设置字体大小和颜色
            Paragraph paragraph = new Paragraph("章节",fontChinese1);// 创建段落对象
            Chapter chapter = new Chapter(paragraph,1);// 创建章节对象
            paragraph = new Paragraph("小节一",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            paragraph = new Paragraph("小节二",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            document.add(chapter);// 向文档添加章节
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }

实例481  不显示大纲和缩略图
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\不显示大纲和缩略图.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.PageModeUseNone);// 设置阅读器不显示大纲和缩略图
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体   
            Font fontChinese1 = new Font(Chinese, 18, Font.BOLDITALIC,BaseColor.RED);// 实例化字体类、设置字体大小和颜色
            Font fontChinese2 = new Font(Chinese, 15, Font.BOLDITALIC,BaseColor.BLUE);// 实例化字体类、设置字体大小和颜色
            Paragraph paragraph = new Paragraph("章节",fontChinese1);// 创建段落对象
            Chapter chapter = new Chapter(paragraph,1);// 创建章节对象
            paragraph = new Paragraph("小节一",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            paragraph = new Paragraph("小节二",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            document.add(chapter);// 向文档添加章节
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }

实例482  全屏显示
    public static void main(String args[]) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\全屏显示.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.PageModeFullScreen);// 设置阅读器以全屏模式显示
            document.open();// 打开文档
            document.add(new Paragraph("PageModelFullScreen."));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

16.2  设置工具栏和全屏模式参数
实例483  显示和隐藏工具栏
    public static void main(String args[]) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\显示和隐藏工具栏.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.HideToolbar);// 设置阅读器隐藏工具栏
            document.open();// 打开文档
            document.add(new Paragraph("HideToolbar."));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例484  显示和隐藏菜单
    public static void main(String args[]) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\显示和隐藏菜单.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.HideMenubar);// 设置阅读器隐藏菜单
            document.open();// 打开文档
            document.add(new Paragraph("HideMenubar."));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例485  显示和隐藏页面元素
    public static void main(String args[]) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\显示和隐藏页面元素.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.HideWindowUI);// 设置阅读器隐藏界面元素
            document.open();// 打开文档
            for (int i = 1; i <= 100; i++) {
                document.add(new Paragraph("HideWindowUI Row " + i));// 向文档中添加段落内容
            }
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例486  使文档窗口适合显示第一页
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\使文档窗口适合显示第一页.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.FitWindow);// 设置阅读器适合显示第一页
            document.open();// 打开文档
            for (int i = 1; i <= 100; i++) {
                document.add(new Paragraph("Fit show Page 1. row " + i));// 向文档中添加段落内容
            }
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

实例487  在屏幕中央显示文档窗口
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\在屏幕中央显示文档窗口.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.CenterWindow);// 设置阅读器在屏幕中央显示
            document.open();// 打开文档
            document.add(new Paragraph("ShowCenterWindow"));// 向文档中添加内容
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }        
    }

实例488  全屏模式下显示大纲
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\全屏模式下显示大纲.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.PageModeFullScreen);// 设置为全屏模式
            writer
                    .setViewerPreferences(PdfWriter.NonFullScreenPageModeUseOutlines);// 设置阅读器在全屏模式下显示大纲
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体   
            Font fontChinese1 = new Font(Chinese, 18, Font.BOLDITALIC,BaseColor.RED);// 实例化字体类、设置字体大小和颜色
            Font fontChinese2 = new Font(Chinese, 15, Font.BOLDITALIC,BaseColor.BLUE);// 实例化字体类、设置字体大小和颜色
            Paragraph paragraph = new Paragraph("章节",fontChinese1);// 创建段落对象
            Chapter chapter = new Chapter(paragraph,1);// 创建章节对象
            paragraph = new Paragraph("小节一",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            paragraph = new Paragraph("小节二",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            document.add(chapter);// 向文档添加章节
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }        
    }

实例489  全屏模式下显示缩略图
    public static void main(String args[]) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\全屏模式下显示缩略图.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.PageModeFullScreen);// 设置为全屏模式
            writer
                    .setViewerPreferences(PdfWriter.NonFullScreenPageModeUseThumbs);// 设置阅读器在全屏模式下显示缩略图
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体   
            Font fontChinese1 = new Font(Chinese, 18, Font.BOLDITALIC,BaseColor.RED);// 实例化字体类、设置字体大小和颜色
            Font fontChinese2 = new Font(Chinese, 15, Font.BOLDITALIC,BaseColor.BLUE);// 实例化字体类、设置字体大小和颜色
            Paragraph paragraph = new Paragraph("章节",fontChinese1);// 创建段落对象
            Chapter chapter = new Chapter(paragraph,1);// 创建章节对象
            paragraph = new Paragraph("小节一",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            paragraph = new Paragraph("小节二",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            document.add(chapter);// 向文档添加章节
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

实例490  全屏模式下不显示大纲和缩略图
    public static void main(String[] args) {
        
        try {
            Document document = new Document();// 创建文档对象
            PdfWriter writer = PdfWriter.getInstance(document,
                    new FileOutputStream("C:\\全屏模式下不显示大纲和缩略图.pdf"));// 关联文档对象与输出流
            writer.setViewerPreferences(PdfWriter.PageModeFullScreen);// 设置为全屏模式
            writer.setViewerPreferences(PdfWriter.NonFullScreenPageModeUseNone);// 设置全屏模式下阅读器不显示大纲和缩略图
            document.open();// 打开文档
            BaseFont Chinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 定义基础字体   
            Font fontChinese1 = new Font(Chinese, 18, Font.BOLDITALIC,BaseColor.RED);// 实例化字体类、设置字体大小和颜色
            Font fontChinese2 = new Font(Chinese, 15, Font.BOLDITALIC,BaseColor.BLUE);// 实例化字体类、设置字体大小和颜色
            Paragraph paragraph = new Paragraph("章节",fontChinese1);// 创建段落对象
            Chapter chapter = new Chapter(paragraph,1);// 创建章节对象
            paragraph = new Paragraph("小节一",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            paragraph = new Paragraph("小节二",fontChinese2);// 创建段落对象
            chapter.addSection(paragraph);// 添加小节
            document.add(chapter);// 向文档添加章节
            document.close();// 关闭文档
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

 

转载于:https://my.oschina.net/MoreYoungGavin/blog/1083426

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/chouzhang8835/article/details/100871096

智能推荐

整数转换成固定位数的字符串_java固定位数字符串-程序员宅基地

文章浏览阅读3.6k次,点赞2次,收藏4次。int youNumber = 1; // 0 代表前面补充0 // 4 代表长度为4 // d 代表参数为正数型 String str = String.format("%04d", youNumber); System.out.println(str); // 0001_java固定位数字符串

elementUI树形组件el-tree添加层级虚线,指示线_element-plus tree前边的层级线展示-程序员宅基地

文章浏览阅读3.5k次。看实现效果element的官网由提供一个属性 indent ,该属性可以控制相邻级节点间的水平缩进,单位为像素。我们可以将该属性设置为0,然后我们在css中为所有 el-tree-node 设置 padding-left ,就可以达到缩进的效果了!由于每一层 el-tree-node 都被上一层的 el-tree-node 包裹着,上一层的padding会影响到下层的div位置,所以每一层的节点就不会撑满整个树形大盒子了,现在就可以直接根据 el-tree-node 定位伪元素了。实现代码<_element-plus tree前边的层级线展示

Windows下超详细安装Anaconda3以及jupyter notebook_下载anaconda后还要下载jupyter-程序员宅基地

文章浏览阅读3.3k次。1.下载Anaconda安装包Anaconda官网网站:https://www.anaconda.com/products/individual下载安装包速度比较慢,国内小伙伴不建议尝试。我们可以使用清华大学开源软件镜像站:清华大学开源软件镜像站:https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/,请选择适合自己系统自行下载。下载完成后如下图:1.安装Anaconda安装包1.打开安装包,点击Next2.点击I Agre_下载anaconda后还要下载jupyter

chromium源码下载并编译_chromium git仓库下载-程序员宅基地

文章浏览阅读1.7w次。目前,chromium的源代码已经全部迁移到了git管理,所以本文主要梳理一下下载chromium源代码的过程。一、概况chromium现在分为四个大分支:canary:最前沿的版本,每天都发布,bug一堆,不适合新人dev:每两个星期发布,比较稳定,可以体验新功能和新特性deta:每周小版本更新,六周大版本更新。比dev稳定,比同期dev版小1个版本号,基本上是stable的待发版本sta_chromium git仓库下载

AXI4协议详解(一)_关于axi4描述正确的是-程序员宅基地

文章浏览阅读6.2k次,点赞12次,收藏81次。写在前面:AMBA总线协议在SOC设计中的地位是不言而喻的,从本篇文章开始,将会依托于官方手册详解AXI4、AXI4-LITE、AHB、APB等目前最常见的AMBA总线协议,梳理一下其中常用的知识点。本篇为AXI4协议的开篇,更多解析将会在之后继续分享给大家。AXI4协议是amba协议中比较新的一个协议,目前应用的也很广泛。例如在ZYNQ核的ARM与FPGA部分信息交互中就大量应用了AXI4总线协议,官方手册给出了以下几点的AXI协议特点:地址/控制信号与数据处于不同阶段支持非对齐数据传输支持._关于axi4描述正确的是

element ui table表头样式修改_header-row-class-name-程序员宅基地

文章浏览阅读3.6k次。网上找了不少方法,都不行,可能是因为版本的问题,下面直接写成可以成功的方法吧:给表格一个代表表头的类header-row-class-name <el-table ref="multipleTable" :data="tableData" border tooltip-effect="dark" style="width: 100%" header-row-class-name_header-row-class-name

随便推点

区块链学习笔记:DAY05 如何使用公有云区块链服务-程序员宅基地

文章浏览阅读1.3k次。这是最后一节课了,主要讲华为云在云区块链提供的服务,如何基于华为云BCS来构建应用先来个简单的比喻:1.有关BaaS的范围定义包含物理主机、虚拟主机、容器服务、区块链、智能合约和服务2.华为云区块链服务BCS3.华为云BCS特点:1)易用:一键部署,区块链系统全生命周期管理2.易用:可视化智能合约生命周期管理,多语言支持3)高可用:成员动态准入,..._公有云区块链服务

C语言-利用泰勒公式计算sinx的值 (不调用库函数,自己定义函数)_利用泰勒级数sinxc语言-程序员宅基地

文章浏览阅读5.1k次,点赞5次,收藏37次。#include<stdio.h>#include <math.h>//先定义一个阶乘函数double fac(double n){ double f=0; if(n == 1 || n==0) f =1; else f = fac(n-1)*n; return f;}//一个指数函数(次方)double kvat(double n,double ci){ double chengf=1; _利用泰勒级数sinxc语言

GCN-tensorflow2.0代码实现_keras gcn tensorflow2-程序员宅基地

文章浏览阅读1.2k次。文章目录代码代码定义图卷积层import tensorflow as tffrom tensorflow.keras import activations, regularizers, constraints, initializersclass GCNConv(tf.keras.layers.Layer): def __init__(self, units, activation=lambda x: x, _keras gcn tensorflow2

k30s刷鸿蒙系统,Redmi K30S至尊版喜提MIUI 12.5稳定版-程序员宅基地

文章浏览阅读3.3k次。Redmi K30S至尊版喜提MIUI 12.5稳定版2021-05-07 13:20:0516点赞18收藏104评论进入5月,第二批升级MIUI 12.5的机型也陆续开启推送,在五一小长假期间包括Redmi K40标准版、小米CC9等机型均已收到MIUI 12.5稳定版的推送升级。今日,根据网友反馈,号称Redmi唯一的骁龙865 LCD旗舰——Redmi K30S至尊纪念版也在今日下午收到了M..._k30s刷鸿蒙

如何使用Hyper-V Manager和Powershell合并Hyper-V检查点-程序员宅基地

文章浏览阅读5.9k次,点赞2次,收藏4次。如何使用Hyper-V Manager和Powershell合并Hyper-V检查点Hyper-V检查点在Hyper-V虚拟机的整个生命周期管理中起着关键作用,因为它们可以在出现问题时通过有效的回滚机制有效地管理和执行软件更新,升级和安装。在最新版本的Hyper-V中,Microsoft增强了检查点功能并将其添加到更强大的功能中,包括生产检查点以及利用了ReFS文件系统的优势..._hyper-v manager

Php7.1 编译安装_pear package php_archive not installed: generated -程序员宅基地

文章浏览阅读1.4w次。EnvironmentCentOS Linux release 7.5.1804 (Core)Process获取 Php7.1 官方安装包wget -c http://cn2.php.net/get/php-7.1.27.tar.gz/from/this/mirror解压安装包tar -xzvf mirror查看配置项./configure --help配置..._pear package php_archive not installed: generated phar will require php's ph

推荐文章

热门文章

相关标签