iText PDF 系列文章之一:continued, page 5

PdfStamper can also be used to fill in AcroForms or to merge a PDF with an (X)FDF file.

Quick Start: iText Toolbox

iText was developed as a developer's library; meaning I and the developers that joined the project later on, weren't aiming at an end user market. Developers could integrate iText into their Java web applications or standalone Java Programs, but the library itself didn't have a user interface. This made it difficult for people downloading the jar file to assess the possibilities of the library. Only recently the iText jar was made executable. Just download the jar and run the command:

java -jar iText.jar

The iText toolbox will open and you'll have a set of tools that demonstrates part of the PDF manipulating functionality: use it to encrypt/decrypt existing PDFs, concatenate PDF files, select specific pages from a PDf and so on.

Figure 5. the iText toolbox

Concatenate and/or Split PDFs

PdfStamper is a great class is you want to manipulate one and only one existing PDF file; but if you are dealing with multiple PDF files, you need another object: PdfCopy. PdfCopy can't change the page content of a PDF, but it can be used to create a new document with a selection of pages from one or more documents. The following sample code takes the first page of three different PDF files and bundles them into one new PDFdocument.

PdfReader reader = new PdfReader("Hello1.pdf");
Document document = new Document(
reader.getPageSizeWithRotation(1));
PdfCopy copy = new PdfCopy(document,
new FileOutputStream("HelloCopy123.pdf"));
document.open();
copy.addPage(copy.getImportedPage(reader, 1));
reader = new PdfReader("Hello2.pdf");
copy.addPage(copy.getImportedPage(reader, 1));
reader = new PdfReader("Hello3.pdf");
copy.addPage(copy.getImportedPage(reader, 1));
document.close();

Hello1.pdf is again a very simple PDF file saying 'Hello World'. The other PDFs contain some more advanced functionality. Hello2.pdf contains an annotation; Hello3.pdf a hyperlink to the iText website. All this functionality and more is supported by iText.

On the Net

.NET ports

Related Software

iText in Action

I wasn't able to go into much detail, but if you have questions there's a very active iText mailing list. The online tutorial provides you with an abundance of short code samples and the upcoming book 'iText in Action' will bring you a series of real world examples that can be easily adapted into your enterprise software. I hope this article inspired you to have a look at PDF as a document format. With iText, you have a tool that can produce PDF files with all kinds of content: text, images, barcodes, tables and columns. There is support for all kinds of fonts (CJK, TTF, OTF) and encodings, including Asian (Chinese, Japanese, Korean...) and Semitic (Arabic, Hebrew) writing systems (top to bottom, right to left). Convenience methods and classes are added to facilitate automation, such as hyphenation, page events , automatic bookmarks. You can also create very

complex graphics, directly or by using a third party library that writes to a Graphics2D object. You can manipulate existing PDF files; select and/or concatenate pages, rotate or scale pages, add watermarks and page numbers; You can create, fill and process forms and form data. iText supports PDF actions, annotations, encryption, digital signatures, XMP, OCG, PDF/X...New functionality is added almost every week; coming soon: further implementation of PDF/A (including Tagged PDF), as well as XFA support. If you have a document problem, iText could be the solution.

原文地址:http://www.pdfdev.com/page/articles_1_5/

posted on 2006-08-20 12:59  OrientalDragon  阅读(936)  评论(0)    收藏  举报