Skip to main content
Skip table of contents

Adding metadata

These examples were written in answer to questions such as:


addlanguage

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfString;
import com.itextpdf.kernel.pdf.PdfWriter;

import java.io.File;

public class AddLanguage {
    public static final String DEST = "./target/sandbox/stamper/add_language.pdf";
    public static final String SRC = "./src/main/resources/pdfs/hello.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new AddLanguage().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        pdfDoc.getCatalog().put(PdfName.Lang, new PdfString("EN"));
        pdfDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Pdf;

namespace iText.Samples.Sandbox.Stamper 
{
    public class AddLanguage 
    {
        public static readonly String DEST = "results/sandbox/stamper/add_language.pdf";
        public static readonly String SRC = "../../../resources/pdfs/hello.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new AddLanguage().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            pdfDoc.GetCatalog().Put(PdfName.Lang, new PdfString("EN"));
            pdfDoc.Close();
        }
    }
}


addxmptopage

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.xmp.XMPMetaFactory;

import java.io.File;

public class AddXmpToPage {
    public static final String DEST = "./target/sandbox/stamper/add_xmp_to_page.pdf";
    public static final String SRC = "./src/main/resources/pdfs/hello.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new AddXmpToPage().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

        PdfPage page = pdfDoc.getFirstPage();
        page.setXmpMetadata(XMPMetaFactory.create());

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.XMP;

namespace iText.Samples.Sandbox.Stamper 
{
    public class AddXmpToPage 
    {
        public static readonly String DEST = "results/sandbox/stamper/add_xmp_to_page.pdf";
        public static readonly String SRC = "../../../resources/pdfs/hello.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new AddXmpToPage().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            
            PdfPage page = pdfDoc.GetFirstPage();
            page.SetXmpMetadata(XMPMetaFactory.Create());
            
            pdfDoc.Close();
        }
    }
}



changeinfodictionary

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfDocumentInfo;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class ChangeInfoDictionary {
    public static final String DEST = "./target/sandbox/stamper/change_info_dictionary.pdf";
    public static final String SRC = "./src/main/resources/pdfs/hello.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new ChangeInfoDictionary().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
        PdfDocumentInfo info = pdfDoc.getDocumentInfo();

        Map<String, String> newInfo = new HashMap<>();
        newInfo.put("Special Character: \u00e4", "\u00e4");

        StringBuilder buf = new StringBuilder();
        buf.append((char) 0xc3);
        buf.append((char) 0xa4);
        newInfo.put(buf.toString(), "\u00e4");

        info.setMoreInfo(newInfo);

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using iText.Kernel.Pdf;

namespace iText.Samples.Sandbox.Stamper 
{
    public class ChangeInfoDictionary 
    {
        public static readonly String DEST = "results/sandbox/stamper/change_info_dictionary.pdf";
        public static readonly String SRC = "../../../resources/pdfs/hello.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new ChangeInfoDictionary().ManipulatePdf(DEST);
        }

        protected internal virtual void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfDocumentInfo info = pdfDoc.GetDocumentInfo();
            
            IDictionary<String, String> newInfo = new Dictionary<String, String>();
            newInfo.Add("Special Character: \u00e4", "\u00e4");
            
            StringBuilder buf = new StringBuilder();
            buf.Append((char)0xc3);
            buf.Append((char)0xa4);
            newInfo.Add(buf.ToString(), "\u00e4");
            
            info.SetMoreInfo(newInfo);
            
            pdfDoc.Close();
        }
    }
}


changeversion

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.stamper;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfVersion;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.WriterProperties;

import java.io.File;

public class ChangeVersion {
    public static final String DEST = "./target/sandbox/stamper/change_version.pdf";
    public static final String SRC = "./src/main/resources/pdfs/OCR.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new ChangeVersion().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC),

                // Please note that the default PdfVersion value is PDF 1.7
                new PdfWriter(dest, new WriterProperties().setPdfVersion(PdfVersion.PDF_1_5)));

        pdfDoc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Pdf;

namespace iText.Samples.Sandbox.Stamper 
{
    public class ChangeVersion 
    {
        public static readonly String DEST = "results/sandbox/stamper/change_version.pdf";
        public static readonly String SRC = "../../../resources/pdfs/OCR.pdf";

        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            
            new ChangeVersion().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest) 
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), 
                    
                        // Please note that the default PdfVersion value is PDF 1.7
                        new PdfWriter(dest, new WriterProperties().SetPdfVersion(PdfVersion.PDF_1_5)));
            
            pdfDoc.Close();
        }
    }
}
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.