This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information

PDF change metadata

Sometimes it is useful to change PDF Metadata.
The following code snipped is based on Add an image to an existing PDF

HashMap<String, String> hmpInfo = new HashMap<String, String>();
hmpInfo.put("Title", "CV");
hmpInfo.put("Author", "rjahn");

PdfDictionary dictTrailer = pdr.getTrailer();

if (dictTrailer != null && dictTrailer.isDictionary())
{
        PdfObject objInfo = PdfReader.getPdfObject(dictTrailer.get(PdfName.INFO));
       
        if (objInfo != null && objInfo.isDictionary())
        {
                PdfDictionary infoDic = (PdfDictionary)objInfo;
               
                for (Map.Entry<String, String>entry : hmpInfo.entrySet())
                {
                        if (entry.getValue().length() == 0)
                        {
                                infoDic.remove(new PdfName(entry.getKey()));
                        }
                        else
                        {
                                infoDic.put(new PdfName(entry.getKey()),
                                 new PdfString(entry.getValue(), PdfObject.TEXT_UNICODE));
                        }
                }
        }
}

pdr.getCatalog().remove(PdfName.METADATA);

Leave a Reply

Spam protection by WP Captcha-Free