Search code examples
.netitext

Adding Meta data with iText 7


How do I add meta data (title,author etc) in iText 7 (.Net). All the threads and examples I have found seem to use the old format

document.addTitle(“Title”); 

It seems you can’t do this in iText 7.

Thanks


Solution

  • Please take a look at chapter 7, more specifically at the subsection XMP metadata. In that subsection, you'll find the following example:

    public void createPdf(String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(
            new PdfWriter(dest,
                new WriterProperties()
                    .addXmpMetadata()
                    .setPdfVersion(PdfVersion.PDF_1_6)));
        PdfDocumentInfo info = pdf.getDocumentInfo();
        info.setTitle("The Strange Case of Dr. Jekyll and Mr. Hyde");
        info.setAuthor("Robert Louis Stevenson");
        info.setSubject("A novel");
        info.setKeywords("Dr. Jekyll, Mr. Hyde");
        info.setCreator("A simple tutorial example");
        Document document = new Document(pdf);
        document.add(new Paragraph("Mr. Jekyl and Mr. Hyde"));
        document.close();
    }
    

    As you can see, the metadata is no longer added straight to the document, but to a PdfDocumentInfo object obtained from the PdfDocument instance. This PdfDocumentInfo object is used to create the Info dictionary (old-style metadata) as well as the XMP stream (new-style metadata). The XMP stream is only created if you use the addXmpMetadata() method on the WriterProperties.

    NOTE: Since the info dictionary is deprecated in favor of XMP metadata in PDF 2.0, this will change in future versions of iText. In those versions, we'll give preference to XMP over using the Info dictionary.