---
title: "iText 相关"
author: "love2java"
date: "2009-07-02T17:54:10Z"
tags: []
slug: "itext"
draft: false
lastmod: "2009-07-02T17:54:10Z"
id: 419123
url: "https://love2java.iteye.com/blog/419123"
---

iText 相关：包括文档，源码

 

 

一些标记：

you’ll learn how to do the following:  
■ Serve dynamically generated PDF to a web browser  
■ Generate documents and reports based on data from an XML file or  
a database  
■ Create maps and ebooks, exploiting numerous interactive features available  
in PDF  
■ Add bookmarks, page numbers, watermarks, and other features to existing  
PDF documents  
■ Split and/or concatenate pages from existing PDF files  
■ Fill out forms, add digital signatures, and much more  
You’ll create these documents on the fly, meaning you aren’t going to use a desktop  
application such as Adobe Acrobat. Instead, you’ll use an API to produce PDF  
directly from your own applications, which is necessary when a project has one of  
the following requirements:  
■ The content needs to be served in a web environment, and PDF is preferred  
over HTML for better printing quality, for security reasons, or to  
reduce the file size.  
■ The PDF files can’t be produced manually due to the volume (number of  
pages/documents) or because the content isn’t available in advance (it’s calculated  
and/or based on user input).  
■ Documents need to be created in unattended mode (for instance, in a  
batch process).  
■    The content needs to be customized and/or personalized.

the Library (or Lesser) GNU Public License (LGPL),  
the Mozilla Public License (MPL)  
the Information and Communications Technology (ICT)  
 

The default width of a table is 80 percent of the available width. Let’s do the  
math for the table in figure 6.1: The width page is 595 pt minus the margins,  
which are 36 pt. In short, the width of the table is (595 – (2 * 36)) * 80 percent, or  
418.4 pt.

//这两者搭配使用  
table.setTotalWidth(widths);  
table.setLockedWidth(true);

document.add(table);  
table.setWidthPercentage(50);  
table.setHorizontalAlignment(Element.ALIGN\_RIGHT);  
document.add(table);  
table.setHorizontalAlignment(Element.ALIGN\_LEFT);  
document.add(table);

PdfPTable table = new PdfPTable(3);  
table.setTotalWidth(216f);  
table.setLockedWidth(true);

The example sets the total width to 216 user units and has three columns, so  
every column in the table is 1 in wide (216 user units / 3 = 72 user units = 1 in).

float\[] widths1 = { 1f, 1f, 2f };  
PdfPTable table = new PdfPTable(widths1);  
float\[] widths2 = { 2f, 1f, 1f };  
table.setWidths(widths2);

/* chapter06/PdfPTableWithoutBorders.java \*/  
PdfPTable table = new PdfPTable(3);  
table.getDefaultCell().setBorder(PdfPCell.NO\_BORDER);  
PdfPCell cell =  
new PdfPCell(new Paragraph("header with colspan 3"));  
cell.setColspan(3);  
table.addCell(cell);  
table.addCell("1.1");  
table.addCell("2.1");  
table.addCell("3.1");

FAQ  
Is it possible to have the column width change dynamically based on the content  
of the cells? PDF isn’t HTML, and a PdfPTable is completely different  
from an HTML table rendered in a browser; iText can’t calculate column  
widths based on the content of the columns. The result would  
depend on too many design decisions and wouldn’t always correspond  
with what a developer expects. It’s better to have the developer define  
the widths.

int width\[] = ;  
aTable.setWidths(width);  
aTable.setWidth(80); // 占页面宽度 80%  
aTable.setAutoFillEmptyCells(true); //自动填满

aTable.setDefaultHorizontalAlignment(Element.ALIGN\_LEFT);  
aTable.setDefaultVerticalAlignment(Element.ALIGN\_MIDDLE);  
aTable.setAutoFillEmptyCells(true); //自动填满  
aTable.setPadding(1);  
aTable.setSpacing(1);  
aTable.setDefaultCellBorder(0);  
aTable.setBorder(0);

java.lang.OutOfMemoryError: Java heap space