To create a pdf file using iText jar first download the iText jar files and include in the application classpath. Create Document instance. It represents the current document to which we are adding content. Create OutputStream instance. It represents the generated pdf. In this iText tutorial, I am writing various code examples read a pdf file and generate PDF file. IText library helps to generate pdf files from java applications dynamically. These code examples are categorized into multiple sections based on the work they do OR functionality they achieve.
jPDFWriter
™Generate PDF documents directly from your Java applications.TRY LIVE DEMO
jPDFWriter – Free Java PDF Library to Create PDF Documents
Generate PDF documents directly from your Java programs. jPDFWriter is a Java class library that allows you to create PDF documents directly from your Java programs without having to install any third party drivers or software. jPDFWriter emulates the standard Java classes to print and draw graphics to reduce the learning curve when using the library and reuse existing code.
jPDFWriter is free to use for commercial purposes, there are no license fees. The library is licensed using the jPDFWriter License.
If you need more functionality than creating PDF documents, Qoppa Software also produces a library called jPDFProcesswhich can open, manipulate and print existing PDF documents.
jPDFWriter can create PDF files in two ways:
- PDF files can be created directly using jPDFWriter’s very simple API. Simply create a PDFDocument object, create as many PDFPage objects as necessary, draw strings, graphics or any other elements supported by Graphics2D to the pages and then save the document.
- jPDFWriter also extends the standard Java PrinterJob so that you can create PDF files in the same way that you would print to a physical printer. This allows for reuse of existing printing code and for an application to decide, at runtime, whether to send the output to a printer or to a PDF file.
PDF files can be saved to files on a disk, written to a java.io.OutputStream or written directly to a javax.servlet.ServletOutputStream to show in a browser when working within a J2EE application server.
jPDFWriter is written entirely in Java so application portability and operating system independence are not compromised. jPDFWriter has been tested on Windows, Linux, Unix (Solaris, HP UX, IBM AIX) and Max OS X.
Main Features
- Emulates the java graphics and printing classes, thus minimizing code changes and training.
- Allows runtime decision to go to a printer or to a PDF file.
- Supports PDF compression for smaller document size.
- Support for PDF encryption and permissions.
- Support for embedding fonts.
- Support for all image types, including JBIG2 and JPEG 2000.
- Works on Windows, Linux, Unix and Mac OS X (100% Java).
- Tested on JDK 1.4.2 and above.
- No need to install or configure additional drivers or software when deploying your application.
If you require any additional information, don’t hesitate to contact us at [email protected].
Try it and Sponsor
Help
Related Products
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.
Maven Dependencies: pom.xml
Java Create Pdf File Online
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.
Itext Pdf Java
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
Java Create Pdf File From Html
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
![Java create pdf file size Java create pdf file size](/uploads/1/2/6/5/126592889/772459504.jpg)
IT Solutions Builder TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD