Easiest way to read PDF(/A) metadata from a Delphi app?
For PDF/A documents which contain additional information (like barcode data) in their embedded metadata, is there a simple way to access this information from a Delphi application - instead of using a full-feat开发者_StackOverflow社区ured PDF reader / writer application library? (For example open source command line tools)
As far as I know the PDF metadata is just available in plain text in the PDF/A document.
So you can search for the <x:xmpmeta
pattern in the file, then the whole metadata is available here, until </x:xmpmeta>
. Then you retrieve the XML content.
function ExtractPDFMetadata(const aPDFFileName: TFileName): UTF8String;
var tmp: RawByteString;
i: integer;
begin
with TFileStream.Create(aPDFFileName,fmOpenRead) do
try
SetLength(tmp,Size);
Read(tmp[1],Size);
finally
Free;
end;
result := '';
i := pos('<x:xmpmeta',tmp);
if i=0 then exit;
delete(tmp,1,i-1);
i := pos('</x:xmpmeta>',tmp);
if i=0 then exit;
result := copy(tmp,1,i+12);
end;
Then this UTF8String could be used to parse the XML content, as usual.
You can also locate this XML content using the "Metadata" kind of entry in the PDF catalog: this will be faster (don't have the parse the file), but will need much extra coding.
精彩评论