How to Read and Write PDF file in Java
In this post, we are going to learn to read and write a pdf file using Java code. The pdf is a portable document file that is used to transport informative documents. It is one of the widely used file formats.
To deal with pdf file in Java, we use pdfbox library which is the design and developed by the apache foundation. It is used to create, read, write, append the pdf file in an efficient way.
You must download this library before reading the pdf file. You can download it here. The downloaded files are basically JARs files so import them into your project and start working with pdf.
There are some important classes PDDocument , PDPage , PDPageContentStream , etc are required to load and fetch data.
If you are working with the maven project then use the following dependencies in your pom.xml file.
4.0.0 com.studytonight.pdfExample pdfExample 0.0.1-SNAPSHOT org.apache.pdfbox pdfbox 2.0.12
It will automatically download all the required JARs in you project. So, you don’t need to download the JARs manually. After adding these dependencies use the below example code in you Java project to read and write PDF file.
Time for an Example:
Let’s start with an example to read a pdf file using the PDFBox library. Here, we have a pdf file test.pdf that we are loading with load() method and reading using getText() method.
import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; public class Main < public static void main(String[] args) throws IOException< try < // Creating file instance File file = new File("test.pdf"); // Loading pdf file PDDocument document = PDDocument.load(file); PDFTextStripper pdfStripper = new PDFTextStripper(); // Fetching PDF document String text = pdfStripper.getText(document); System.out.println(text); // Closing the document document.close(); >catch(Exception e) < System.out.println(e); >> >
Example:
Since PDFBox is made for pdf handling, then we can write data to a pdf file. Here, we are writing data to test.pdf file in append mode, for append mode we used APPEND constant in the PDPageContentStream so that the file data will not replace. We used setFont() method to set the font and save() method to save the changes to the file.
import java.io.File; import java.io.IOException; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.font.PDType1Font; public class Main < public static void main(String[] args) throws IOException< PDDocument document = PDDocument.load(new File("test.pdf")); PDPage page = document.getPage(0); PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND,true,true); contentStream.beginText(); //Setting the font contentStream.setFont(PDType1Font.TIMES_ROMAN, 12); //Setting the text position contentStream.newLineAtOffset(25, 500); String text = "This message is writtern to the pdf file."; contentStream.showText(text); contentStream.endText(); contentStream.close(); // Saving file after writing document.save(new File("test.pdf")); document.close(); >>
After executing this program, it will create a PDF file test.pdf in the current directory. You can open that file and see it contains the text that we saved with the code.
Java – Read and Write PDF with iText
In this iText tutorial, we are writing various code examples to read a PDF file and write a PDF file. iText library helps in dynamically generating the .pdf files from Java applications.
The given code examples are categorized into multiple sections based on the functionality they achieve. With each example, I have attached a screenshot of the generated PDF file to visualize what exactly the code is writing in the PDF file. You may extend these examples to suit your needs.
On the brighter side, iText is an open-source library. Note that though iText is open source, you still need to purchase a commercial license if you want to use it for commercial purposes.
iText is freely available from https://itextpdf.com/. The iText library is powerful and supports the generation of HTML, RTF, and XML documents and generating PDFs.
We can choose from various fonts to be used in the document. Also, the structure of iText allows us to generate any of the above-mentioned type of documents with the same code. Isn’t it a great feature, right?
The iText library contains classes to generate PDF text in various fonts, create tables in PDF documents, add watermarks to pages, and so on. There are many more features available with iText which I will leave on you to explore.
To add iText into your application, include the following maven repository into our pom.xml file.
com.itextpdf itextpdf 5.5.13.2
Or we can download the latest jar files from its maven repository.
Let’s list down and get familiar with the essential classes we will use in this application.
- com.itextpdf.text.Document : This is the most important class in iText library and represent PDF document instance. If you need to generate a PDF document from scratch, you will use the Document class. First you must create a Document instance. Then you must open it. After that you add content to the document. Finally you close the Document instance.
- com.itextpdf.text.Paragraph : This class represents a indented “paragraph” of text. In a paragraph you can set the paragraph alignment, indentation and spacing before and after the paragraph.
- com.itextpdf.text.Chapter : This class represents a chapter in the PDF document. It is created using a Paragraph as title and an int as chapter number.
- com.itextpdf.text.Font : This class contains all specifications of a font, such as family of font, size, style, and color. Various fonts are declared as static constants in this class.
- com.itextpdf.text.List : This class represents a list, which, in turn, contains a number of ListItems.
- com.itextpdf.text.pdf.PDFPTable : This is a table that can be put at an absolute position but can also be added to the document as the class Table.
- com.itextpdf.text.Anchor : An Anchor can be a reference or a destination of a reference. A link like we have in HTML pages.
- com.itextpdf.text.pdf.PdfWriter : When this PdfWriter is added to a certain PdfDocument, the PDF representation of every Element added to this Document will be written to the outputstream attached to writer (file or network).
- com.itextpdf.text.pdf.PdfReader : Used to read a PDF document. Simple and clear.
3. iText Hello World Example
Let’s start writing our example codes with the typical Hello World application. I will create a PDF file with a single statement in the content.
import java.io.FileNotFoundException; import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.DocumentException; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; public class JavaPdfHelloWorld < public static void main(String[] args) < Document document = new Document(); try < PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf")); document.open(); document.add(new Paragraph("A Hello World PDF document.")); document.close(); writer.close(); >catch (DocumentException e) < e.printStackTrace(); >catch (FileNotFoundException e) < e.printStackTrace(); >> >
4. Setting File Attributes to PDF
This example shows how to set various attributes like author name, created date, creator name or simply title of the pdf file.
Document document = new Document(); try < PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("SetAttributeExample.pdf")); document.open(); document.add(new Paragraph("Some content here")); //Set attributes here document.addAuthor("Lokesh Gupta"); document.addCreationDate(); document.addCreator("HowToDoInJava.com"); document.addTitle("Set Attribute Example"); document.addSubject("An example to show how attributes can be added to pdf files."); document.close(); writer.close(); >catch (Exception e)
An example to show how we can add images to PDF files. The example contains adding images from the file system as well as URLs. Also, I have added code to position the pictures within the document.
Document document = new Document(); try < PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("AddImageExample.pdf")); document.open(); document.add(new Paragraph("Image Example")); //Add Image Image image1 = Image.getInstance("temp.jpg"); //Fixed Positioning image1.setAbsolutePosition(100f, 550f); //Scale to new height and new width of image image1.scaleAbsolute(200, 200); //Add to document document.add(image1); String imageUrl = "http://www.eclipse.org/xtend/images/java8_logo.png"; Image image2 = Image.getInstance(new URL(imageUrl)); document.add(image2); document.close(); writer.close(); >catch (Exception e)
6. Generating a Table in PDF
Below example shows how to add tables in a pdf document.
public static void main(String[] args) < Document document = new Document(); try < PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("AddTableExample.pdf")); document.open(); PdfPTable table = new PdfPTable(3); // 3 columns. table.setWidthPercentage(100); //Width 100% table.setSpacingBefore(10f); //Space before table table.setSpacingAfter(10f); //Space after table //Set Column widths float[] columnWidths = ; table.setWidths(columnWidths); PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1")); cell1.setBorderColor(BaseColor.BLUE); cell1.setPaddingLeft(10); cell1.setHorizontalAlignment(Element.ALIGN_CENTER); cell1.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2")); cell2.setBorderColor(BaseColor.GREEN); cell2.setPaddingLeft(10); cell2.setHorizontalAlignment(Element.ALIGN_CENTER); cell2.setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3")); cell3.setBorderColor(BaseColor.RED); cell3.setPaddingLeft(10); cell3.setHorizontalAlignment(Element.ALIGN_CENTER); cell3.setVerticalAlignment(Element.ALIGN_MIDDLE); //To avoid having the cell border and the content overlap, if you are having thick cell borders //cell1.setUserBorderPadding(true); //cell2.setUserBorderPadding(true); //cell3.setUserBorderPadding(true); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); document.add(table); document.close(); writer.close(); > catch (Exception e) < e.printStackTrace(); >>
7. Creating List of Items in PDF
Below example will help you understand how to write lists in pdf files using the iText library.
Document document = new Document(); try < PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("ListExample.pdf")); document.open(); document.add(new Paragraph("List Example")); //Add ordered list List orderedList = new List(List.ORDERED); orderedList.add(new ListItem("Item 1")); orderedList.add(new ListItem("Item 2")); orderedList.add(new ListItem("Item 3")); document.add(orderedList); //Add un-ordered list List unorderedList = new List(List.UNORDERED); unorderedList.add(new ListItem("Item 1")); unorderedList.add(new ListItem("Item 2")); unorderedList.add(new ListItem("Item 3")); document.add(unorderedList); //Add roman list RomanList romanList = new RomanList(); romanList.add(new ListItem("Item 1")); romanList.add(new ListItem("Item 2")); romanList.add(new ListItem("Item 3")); document.add(romanList); //Add Greek list GreekList greekList = new GreekList(); greekList.add(new ListItem("Item 1")); greekList.add(new ListItem("Item 2")); greekList.add(new ListItem("Item 3")); document.add(greekList); //ZapfDingbatsList List Example ZapfDingbatsList zapfDingbatsList = new ZapfDingbatsList(43, 30); zapfDingbatsList.add(new ListItem("Item 1")); zapfDingbatsList.add(new ListItem("Item 2")); zapfDingbatsList.add(new ListItem("Item 3")); document.add(zapfDingbatsList); //List and Sublist Examples List nestedList = new List(List.UNORDERED); nestedList.add(new ListItem("Item 1")); List sublist = new List(true, false, 30); sublist.setListSymbol(new Chunk("", FontFactory.getFont(FontFactory.HELVETICA, 6))); sublist.add("A"); sublist.add("B"); nestedList.add(sublist); nestedList.add(new ListItem("Item 2")); sublist = new List(true, false, 30); sublist.setListSymbol(new Chunk("", FontFactory.getFont(FontFactory.HELVETICA, 6))); sublist.add("C"); sublist.add("D"); nestedList.add(sublist); document.add(nestedList); document.close(); writer.close(); >catch (Exception e)
8. Styling the PDF Content
Let’s see some examples of styling the content of PDF. The example contains the usage of Fonts, chapters, and sections.
Font blueFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL, new CMYKColor(255, 0, 0, 0)); Font redFont = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD, new CMYKColor(0, 255, 0, 0)); Font yellowFont = FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new CMYKColor(0, 0, 255, 0)); Document document = new Document(); try < PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("StylingExample.pdf")); document.open(); //document.add(new Paragraph("Styling Example")); //Paragraph with color and font styles Paragraph paragraphOne = new Paragraph("Some colored paragraph text", redFont); document.add(paragraphOne); //Create chapter and sections Paragraph chapterTitle = new Paragraph("Chapter Title", yellowFont); Chapter chapter1 = new Chapter(chapterTitle, 1); chapter1.setNumberDepth(0); Paragraph sectionTitle = new Paragraph("Section Title", redFont); Section section1 = chapter1.addSection(sectionTitle); Paragraph sectionContent = new Paragraph("Section Text content", blueFont); section1.add(sectionContent); document.add(chapter1); document.close(); writer.close(); >catch (Exception e)
Let’s see an example of creating password protected pdf file. Here writer.setEncryption() is used to set a password to the generated PDF.
We need to add bouncy castle jars generating for password protected PDFs. I have added these jars in sourcecode of examples for this post.
private static String USER_PASSWORD = "password"; private static String OWNER_PASSWORD = "lokesh"; public static void main(String[] args) < try < OutputStream file = new FileOutputStream(new File("PasswordProtected.pdf")); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, file); writer.setEncryption(USER_PASSWORD.getBytes(), OWNER_PASSWORD.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128); document.open(); document.add(new Paragraph("Password Protected pdf example !!")); document.close(); file.close(); >catch (Exception e) < e.printStackTrace(); >>
10. Creating PDF with Limited Permissions
In this example, I am setting a few file permissions for a pdf file to limit access for other users. Following are several permission values:
PdfWriter.ALLOW_PRINTING PdfWriter.ALLOW_ASSEMBLY PdfWriter.ALLOW_COPY PdfWriter.ALLOW_DEGRADED_PRINTING PdfWriter.ALLOW_FILL_IN PdfWriter.ALLOW_MODIFY_ANNOTATIONS PdfWriter.ALLOW_MODIFY_CONTENTS PdfWriter.ALLOW_SCREENREADERS
You can provide multiple permissions by ORing different values. For example, PdfWriter.ALLOW_PRINTING | PdfWriter.ALLOW_COPY.
public static void main(String[] args) < try < OutputStream file = new FileOutputStream(new File( "LimitedAccess.pdf")); Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, file); writer.setEncryption("".getBytes(), "".getBytes(), PdfWriter.ALLOW_PRINTING , //Only printing allowed; Try to copy text !! PdfWriter.ENCRYPTION_AES_128); document.open(); document.add(new Paragraph("Limited Access File !!")); document.close(); file.close(); >catch (Exception e) < e.printStackTrace(); >>
11. Reading and Modifying an Existing PDF
To complete this tutorial, let’s see an example of reading and modifying a PDF file using PDFReader class provided by the iText library itself. In this example, I will read content from a PDF and add some random content to all its pages.
public static void main(String[] args) < try < //Read file using PdfReader PdfReader pdfReader = new PdfReader("HelloWorld.pdf"); //Modify file using PdfReader PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("HelloWorld-modified.pdf")); Image image = Image.getInstance("temp.jpg"); image.scaleAbsolute(100, 50); image.setAbsolutePosition(100f, 700f); for(int i=1; i<= pdfReader.getNumberOfPages(); i++) < PdfContentByte content = pdfStamper.getUnderContent(i); content.addImage(image); >pdfStamper.close(); > catch (IOException e) < e.printStackTrace(); >catch (DocumentException e) < e.printStackTrace(); >>
12. Writing PDF to HttpOutputStream
This is the last example in the list, and in this example, I am writing the content of created PDF file into the output stream attached to the HttpServletResponse object. This will be needed when you stream the PDF file in a client-server environment.
Document document = new Document(); try< response.setContentType("application/pdf"); PdfWriter.getInstance(document, response.getOutputStream()); document.open(); document.add(new Paragraph("howtodoinjava.com")); document.add(new Paragraph(new Date().toString())); //Add more content here >catch(Exception e) < e.printStackTrace(); >document.close(); >
That’s all for the iText example codes. Leave a comment if something is unclear to you, OR you would like to add any other example to this list.
How to create PDF File in Java – iText Example
This is also one of the top 20 Java libraries I recommend Java programmer to learn. Learning these libraries will greatly improve your ability as a Java developer.
Getting iText
You can either download iText jar from its home page http://www.lowagie.com/iText/download.html
or you can also download from the Maven repository by adding iText dependency on your pom.xml file.
iText Maven Dependency
iText Gradle Dependency
How to create PDF files/documents in Java – iText library Example
Here is a complete code example to generate a PDF file using the
iText library in Java.
Adding metadata/Setting attributes of PDF using free Java-PDF library
While you generate a PDF, you may want to set its different attribute like: author name, title, file description etc. iText jar can help you to set different attributes of a PDF file. Document object provide different methods to add various attributes to a PDF file.
That’s all on how to generate PDF files in Java using iText. Lots of open source framework uses iText to provide PDF support in their application. for example display tag, which is a popular JSP tag library to generate dynamic HTML tables, allow you to export table in PDF format. It does this conversion by using iText library, so all you need is to include iText.jar in your classpath and boom, you can export PDF documents from Java application.
- The 2020 Java Developer RoadMap
- 10 Frameworks Java and Web Developers should learn
- 10 Best Courses to learn Spring Framework
- 10 Things Java Developer Should Learn in 2020
- My favorite courses to learn Software Architecture
- 10 Programming Languages to explore in 2020
- 10 Ways to Learn a New Technology in 2020
- 10 PluralSight Courses for Java and Web Developers
- 21 Books Java Developers should read in 2020
- Data Structure and Algorithms Courses to Crack Coding Interview
- 20 Java Books You Can Read in 2020
- 10 Tutorials to Learn Java 8 Better
- 10 tips to become a better Java Programmer
- 10 Tools Java Developer Should Learn in 2020
Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues too. If you have any feedback or questions then please share your thoughts by commenting, I highly appreciate your comment and try to reply as soon as possible.
Published on Java Code Geeks with permission by Javin Paul, partner at our JCG program. See the original article here: How to create PDF File in Java – iText Example