How to set attributes of PDF document using iText library

In this tutorial you will learn how to set the PDF attributes like author of PDF document, creation date of PDF document and title of PDF. You can set these attribute while generating a PDF document.

Please note we are using iText version 5 for this example.

You can set PDF’s different attribute like author name, title, file description etc. The iText jar will help you to set different attributes of a PDF file. The Document object provides different methods to add various attributes to a PDF file.

Below is the example of setting the diffrent type of attributes using iText library.

package com.java.connect.itext;
 
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
 
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
 
public class PDFAttributesExample {
    public static void main(String[] args) throws DocumentException, FileNotFoundException {
        // Create new PDF document
        Document document = new Document();
        // Create an output stream of PDF file.
        OutputStream out = new FileOutputStream("PDF_Attributes_Example.pdf");
        // Open the PDF document
        PdfWriter.getInstance(document, out);
        // Set the author of PDF document.
        document.addAuthor("Java Connect");
        // Set the creation date. by defaul it set as current date.
        document.addCreationDate();
        // Set the creator of PDF document.
        document.addCreator("iText java library example");
        // Set the title of PDF document.
        document.addTitle("Hello World example PDF document");
        // Set the Subject of PDF document.
        document.addSubject("Hello world subject");
        // Get a PdfWriter instance to write in PDF document.
        document.open();
        // Wirte a Text using Paragraph object.
        Paragraph paragraph = new Paragraph("Setting attributes of this document.");
        // Add paragraph to PDF document.
        document.add(paragraph);
        // Close the PDf document after use.
        document.close();
    }
}

You can see the PDF generated document attributes by click on Document properties in File menu of PDF document you can see in below screen.

iText-setting-attributes-of-PDF-using-iText-library