Sample code for 30+ languages & platforms
Dart

Extract Metadata XML from PDF

See more PDF Signatures Examples

Demonstrates how to extract the metadata XML from a PDF.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  // This example requires the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  final pdf = CkPdf();

  try {
    pdf.loadFile('qa_data/pdf/blank_with_metadata.pdf');
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final sbXml = CkStringBuilder();
  // Note: Not all PDF files have metadata.  Metadata is optional.
  try {
    pdf.getMetadata(sbXml);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  final xml = CkXml();
  xml.loadSb(sbXml, true);

  print(xml.getXml());

  // Sample PDF metadata XML:
  // (Code for parsing follows)

  // Use this online tool to generate parsing code from sample XML: 
  // Generate Parsing Code from XML

  // <?xml version="1.0" encoding="utf-8"?>
  // <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 9.1-c001 79.675d0f7, 2023/06/11-19:21:16        ">
  //     <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  //         <rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/" xmlns:pdf="http://ns.adobe.com/pdf/1.3/" xmlns:xmpRights="http://ns.adobe.com/xap/1.0/rights/">
  //             <xmp:ModifyDate>2024-11-27T11:33:12-06:00</xmp:ModifyDate>
  //             <xmp:CreateDate>2024-11-27T11:28:23-06:00</xmp:CreateDate>
  //             <xmp:MetadataDate>2024-11-27T11:33:12-06:00</xmp:MetadataDate>
  //             <xmp:CreatorTool>Adobe Acrobat Pro (32-bit) 24.3.20112</xmp:CreatorTool>
  //             <dc:format>application/pdf</dc:format>
  //             <dc:title>
  //                 <rdf:Alt>
  //                     <rdf:li xml:lang="x-default">Blank</rdf:li>
  //                 </rdf:Alt>
  //             </dc:title>
  //             <dc:creator>
  //                 <rdf:Bag>
  //                     <rdf:li>Chilkat Software</rdf:li>
  //                 </rdf:Bag>
  //             </dc:creator>
  //             <dc:description>
  //                 <rdf:Alt>
  //                     <rdf:li xml:lang="x-default">Blank Document</rdf:li>
  //                 </rdf:Alt>
  //             </dc:description>
  //             <dc:subject>
  //                 <rdf:Bag>
  //                     <rdf:li>blank</rdf:li>
  //                     <rdf:li>metadata</rdf:li>
  //                     <rdf:li>document</rdf:li>
  //                 </rdf:Bag>
  //             </dc:subject>
  //             <xmpMM:DocumentID>uuid:34535ffa-b632-43f1-b1fd-80cea6fdc351</xmpMM:DocumentID>
  //             <xmpMM:InstanceID>uuid:69ace620-4c54-407f-8d45-6eebc90f34c2</xmpMM:InstanceID>
  //             <pdf:Producer>Adobe Acrobat Pro (32-bit) 24.3.20112</pdf:Producer>
  //             <pdf:Keywords>blank; metadata; document</pdf:Keywords>
  //             <xmpRights:WebStatement>https://www.chilkatsoft.com/</xmpRights:WebStatement>
  //         </rdf:Description>
  //     </rdf:RDF>
  // </x:xmpmeta>

  final xXmpmetaXmlnsX = xml.getAttrValue('xmlns:x');
  final xXmpmetaXXmptk = xml.getAttrValue('x:xmptk');
  final rdfRDFXmlnsRdf = xml.chilkatPath('rdf:RDF|(xmlns:rdf)');
  final rdfDescriptionRdfAbout = xml.chilkatPath('rdf:RDF|rdf:Description|(rdf:about)');
  final rdfDescriptionXmlnsXmp = xml.chilkatPath('rdf:RDF|rdf:Description|(xmlns:xmp)');
  final rdfDescriptionXmlnsDc = xml.chilkatPath('rdf:RDF|rdf:Description|(xmlns:dc)');
  final rdfDescriptionXmlnsXmpMM = xml.chilkatPath('rdf:RDF|rdf:Description|(xmlns:xmpMM)');
  final rdfDescriptionXmlnsPdf = xml.chilkatPath('rdf:RDF|rdf:Description|(xmlns:pdf)');
  final rdfDescriptionXmlnsXmpRights = xml.chilkatPath('rdf:RDF|rdf:Description|(xmlns:xmpRights)');
  final xmpModifyDate = xml.getChildContent('rdf:RDF|rdf:Description|xmp:ModifyDate');
  final xmpCreateDate = xml.getChildContent('rdf:RDF|rdf:Description|xmp:CreateDate');
  final xmpMetadataDate = xml.getChildContent('rdf:RDF|rdf:Description|xmp:MetadataDate');
  final xmpCreatorTool = xml.getChildContent('rdf:RDF|rdf:Description|xmp:CreatorTool');
  final dcFormat = xml.getChildContent('rdf:RDF|rdf:Description|dc:format');
  var rdfLiXmlLang = xml.chilkatPath('rdf:RDF|rdf:Description|dc:title|rdf:Alt|rdf:li|(xml:lang)');
  var rdfLi = xml.getChildContent('rdf:RDF|rdf:Description|dc:title|rdf:Alt|rdf:li');
  rdfLi = xml.getChildContent('rdf:RDF|rdf:Description|dc:creator|rdf:Bag|rdf:li');
  rdfLiXmlLang = xml.chilkatPath('rdf:RDF|rdf:Description|dc:description|rdf:Alt|rdf:li|(xml:lang)');
  rdfLi = xml.getChildContent('rdf:RDF|rdf:Description|dc:description|rdf:Alt|rdf:li');
  var i = 0;
  final countI = xml.numChildrenHavingTag('rdf:RDF|rdf:Description|dc:subject|rdf:Bag|rdf:li');
  while (i < countI) {
    xml.i = i;
    rdfLi = xml.getChildContent('rdf:RDF|rdf:Description|dc:subject|rdf:Bag|rdf:li[i]');
    i++;
  }

  final xmpMMDocumentID = xml.getChildContent('rdf:RDF|rdf:Description|xmpMM:DocumentID');
  final xmpMMInstanceID = xml.getChildContent('rdf:RDF|rdf:Description|xmpMM:InstanceID');
  final pdfProducer = xml.getChildContent('rdf:RDF|rdf:Description|pdf:Producer');
  final pdfKeywords = xml.getChildContent('rdf:RDF|rdf:Description|pdf:Keywords');
  final xmpRightsWebStatement = xml.getChildContent('rdf:RDF|rdf:Description|xmpRights:WebStatement');
}