`
lzth
  • 浏览: 141010 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

itext学习之表格pdftable (转载)

    博客分类:
  • Java
阅读更多
一个最基本的PdfPTable的例子



package com.itext.test;



import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;



import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;



/**
* 一个非常简单的PdfPTable的例子.
*/
public class MyFirstTable {



/**
  A very simple PdfPTable example.
 
  @param args
              no arguments needed
  */
public static void main(String[] args) {



  System.out.println("My First PdfPTable");



  // 步骤 1: 创建一个document对象
  Document document = new Document();



  try {
   // 步骤 2:
   // 我们为document创建一个监听,并把PDF流写到文件中
   PdfWriter.getInstance(document, new FileOutputStream("c:\\MyFirstTable.pdf"));



   // 步骤 3:打开文档
   document.open();
   //创建一个有3列的表格
   PdfPTable table = new PdfPTable(3);
   //定义一个表格单元
   PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
   //定义一个表格单元的跨度
   cell.setColspan(3);
   //把单元加到表格中
   table.addCell(cell);
   //把下面这9项顺次的加入到表格中,当一行充满时候自动折行到下一行
   table.addCell("1.1");
   table.addCell("2.1");
   table.addCell("3.1");
   table.addCell("1.2");
   table.addCell("2.2");
   table.addCell("3.2");
   table.addCell("1.3");
   table.addCell("2.3");
   table.addCell("3.3");
   //重新定义单元格
   cell = new PdfPCell(new Paragraph("cell test1"));
   //定义单元格的框颜色
   cell.setBorderColor(new Color(255, 0, 0));
   //把单元格加到表格上,默认为一个单元
   table.addCell(cell);
   //重新定义单元格
   cell = new PdfPCell(new Paragraph("cell test2"));
   //定义单元格的跨度
   cell.setColspan(2);
   //定义单元格的背景颜色
   cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
   //增加到表格上
   table.addCell(cell);
   //增加到文档中
   document.add(table);
  } catch (DocumentException de) {
   System.err.println(de.getMessage());
  } catch (IOException ioe) {
   System.err.println(ioe.getMessage());
  }



  // 步骤 5:关闭文档
  document.close();
}
}



看完这个例子,又看过我第一天记录的朋友一定会问为什么不用Table,我在这里解释一下。



PdfPTable is a very powerful and flexible object, but for some specific needs, you can also use one of the alternatives for PdfPTable. If you have a Swing application with JTables, you can look at the JTable2Pdf section. PdfPTable only works for generating PDF. If you need to generate HTML or RTF, you need the (no longer supported) Table object.



上面这句话来之---iText的 tutorial,你应该明白了吧。







If you add a PdfPTable with Document.add(), the default width of the table is 80 percent of the available space and the table is aligned in the center. You can change these defaults with setWidthPercentage and setHorizontalAlignment.







下面就讲一个可以自己定义表格宽度和对齐方式的例子:



package com.itext.test;



import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;



import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;



/**
* 改变复杂表格的宽度和对齐方式.
*/
public class TableWidthAlignment {



/**
  Changing the width and alignment of the complete table.
 
  param args              no arguments needed  throws IOException              no arguments needed
  throws IOException
  @throws DocumentException
  */
public static void main(String[] args) throws DocumentException,
   IOException {
  // 定义中文字体
  BaseFont bfChinese = BaseFont.createFont("STSong-Light",
    "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
  com.lowagie.text.Font fontCN = new com.lowagie.text.Font(bfChinese, 12,
    com.lowagie.text.Font.NORMAL);
  System.out.println("table width and alignment");
  // 步骤1:创建一个大小为A4的文档
  Document document = new Document(PageSize.A4);
  try {
   // 步骤 2:
   // 我们为document创建一个监听,并把PDF流写到文件中
   PdfWriter.getInstance(document, new FileOutputStream(
     "c:\\TableWidthAlignment.pdf"));
   // 步骤 3:打开文档
   document.open();
   // 创建一个有3列的表格
   PdfPTable table = new PdfPTable(3);
   // 定义一个表格单元
   PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
   // 定义一个表格单元的跨度
   cell.setColspan(3);
   // 把单元加到表格中
   table.addCell(cell);
   // 把下面这9项顺次的加入到表格中,当一行充满时候自动折行到下一行
   table.addCell("1.1");
   table.addCell("2.1");
   table.addCell("3.1");
   table.addCell("1.2");
   table.addCell("2.2");
   table.addCell("3.2");
   table.addCell("1.3");
   table.addCell("2.3");
   table.addCell("3.3");
   // 重新定义单元格
   cell = new PdfPCell(new Paragraph("cell test1"));
   // 定义单元格的框颜色
   cell.setBorderColor(new Color(255, 0, 0));
   // 把单元格加到表格上,默认为一个单元
   table.addCell(cell);
   // 重新定义单元格
   cell = new PdfPCell(new Paragraph("cell test2"));
   // 定义单元格的跨度
   cell.setColspan(2);
   // 定义单元格的背景颜色
   cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
   // 增加到表格上
   table.addCell(cell);
   document.add(new Paragraph("默认情况下的大小---居中 80%", fontCN));
   // 增加到文档中
   document.add(table);
   document.add(new Paragraph("居中 100%", fontCN));
   // 设置表格大小为可用空白区域的100%
   table.setWidthPercentage(100);
   // 增加到文档中2
   document.add(table);
   document.add(new Paragraph("居右 50%", fontCN));
   // 设置表格大小为可用空白区域的50%
   table.setWidthPercentage(50);
   // 设置水平对齐方式为 居右
   table.setHorizontalAlignment(Element.ALIGN_RIGHT);
   document.add(new Paragraph("居左 50%", fontCN));
   // 增加到文档中3
   document.add(table);
   // 设置水平对齐方式为 居左
   table.setHorizontalAlignment(Element.ALIGN_LEFT);
   document.add(table);
  } catch (Exception de) {
   de.printStackTrace();
  }
  // 步骤 5:关闭文档
  document.close();
}
}

在上面的例子里,我们自己定义了一个3列的表格,而且对它进行了宽度设置和对齐方式的设置,但是细心的朋友会看到,所有的单元宽度都是相同的,因为iText为我们做了一些计算,在默认情况下,各单元格之间就是相同大小的,下面我们就讲一下,如何定义自己的单元格宽度。



想要做到这一点,我们需要PdfPTable(float[] relativeWidths)构造函数,它接受的是一个float数组,比喻说你定义一个有3列的表格,第一列的宽度为单位1,第二列也为单位1,第3列为单位2,那你就可以组织这样一个数组{1f,1f,2f},这个相关数组提供给这个构造函数以后,iText会为你自动计算,每一列到底应该多大。



一旦上面的这些操作完成,你还想改变表格的单元宽度,你可以使用setWidth()方法,我们也会在下面的例子里讲到。



更高级的部分请看:



If you want to work with absolute widths for the columns. You have to let iText calculate a widthpercentage for the table. In this case you should use: setWidthPercentage(float[] columnWidth, Rectangle pageSize). As you can see in the example, you need to do some calculations first to get the right pagesize.
It even easier to use absolute widths if you lock the width of the table to a 'total width'. You need the methods setTotalWidth and setLockedWidth for this. In the example the relation between the different cells will remain 10%, 10%, 5%, 75%, so you'll have 2 columns with a width of 30pt, one with a width of 15pt and one that's 225pt wide.
package com.itext.test;


package cn.xishui;
import java.io.FileOutputStream;

import com.lowagie.text.Document;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;

/**
 * 定义PdfPTable的列宽.
 */
public class CellWidths {

	/**
	 * Width manipulations of cells.
	 * 
	 * @param args
	 *            no arguments needed
	 */
	public static void main(String[] args) {

		System.out.println("Width");
		// 步骤 1: 创建一个document对象,大小为A4,上下左右边距都为36
		Document document = new Document(PageSize.A4, 36, 36, 36, 36);
		try {
			// 步骤 2:
			// 我们为document创建一个监听,并把PDF流写到文件中
			PdfWriter.getInstance(document, new FileOutputStream(
					"c:\\CellWidths.pdf"));
			// 步骤 3:打开文档
			document.open();
			// 创建一个有4列的表格,它们之间的相关比率为 10%,10%,5%,75%
			document.add(new Paragraph("We use 10%,10%,5%,75%:\n\n"));
			float[] widths = { 0.1f, 0.1f, 0.05f, 0.75f };
			PdfPTable table = new PdfPTable(widths);
			table.addCell("10%");
			table.addCell("10%");
			table.addCell("5%");
			table.addCell("75%");
			table.addCell("aa");
			table.addCell("aa");
			table.addCell("a");
			table.addCell("aaaaaaaaaaaaaaa");
			table.addCell("bb");
			table.addCell("bb");
			table.addCell("b");
			table.addCell("bbbbbbbbbbbbbbb");
			table.addCell("cc");
			table.addCell("cc");
			table.addCell("c");
			table.addCell("ccccccccccccccc");
			// 把定义好的表格增加到文档中
			document.add(table);
			document.add(new Paragraph(
				"We change the percentages,20%,20%,10%,50%:\n\n"));
			// 修改表格列关联比 ,现在为20%,20%,10%,50%
			widths[0] = 20f;
			widths[1] = 20f;
			widths[2] = 10f;
			widths[3] = 50f;
			// 这句完成了表格列宽的修改
			table.setWidths(widths);
			document.add(table);
			// 再改变,使用绝对宽度
			widths[0] = 40f;
			widths[1] = 40f;
			widths[2] = 20f;
			widths[3] = 300f;
			// 定义右边距和上边距
			Rectangle r = new Rectangle(PageSize.A4.getRight(72), PageSize.A4.getTop(72));
			table.setWidthPercentage(widths, r);
			document
					.add(new Paragraph(
							"We change the percentage using absolute widths,40,40,20,300:\n\n"));
			document.add(table);
			// 使用一个固定的大小
			document.add(new Paragraph("We use a locked width,300:\n\n"));
			// 设置表格宽度
			table.setTotalWidth(300);
			table.setLockedWidth(true);
			document.add(table);
		} catch (Exception de) {
			de.printStackTrace();
		}
		// 步骤 5:关闭文档
		document.close();
	}
}

分享到:
评论
2 楼 lzth 2012-04-26  
对,呵呵。
1 楼 1927105 2011-08-29  
不错,也就是说PDFTable和Table的区别就是PDFTable支持的丰富些对吧,Table的功能相对弱点,对于一般的导出表格来说应该够用了。

相关推荐

    iText输出pdf表格

    `iText`提供了强大的功能来实现这一需求,让我们深入了解一下如何使用iText库来输出PDF中的表格。 首先,我们需要了解`PdfPTable`类,它是iText中用于创建表格的核心对象。`PdfPTable`允许我们定义表格的列数、行数...

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

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

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

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

    itext生成pdf文件-表格

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

    iText7 原生方法制作带斜线表格

    iText7 原生方法制作带斜线表格

    itextpdf 导出pdf 表格 自动分页中文 目录

    3. **PDF表格自动分页**: 在生成PDF文档时,如果一个表格太长以至于无法在一页内完全显示,iTextPDF可以自动将其分页。通过调整表格属性,如行高、列宽,以及设置分页策略,可以确保表格在每一页上都保持完整的结构...

    Itext生成表格形式Word文档,比较简单的Word

    用iText生成比较简单的表格形式Word

    iTextSharp7库及读取表格数据源码.7z

    资源包含iTextSharp7(net40及netstandard1.6下的库文件),iText.kernel源码和读取表格数据源码,运行TableExtractionFromPDF项目可查看效果。 iText.kernel版本7.1.3.0 iText.io版本7.1.3.0 原文网址:...

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

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

    iText 页码、保持表头、页眉、页脚DEMO

    iText页码、页眉、页脚,itext 的复杂表格实现、保留表头、表尾、根据需求生成不同的iText表格

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

    但对于初学者而言,理解如何使用iText创建包含表格、条形码和图片的文档是学习iText库的首要步骤。 需要注意的是,上述提到的内容部分片段“GERMANYSHA201204A7073SHA201204A7073”,这可能是由于OCR扫描识别错误...

    IText学习资料

    IText库允许开发者动态地创建PDF文档,添加文本、图像、表格和超链接。你可以通过`Document`对象来构造整个PDF页面,并使用`Paragraph`、`Font`、`Image`等类添加内容。此外,`PdfWriter`和`PdfReader`类用于写入和...

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

    在Java开发中,有时我们需要将数据导出为Word文档,以方便阅读和打印。...通过学习和理解这些代码,你可以快速掌握在Java中使用iText生成Word文档的技巧。记得根据你的项目需求进行适当的修改和优化。

    iTextSharp读取表格数据.rar

    资源包含iText.kernel源码和读取表格数据源码,运行TableExtractionFromPDF项目可查看效果。 iText.kernel版本7.1.3.0 iText.io版本7.1.3.0 原文网址:...

    itextpdf包及复杂结构实现

    itextpdf包及复杂结构实现,几个例子运行一下,看看,基本上你就会了

    java itext pdf word 中文 表格 图片

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

    利用itext操作pdf从数据库导出大量数据

    iText提供了丰富的API,可以用于创建新的PDF文档、添加文本、图像、表格、链接等元素,以及对已有PDF进行编辑。在处理大量数据时,我们可能需要创建表格来展示信息,iText提供了`PdfPTable`类来实现这一功能。 步骤...

    itext5pdf表格行变色实现过程源码+doc文档

    新需求,隔行换色,itext in action 是个很好的说明书,照着英文读下来,很简单的进行了实现,思路如下: 1.先创建PdfPTable对象,生成PDF表格cell之后,添加隔行换色的事件,将此事件在PdfPTable加入Document对象...

    Java使用itext5实现PDF表格文档导出

    Java使用itext5实现PDF表格文档导出 Java使用itext5实现PDF表格文档导出是指通过Java语言使用itext5库来生成PDF表格文档的过程。itext5是一个流行的Java类库,用于生成PDF文档。下面将详细介绍Java使用itext5实现...

Global site tag (gtag.js) - Google Analytics