Sample code for 30+ languages & platforms
Rust

Extract Metadata XML from PDF

See more PDF Signatures Examples

Demonstrates how to extract the metadata XML from a PDF.

Chilkat Rust Downloads

Rust

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

let pdf = chilkat::Pdf::new();

if pdf.load_file("qa_data/pdf/blank_with_metadata.pdf").is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

let sb_xml = chilkat::StringBuilder::new();
// Note: Not all PDF files have metadata.  Metadata is optional.
if pdf.get_metadata(&sb_xml).is_err() {
    println!("{}", pdf.last_error_text());
    return;
}

let xml = chilkat::Xml::new();
let _ = xml.load_sb(&sb_xml, true);

println!("{}", xml.get_xml().unwrap_or_default());

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

let x_xmpmeta_xmlns_x = xml.get_attr_value("xmlns:x").unwrap_or_default();
let x_xmpmeta_x_xmptk = xml.get_attr_value("x:xmptk").unwrap_or_default();
let rdf_rdf_xmlns_rdf = xml.chilkat_path("rdf:RDF|(xmlns:rdf)").unwrap_or_default();
let rdf_description_rdf_about = xml.chilkat_path("rdf:RDF|rdf:Description|(rdf:about)").unwrap_or_default();
let rdf_description_xmlns_xmp = xml.chilkat_path("rdf:RDF|rdf:Description|(xmlns:xmp)").unwrap_or_default();
let rdf_description_xmlns_dc = xml.chilkat_path("rdf:RDF|rdf:Description|(xmlns:dc)").unwrap_or_default();
let rdf_description_xmlns_xmp_mm = xml.chilkat_path("rdf:RDF|rdf:Description|(xmlns:xmpMM)").unwrap_or_default();
let rdf_description_xmlns_pdf = xml.chilkat_path("rdf:RDF|rdf:Description|(xmlns:pdf)").unwrap_or_default();
let rdf_description_xmlns_xmp_rights = xml.chilkat_path("rdf:RDF|rdf:Description|(xmlns:xmpRights)").unwrap_or_default();
let xmp_modify_date = xml.get_child_content("rdf:RDF|rdf:Description|xmp:ModifyDate").unwrap_or_default();
let xmp_create_date = xml.get_child_content("rdf:RDF|rdf:Description|xmp:CreateDate").unwrap_or_default();
let xmp_metadata_date = xml.get_child_content("rdf:RDF|rdf:Description|xmp:MetadataDate").unwrap_or_default();
let xmp_creator_tool = xml.get_child_content("rdf:RDF|rdf:Description|xmp:CreatorTool").unwrap_or_default();
let dc_format = xml.get_child_content("rdf:RDF|rdf:Description|dc:format").unwrap_or_default();
let mut rdf_li_xml_lang = xml.chilkat_path("rdf:RDF|rdf:Description|dc:title|rdf:Alt|rdf:li|(xml:lang)").unwrap_or_default();
let mut rdf_li = xml.get_child_content("rdf:RDF|rdf:Description|dc:title|rdf:Alt|rdf:li").unwrap_or_default();
rdf_li = xml.get_child_content("rdf:RDF|rdf:Description|dc:creator|rdf:Bag|rdf:li").unwrap_or_default();
rdf_li_xml_lang = xml.chilkat_path("rdf:RDF|rdf:Description|dc:description|rdf:Alt|rdf:li|(xml:lang)").unwrap_or_default();
rdf_li = xml.get_child_content("rdf:RDF|rdf:Description|dc:description|rdf:Alt|rdf:li").unwrap_or_default();
let mut i = 0;
let count_i = xml.num_children_having_tag("rdf:RDF|rdf:Description|dc:subject|rdf:Bag|rdf:li");
while i < count_i {
    xml.set_i(i);
    rdf_li = xml.get_child_content("rdf:RDF|rdf:Description|dc:subject|rdf:Bag|rdf:li[i]").unwrap_or_default();
    i = i + 1;
}

let xmp_mm_document_id = xml.get_child_content("rdf:RDF|rdf:Description|xmpMM:DocumentID").unwrap_or_default();
let xmp_mm_instance_id = xml.get_child_content("rdf:RDF|rdf:Description|xmpMM:InstanceID").unwrap_or_default();
let pdf_producer = xml.get_child_content("rdf:RDF|rdf:Description|pdf:Producer").unwrap_or_default();
let pdf_keywords = xml.get_child_content("rdf:RDF|rdf:Description|pdf:Keywords").unwrap_or_default();
let xmp_rights_web_statement = xml.get_child_content("rdf:RDF|rdf:Description|xmpRights:WebStatement").unwrap_or_default();