`
Donald_Draper
  • 浏览: 972096 次
社区版块
存档分类
最新评论

iText无内边框表格

 
阅读更多
package test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import test.CheckboxCell2.CheckboxCellEvent;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.ExceptionConverter;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfAnnotation;
import com.itextpdf.text.pdf.PdfAppearance;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfFormField;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RadioCheckField;

public class TestCheckBox {
	public static final String DEST = "E:\\testCheck.pdf";
	 
    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TestCheckBox().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        //定义字体
        BaseFont bfChinese = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H", false );  
		Font fNormal = new Font(bfChinese, 11, Font.NORMAL);
		//新建Table1列
        PdfPTable table = new PdfPTable(1);
        //定义每列的宽度
        table.setWidths(new int[]{240});

        //新建table t1 5 列
        PdfPTable t1 = new PdfPTable(5);
        t1.setWidths(new int[]{60,30,60,30,60});
        t1.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        
        PdfPCell head = new PdfPCell(new Phrase("状态:",fNormal));
        head.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        head.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        head.setBorder(PdfPCell.NO_BORDER);
        head.setMinimumHeight(30);
        t1.addCell(head);
/*        head.setBorderWidthRight(0f);
        head.setBorderColorRight(BaseColor.WHITE);
        head.setBorder(Rectangle.BOTTOM | Rectangle.LEFT | Rectangle.TOP );*/
       
//        head.setCellEvent(new BorderCell(3));
//        table.addCell(head);
        PdfPCell c0 = new PdfPCell();
        c0.setCellEvent(new CheckboxCellEvent("1", true));
        c0.setFixedHeight(10);
        c0.setBorder(PdfPCell.NO_BORDER);
        c0.setPadding(10);
       
        
        PdfPCell c1 = new PdfPCell(c0);
//        c1.setIndent(15);
//        c1.setPadding(10);
        c1.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        c1.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        c1.setBorder(PdfPCell.NO_BORDER);
        t1.addCell(c1);
        
        PdfPCell c11 = new PdfPCell(new Phrase("是",fNormal));
        c11.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        c11.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        c11.setMinimumHeight(30);
        c11.setBorder(PdfPCell.NO_BORDER);
        t1.addCell(c11);
        
        PdfPCell c2 = new PdfPCell();
        c2.setCellEvent(new CheckboxCellEvent("0", false));
        c2.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        c2.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        c2.setBorder(PdfPCell.NO_BORDER);
        c2.setFixedHeight(10);
        c2.setMinimumHeight(30);
        t1.addCell(c2);
        
        PdfPCell c21 = new PdfPCell(new Phrase("否",fNormal));
        c21.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
        c21.setVerticalAlignment(PdfPCell.ALIGN_MIDDLE);
        c21.setBorder(PdfPCell.NO_BORDER);
        c21.setMinimumHeight(30);
        t1.addCell(c21);
        //将t1嵌入到 列中
        PdfPCell checkbox = new PdfPCell(t1);
        table.addCell(checkbox);
        document.add(table);
        document.close();
        System.out.println("===========:end");
    }
 
    class CheckboxCellEvent implements PdfPCellEvent {
        // The name of the check box field
        protected String name;
        protected boolean flag ;
        // We create a cell event
        public CheckboxCellEvent(String name, boolean flag) {
            this.name = name;
            this.flag = flag;
        }
        // We create and add the check box field
        public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
            PdfWriter writer = canvases[0].getPdfWriter(); 
            // define the coordinates of the middle
            float x = (position.getLeft() + position.getRight()) / 2;
            float y = (position.getTop() + position.getBottom()) / 2;
            // define the position of a check box that measures 20 by 20
            //画勾
            Rectangle rect = new Rectangle(x - 5, y - 5, x + 5, y + 5);
            RadioCheckField checkbox = new RadioCheckField(writer, rect, name, "On");
            checkbox.setCheckType(RadioCheckField.TYPE_CHECK);
            
            if(flag){
            	//设置为选中状态
            	checkbox.setChecked(true);
            }
            else{
            	checkbox.setChecked(false);
            }
            //画框
            PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
            canvas.setColorStroke(BaseColor.BLACK);
            canvas.setLineDash(1f);
            canvas.rectangle(x - 10, y - 10, 20,20);
            canvas.stroke();
           
            try {
                writer.addAnnotation(checkbox.getCheckField());
            } catch (Exception e) {
                throw new ExceptionConverter(e);
            }
        }
    }
    class BorderCell implements PdfPCellEvent {
        private int border = 0;
        public BorderCell(int border) {
            this.border = border;
        }
        public void cellLayout(PdfPCell cell, Rectangle position,
            PdfContentByte[] canvases) {
            PdfContentByte canvas = canvases[PdfPTable.LINECANVAS];
            canvas.saveState();
            canvas.setLineDash(0, 4, 2);
            if ((border & PdfPCell.TOP) == PdfPCell.TOP) {
                canvas.moveTo(position.getRight(), position.getTop());
                canvas.lineTo(position.getLeft(), position.getTop());
            }
            if ((border & PdfPCell.BOTTOM) == PdfPCell.BOTTOM) {
                canvas.moveTo(position.getRight(), position.getBottom());
                canvas.lineTo(position.getLeft(), position.getBottom());
            }
            if ((border & PdfPCell.RIGHT) == PdfPCell.RIGHT) {
                canvas.moveTo(position.getRight(), position.getTop());
                canvas.lineTo(position.getRight(), position.getBottom());
            }
            if ((border & PdfPCell.LEFT) == PdfPCell.LEFT) {
                canvas.moveTo(position.getLeft(), position.getTop());
                canvas.lineTo(position.getLeft(), position.getBottom());
            }
            canvas.stroke();
            canvas.restoreState();
        }
    }
}


结果如下:


  • 大小: 1.5 KB
分享到:
评论

相关推荐

    Itext输出复杂PDF表格样式参数外部配置化

    本文将深入探讨如何使用Itext来输出复杂的PDF表格,并实现样式参数的外部配置化,以提高代码的可维护性和灵活性。 首先,`Itext`库提供了丰富的API来构建PDF表格。`PdfPTable`类是核心,它允许我们定义表格的列数、...

    iText输出pdf表格

    `PdfPTable`允许我们定义表格的列数、行数以及单元格的样式,如边框、填充色、字体样式等。例如,以下代码展示了如何创建一个简单的两列表格: ```java // 创建一个2列的表格 PdfPTable table = new PdfPTable(2); ...

    itext生成pdf文件-表格

    本文将深入探讨iText如何生成包含表格的PDF文件,同时结合提供的“itext教程_itext的使用方法_iava使用itext实现pdh的输出.doc”文档,为您详细解析这一过程。 首先,我们需要了解iText的基本用法。iText是用Java...

    java itext pdf word 中文 表格 图片

    `PdfPTable`类是核心,你可以创建一个表格对象,设置列宽,添加行和单元格,甚至调整单元格的对齐方式和边框样式。对于复杂的数据展示,表格是一个很好的选择。 在文档中插入图片则需要用到`Image`类。你可以通过`...

    Itext生成带表格,图片的word文档代码,里面包含需要的jar包

    本篇文章将深入探讨如何使用Itext库生成带有表格和图片的Word文档,并提供一个基于描述中的"Demo"文件的示例代码。 首先,我们需要了解Itext库的基本用法。Itext主要通过`Document`对象来构建文档结构,通过`...

    Java 使用iText生成word文档,有表格,图片,文本有颜色

    在这个场景中,我们将探讨如何利用iText来生成包含表格、图片以及带有颜色的文本的Word文档。 首先,要使用iText生成Word文档,你需要在项目中引入iText的库。iText提供了一个名为iText-for-Office的模块,专门用于...

    itext往word里插入图片和画表格

    在这个场景中,我们将探讨如何使用iText在Word文档中插入图片和绘制表格。 首先,理解一个关键概念:iText本身并不直接支持Word格式。然而,由于Word文档可以被转换为或从OpenXML格式解析,我们可以通过处理这些XML...

    itext7 根据pdf模板填充图像框

    ### itext7根据PDF模板填充图像框的知识点详解 #### 一、背景介绍 在实际工作中,经常需要根据模板批量生成PDF文档,特别是在人事管理、财务报表等领域。使用itext7进行PDF文档处理是一个非常实用的选择。itext7是...

    itext操作word生成目录、页码、表格、插入图片

    本篇将深入探讨如何使用iText来实现Word文档中的目录生成、页码设置、表格创建以及图片插入等功能。 首先,我们需要理解的是,由于iText主要用于PDF,所以在操作Word时,我们通常会借助于像Apache POI这样的库来...

    初学Itext 生成PDF 表格,条形码(一维),图片

    例如,你可以设置表格的宽度、边框、对齐方式等。表头的设置也十分方便,可以简单地区分表格中的标题行和数据行。开发者还可以在表格中插入各种格式的数据,比如文本、图片等,并且可以控制单元格的合并。 接下来是...

    itextpdf+itext-asian+xmlworker 导出pdf 表格 自动分页中文 目录.rar

    在表格中,如果表格太大无法在一页内完全显示,iText会自动进行跨页处理,确保表格的完整性和可读性。 5. **目录生成**:生成PDF目录可以帮助读者快速定位文档的重要部分。iText提供了一套API来创建和维护PDF的书签...

    Itext应用封装(二)_生成表格

    在这个主题“iText应用封装(二)_生成表格”中,我们将深入探讨如何使用iText来创建和操作PDF文档中的表格。这个博客文章可能详细介绍了在实际项目中如何高效地封装iText的功能,以实现表格的自定义生成。 首先,...

    iText笔记

    这篇笔记主要探讨了如何使用iText创建和操作表格。在给定的代码示例中,我们看到一个简单的Java程序,该程序展示了如何使用iText库创建一个包含多个单元格的PDF表格。 首先,程序创建了一个`Document`对象,这是...

    itext7jar包

    - **表格处理**:创建和编辑表格,包括合并单元格、设置边框、调整列宽等。 - **链接和书签**:添加超链接和书签,方便用户跳转至文档的特定位置。 - **表单处理**:创建交互式PDF表单,收集用户数据。 - **安全和...

    导出PDF插件(表格没有线框)

    - 检查插件设置:确保在导出选项中选择了显示表格边框,或者调整边框样式和宽度设置。 - 更新插件:尝试更新到最新版本,修复可能存在的bug。 - 替换工具:如果当前插件无法解决问题,可以考虑使用其他可靠的PDF...

    itextpdf-5.5.5.jar

    2. **表格处理**:iTextPDF中的` PdfPTable`类提供了丰富的功能,可以创建多列或多行的表格,调整列宽、行高,以及设置单元格的边框、填充和对齐方式。此外,表格中的数据可以来源于数据库或Excel文件(如`jxl.jar`...

    itext-2.1.7.jar和itext-rtf-2.1.7.jar

    4. **表格处理**:iText提供了创建和操作表格的功能,包括添加行、列,合并单元格,以及设置边框和填充色。 5. **交互元素**:iText允许添加链接、按钮、表单字段和其他交互元素到PDF中,增强用户与文档的互动性。 ...

    itext相关jar包

    通过使用这个库,开发者可以自定义Word文档的样式,包括字体、颜色、大小、对齐方式、边框、背景色等。此外,还可以插入图片、创建复杂的表格、添加页眉页脚、设置页码、应用样式模板等。IText提供了API来实现这些...

Global site tag (gtag.js) - Google Analytics