Working with metadata in PNG images

Reading PNG metadata properties

The GroupDocs.Metadata API supports extracting format-specific information from PNG images.

The following are the steps to read the native PNG metadata.

  1. Load a PNG image
  2. Get the root metadata package
  3. Extract  the native metadata package using PngRootPackage.getPngPackage
  4. Read the PNG metadata properties

advanced_usage.managing_metadata_for_specific_formats.image.png.PngReadTextChunks

try (Metadata metadata = new Metadata(Constants.InputPng)) {
    PngRootPackage root = metadata.getRootPackageGeneric();
    for (PngTextChunk chunk : root.getPngPackage().getTextChunks()) {
  
        System.out.println(chunk.getKeyword());
        System.out.println(chunk.getText());
  
        if (chunk instanceof PngCompressedTextChunk) {
            PngCompressedTextChunk compressedChunk = (PngCompressedTextChunk) chunk;
            System.out.println(compressedChunk.getCompressionMethod());
        }
  
        if (chunk instanceof PngInternationalTextChunk) {
            PngInternationalTextChunk internationalChunk = (PngInternationalTextChunk) chunk;
            System.out.println(internationalChunk.isCompressed());
            System.out.println(internationalChunk.getLanguage());
            System.out.println(internationalChunk.getTranslatedKeyword());
        }
    }
}

Working with XMP metadata

GroupDocs.Metadata for Java allows managing XMP metadata in PNG images. For more details please refer to the following guide: Working with XMP Metadata.

The code sample below shows how to update XMP properties in a PNG image. If the image has no XMP package or the required schemes yet, they are created first.

advanced_usage.managing_metadata_for_specific_formats.image.png.PngUpdateXmpProperties

try (Metadata metadata = new Metadata(Constants.PngWithXmp)) {
    IXmp root = (IXmp) metadata.getRootPackage();

    if (root.getXmpPackage() == null) {
        root.setXmpPackage(new XmpPacketWrapper());
    }

    if (root.getXmpPackage().getSchemes().getDublinCore() == null) {
        root.getXmpPackage().getSchemes().setDublinCore(new XmpDublinCorePackage());
    }
    root.getXmpPackage().getSchemes().getDublinCore().setFormat("image/png");
    root.getXmpPackage().getSchemes().getDublinCore().setRights("Copyright (C) 2011-2026 GroupDocs. All Rights Reserved.");
    root.getXmpPackage().getSchemes().getDublinCore().setSubject("test");

    if (root.getXmpPackage().getSchemes().getXmpBasic() == null) {
        root.getXmpPackage().getSchemes().setXmpBasic(new XmpBasicPackage());
    }
    root.getXmpPackage().getSchemes().getXmpBasic().setCreateDate(new Date());
    root.getXmpPackage().getSchemes().getXmpBasic().setLabel("GroupDocs");
    root.getXmpPackage().getSchemes().getXmpBasic().setRating(5);
    root.getXmpPackage().getSchemes().getXmpBasic().setCreatorTool("GroupDocs.Metadata");
    // ...

    metadata.save(Constants.OutputPng);
}

Working with EXIF metadata

The GroupDocs.Metadata API supports handling EXIF metadata in PNG images. Please find appropriate code samples in the Working with EXIF Metadata section.

More resources

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

PNG metadata management demo App

The PNG Metadata Editing app is available to view and edit metadata of PNG images, including text chunks, XMP, EXIF, and other properties.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.