The PdfDocument is the main class in PDFOne Java. It represents a PDF document and allows you to create, read, and enhance PDF documents. It offers numerous methods for you to render PDF elements such as text, images, shapes, forms, watermarks, and annotations on to documents. Creating Your First PDF Document. Following are few libraries to create PDF with Java: iText; Apache PDFBox; BFO; I have used iText for genarating PDF's with a little bit of pain in the past. Or you can try using FOP: FOP is an XSL formatter written in Java. It is used in conjunction with an XSLT transformation engine to format XML documents into PDF.
The Portable Document Format, aka PDF, is a de facto standard for document exchanges between users. Java programmers sometimes need to create document files of their application data to showcase business information or create a report that can be exchanged or printed without messing with the data layout and format. PDF is an ideal file format that can reliably do that independent of software hardware and operating system. This article delves into the tool called iText, which enables a Java programmer to create PDF documents through Java code.
An Overview
The Portable Document Format (PDF) is a product of the Camelot project by Dr. John Edward Warnock, 1991, co-founder of Adobe Systems. The goal was to enable anyone to capture documents from any application as an electronic version for document exchange. It stood the test of time and became a trusted format of electronic document exchange down the years. Although PDF is now an open file format (ISO-32000-1) maintained by the International Organization for Standardization (ISO), some indispensable parts to work with it are still proprietary (such as Adobe XML Forms Architecture and Adobe JavaScript) and defined by Adobe. A PDF document can contain various elements and not just mere text—such as links and buttons, form fields, audio, video, and business logic. It also can be signed electronically and is easily viewed using Acrobat Reader (a PDF reader application by Adobe). However, today there are many other open source PDF applications that can be used to create, read, and write PDF file format. The popular Microsoft Office and LibreOffice collections have options to export their documents in this format.
The iText is a Java library that enables a developer to generate and manipulate PDF documents through Java code on the fly. The library offers some powerful features to generate read-only, platform-independent documents which contain not only text but also lists, tables, and images. This library is particularly useful to create a consistent look-and-feel document; otherwise, what Java can do best is create an HTML page which is browser dependent. The API comes quite handy and has almost flat learning curve. The iText has a version that works with the .NET framework as well. For an iText library to work with Java, it requires Java version 5 and above.
Java Create Pdf File From Images
Maven Dependencies: pom.xml
Here are the dependencies one needs to add into pom.xml to work out the following examples. The lines commented with <!-- START --> and <!-- END --> are essential; other tags and dependencies illustrated are discretionary.
Working with a PDF Document
Intuitively, a PDF file is a container for documents and an iText Document class is the default root element. It provides methods that work with the page size setting, page rotating, adding elements, and writing text according to specific co-ordinates. Because this class has no idea of the actual PDF format and their syntaxes, the library provides a subclass of Document, called the PdfDocument class. An instance of this class provides the entry point to work with the PDF document. This class provides the necessary functionalities to work with the PDF document, such as adding pages, font, and events; copying pages from one document to another; extracting document information; and so forth To open a PDF document for writing, it must be opened in association with a PdfWriter instance. In a similar manner, a PDF document to be opened for reading and parsing must be opened in association with an PdfReader instance. The PdfWriter is subscribing to the Java core class java.io.OutputStream and PdfReader to java.io.InputStream. They are both defined in the package named com.itextpdf.kernel.pdf. The PdfReader supplies five constructors, such as:
- PdfReader (File file)
- PdfReader (InputStream is)
- PdfReader (InputStream is, ReaderProperties prop)
- PdfReader (IRandomAccessSource byteSource, ReaderProperties prop)
- PdfReader (String fileName)
- PdfReader (String fileName, ReaderProperties prop)
And, the constructors of PdfWriter are the following:
- PdfWriter (java.io.File file)
- PdfWriter (java.io.OutputStream ostream)
- PdfWriter (java.io.OutputStream ostream, com.itextpdf.kernel.pdf.WriterProperties props)
- PdfWriter (java.lang.String fileName)
- PdfWriter (java.lang.String fileName, com.itextpdf.kernel.pdf.WriterProperties props)
Any of these constructors may be used to create an instance of PdfWriter/PdfReader. The library provides the necessary functionalities to read and write byte information according to the rules of the PDF document.
A Quick Example
This is a very simple and rudimentary example to illustrate the ideas discussed earlier. The code is self-explanatory. The API documentation of iText 7 can be found in this link.
The preceding data also can be stated in a tabular format, as follows. The code has been elaborated to make it more comprehensible.
The iText Library for the Java Programmer
Perhaps the most significant aspect of this library is that we are able to create a PDF document without having to know much about the intricacies of the PDF specification. The specification is huge and complex because it envelops much more than text, such as cross-reference tables, graphics, links, file attachment, rich media, and so on. The library, however, is limited in its support but nonetheless provides the essential features to create a PDF document programmatically. The API is intuitive and simple to learn. In most cases, developers do not need to create PDF documents that are complex. But, as the library develops, it is going to fill in the missing spots of the specification.
Conclusion
The iText developer home page offers extensive examples on various features of the library. There are numerous classes available to deal with various other aspects of the PDF format. This article simply scratched the surface to give a taste of what the library offers in general. Visit the links below to get a more comprehensive example on this.
References
IT Solutions Builder TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD