ExifTool Forum

ExifTool => Archives => Topic started by: Archive on May 12, 2010, 08:54:16 AM

Title: Extracting XMP from PDF
Post by: Archive on May 12, 2010, 08:54:16 AM
[Originally posted by herm on 2008-03-12 12:43:53-07]

Dear Forummembers,

I have the following problem and could not solve it by searching the forum:

The following snippet is supposed to extract and Print the XMP-dc:Creator field, which as I understood the documentation get preference before tags like XMP-pdf:Creator and all other tags. But I dont get this value.
What I get instead is (as far as I understand it) the legacy PDF information (?) which I can see in a Texteditor opening the PDF: "'/Creator(PFU ScanSnap Manager 4.1.12)/"

Code:
$exifTool = new Image::ExifTool;
my $info = $exifTool->ExtractInfo("$file");
my $valCreator = $exifTool->GetValue('Creator');
print "$valCreator";

What am I doing wrong?
Title: Re: Extracting XMP from PDF
Post by: Archive on May 12, 2010, 08:54:16 AM
[Originally posted by herm on 2008-03-12 12:45:54-07]

I am also confused about the TagIDs regarding XMP. Normally TagID and TagName are different, but for XMP they are the same, aren't they?

Thanks a lot!
Title: Re: Extracting XMP from PDF
Post by: Archive on May 12, 2010, 08:54:16 AM
[Originally posted by exiftool on 2008-03-12 13:02:11-07]

Your problem is that you need to call GetInfo() after ExtractInfo().
Either that, or just call ImageInfo().

Code:
$exifTool = new Image::ExifTool;
my $success = $exifTool->ExtractInfo($file);
my $info = $exifTool->GetInfo('Creator');
my $tagKey;
foreach $tagKey (%$info) {
    print "$$info{$tagKey}\n";
}

Note that you may get more than one creator tag.  If you just
want the XMP-dc, you should specify 'XMP-dc:Creator' in the
GetInfo() call, but you still need to get the tag key from the
$info hash because it isn't necessarily the same as the tag name.

I think part of your problem is understanding the difference
between a tag name (the handle used to refer to a tag), a
tag ID (the identification name or number actually written to
the file), and a tag key (the key used in the tag info hash
to access a value).

TagID and tag name are the same for most XMP tags, but not all.

I hope this helps.

- Phil
Title: Re: Extracting XMP from PDF
Post by: Archive on May 12, 2010, 08:54:17 AM
[Originally posted by herm on 2008-03-12 14:37:44-07]

Thank you so much. I think I understood the Extraction a little better now.
What I am actually trying to do is to put the Value of (for example) XMP-dc:Creator into a value like $valCreator, so that I can later display it in a TextField.

I still did not understand how to do this inside of the foreach call. Can I create variables with the corresponding Tag names and put the value of the tag into them inside the foreach call?

I needed the first value that the GetValue returns so I did it now with this terrible hack, but there has to be a much better way. Sorry, I am just beginning to learn programming and to do Perl.

Code:
$exifTool = new Image::ExifTool;
my $success = $exifTool->ExtractInfo("$file");
my $info = $exifTool->GetInfo('XMP-dc:Creator');
my $tagKey;
@arrayCreator = "";
$NumAr = "1";
   foreach $tagKey (%$info) {
       $arrayCreator[$NumAr] = $exifTool->GetValue($tagKey);
       # print "$$info{$tagKey}\n";
       # print "$arrayCreator[$NumAr] $NumAr hummer\n";
       ++$NumAr;
   }
$win->TextCreator->Text("$arrayCreator[1]"); # This fills the Textbox

Thank you so much for your help!
Title: Re: Extracting XMP from PDF
Post by: Archive on May 12, 2010, 08:54:17 AM
[Originally posted by herm on 2008-03-12 14:51:20-07]

Sorry, I had left two debug print calls as comments in the code
Here the cleaned code:

Code:
$exifTool = new Image::ExifTool;
my $success = $exifTool->ExtractInfo("$file");
my $info = $exifTool->GetInfo('XMP-dc:Creator');
   my $tagKey;
   @arrayCreator = "";
   $NumAr = "1";
   foreach $tagKey (%$info) {
       $arrayCreator[$NumAr] = $exifTool->GetValue($tagKey);
       ++$NumAr;
   }
Title: Re: Extracting XMP from PDF
Post by: Archive on May 12, 2010, 08:54:17 AM
[Originally posted by exiftool on 2008-03-12 14:56:18-07]

There are many, many ways to do what you want in Perl.
If you only want the value of one tag, perhaps the easiest
way is this:

Code:
$exifTool = new Image::ExifTool;
my $success = $exifTool->ExtractInfo($file);
my $info = $exifTool->GetInfo('XMP-dc:Creator');
my ($valCreator) = values %$info;
$win->TextCreator->Text($valCreator); # This fills the Textbox

This code takes the first value from $info and sticks it in $valCreator.
(The brackets are necessary because "values" returns a list.)
Of course, the value could be undefined if the tag doesn't exist, so
you should also add a check for this.

- Phil
Title: Re: Extracting XMP from PDF
Post by: Archive on May 12, 2010, 08:54:17 AM
[Originally posted by herm on 2008-03-12 14:57:23-07]

I will check this out! Thank you again!
Title: Re: Extracting XMP from PDF
Post by: Archive on May 12, 2010, 08:54:17 AM
[Originally posted by exiftool on 2008-03-12 14:58:32-07]

Oh. But then, if you only want one tag, you should use ImageInfo()
instead of ExtractInfo() and GetInfo():

Code:
$exifTool = new Image::ExifTool;
my $info = $exifTool->ImageInfo($file, 'XMP-dc:Creator');
my ($valCreator) = values %$info;
$win->TextCreator->Text($valCreator); # This fills the Textbox