Sample code for 30+ languages & platforms
Delphi DLL

Extract Metadata XML from PDF

See more PDF Signatures Examples

Demonstrates how to extract the metadata XML from a PDF.

Chilkat Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, StringBuilder, Pdf, Xml;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
pdf: HCkPdf;
sbXml: HCkStringBuilder;
xml: HCkXml;
x_xmpmeta_xmlns_x: PWideChar;
x_xmpmeta_x_xmptk: PWideChar;
rdf_RDF_xmlns_rdf: PWideChar;
rdf_Description_rdf_about: PWideChar;
rdf_Description_xmlns_xmp: PWideChar;
rdf_Description_xmlns_dc: PWideChar;
rdf_Description_xmlns_xmpMM: PWideChar;
rdf_Description_xmlns_pdf: PWideChar;
rdf_Description_xmlns_xmpRights: PWideChar;
xmp_ModifyDate: PWideChar;
xmp_CreateDate: PWideChar;
xmp_MetadataDate: PWideChar;
xmp_CreatorTool: PWideChar;
dc_format: PWideChar;
rdf_li_xml_lang: PWideChar;
rdf_li: PWideChar;
i: Integer;
count_i: Integer;
xmpMM_DocumentID: PWideChar;
xmpMM_InstanceID: PWideChar;
pdf_Producer: PWideChar;
pdf_Keywords: PWideChar;
xmpRights_WebStatement: PWideChar;

begin
success := False;

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

pdf := CkPdf_Create();

success := CkPdf_LoadFile(pdf,'qa_data/pdf/blank_with_metadata.pdf');
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

sbXml := CkStringBuilder_Create();
// Note: Not all PDF files have metadata.  Metadata is optional.
success := CkPdf_GetMetadata(pdf,sbXml);
if (success = False) then
  begin
    Memo1.Lines.Add(CkPdf__lastErrorText(pdf));
    Exit;
  end;

xml := CkXml_Create();
CkXml_LoadSb(xml,sbXml,True);

Memo1.Lines.Add(CkXml__getXml(xml));

// 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>

x_xmpmeta_xmlns_x := CkXml__getAttrValue(xml,'xmlns:x');
x_xmpmeta_x_xmptk := CkXml__getAttrValue(xml,'x:xmptk');
rdf_RDF_xmlns_rdf := CkXml__chilkatPath(xml,'rdf:RDF|(xmlns:rdf)');
rdf_Description_rdf_about := CkXml__chilkatPath(xml,'rdf:RDF|rdf:Description|(rdf:about)');
rdf_Description_xmlns_xmp := CkXml__chilkatPath(xml,'rdf:RDF|rdf:Description|(xmlns:xmp)');
rdf_Description_xmlns_dc := CkXml__chilkatPath(xml,'rdf:RDF|rdf:Description|(xmlns:dc)');
rdf_Description_xmlns_xmpMM := CkXml__chilkatPath(xml,'rdf:RDF|rdf:Description|(xmlns:xmpMM)');
rdf_Description_xmlns_pdf := CkXml__chilkatPath(xml,'rdf:RDF|rdf:Description|(xmlns:pdf)');
rdf_Description_xmlns_xmpRights := CkXml__chilkatPath(xml,'rdf:RDF|rdf:Description|(xmlns:xmpRights)');
xmp_ModifyDate := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|xmp:ModifyDate');
xmp_CreateDate := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|xmp:CreateDate');
xmp_MetadataDate := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|xmp:MetadataDate');
xmp_CreatorTool := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|xmp:CreatorTool');
dc_format := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|dc:format');
rdf_li_xml_lang := CkXml__chilkatPath(xml,'rdf:RDF|rdf:Description|dc:title|rdf:Alt|rdf:li|(xml:lang)');
rdf_li := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|dc:title|rdf:Alt|rdf:li');
rdf_li := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|dc:creator|rdf:Bag|rdf:li');
rdf_li_xml_lang := CkXml__chilkatPath(xml,'rdf:RDF|rdf:Description|dc:description|rdf:Alt|rdf:li|(xml:lang)');
rdf_li := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|dc:description|rdf:Alt|rdf:li');
i := 0;
count_i := CkXml_NumChildrenHavingTag(xml,'rdf:RDF|rdf:Description|dc:subject|rdf:Bag|rdf:li');
while i < count_i do
  begin
    CkXml_putI(xml,i);
    rdf_li := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|dc:subject|rdf:Bag|rdf:li[i]');
    i := i + 1;
  end;

xmpMM_DocumentID := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|xmpMM:DocumentID');
xmpMM_InstanceID := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|xmpMM:InstanceID');
pdf_Producer := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|pdf:Producer');
pdf_Keywords := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|pdf:Keywords');
xmpRights_WebStatement := CkXml__getChildContent(xml,'rdf:RDF|rdf:Description|xmpRights:WebStatement');

CkPdf_Dispose(pdf);
CkStringBuilder_Dispose(sbXml);
CkXml_Dispose(xml);

end;