ExifTool Forum

ExifTool => The Image::ExifTool API => Topic started by: sumit07 on November 16, 2012, 04:09:57 AM

Title: pdf Custom Properties
Post by: sumit07 on November 16, 2012, 04:09:57 AM
Hi Phil,

I have added a user defined property to the Info Dictionary of a pdf using the below code,

%Image::ExifTool::UserDefined =
   (
       'Image::ExifTool::PDF::Info' =>
      {
              Test => { },
       },
   );

Now i want to programmatically delete this added property.Please help me .

When I try to delete this property by setting the value as empty string Test= . But this does not work. Instead  the property still remains with no value.
Title: Re: pdf Custom Properties
Post by: Phil Harvey on November 16, 2012, 07:07:40 AM
Why do you say that "-test=" sets the value to an empty string?  After doing this, exiftool should not the "Test" tag at all.

> exiftool a.pdf -test=yes
    1 image files updated

> exiftool a.pdf -test
Test                            : yes

> exiftool a.pdf -test=
    1 image files updated

> exiftool a.pdf -test

>


- Phil
Title: Re: pdf Custom Properties
Post by: sumit07 on November 18, 2012, 10:01:07 AM
Hi Phil

Thanks for the reply. I have another issue. Please help me with this too.

Here is my code. I am trying to add two properties dynamically passing the property name one after the other. But with this code only the 1st property gets added and 2nd property adding says "TAG does not exist".

&addProperty("Test");
&addProperty("TestAgain");

sub addProperty
{
   my $Name = shift;
   
   %Image::ExifTool::UserDefined = (
            'Image::ExifTool::PDF::Info' => {
                  $Name => {}
               }
         );
   
   $exiftool->SetNewValue($Name,"Test");
}
Title: Re: pdf Custom Properties
Post by: Phil Harvey on November 18, 2012, 11:35:47 AM
Yes.  This won't work because the UserDefined hash is processed only once.  This is actually quite complicated because I never designed an interface to add writable tags dynamically.  The way things normally work is that ExifTool adds tags to a table only when the table is first loaded.  To add tags dynamically after this, you would need to use some undocumented API functions:

use Image::ExifTool::TagLookup;

sub addProperty
{
   my $Name = shift;
   my %tagInfo = ( Writable => 'string' );

   # add tag to writable lookup
   Image::ExifTool::TagLookup::AddTags({ $Name => \%tagInfo }, 'Image::ExifTool::PDF::Info');

   # add tag to the actual table
   my $table = Image::ExifTool::GetTagTable('Image::ExifTool::PDF::Info');
   Image::ExifTool::AddTagToTable($table, $Name, \%tagInfo);

   $exiftool->SetNewValue($Name,"Test");
}


I think this should work, but let me know if you have any problems.

- Phil
Title: Re: pdf Custom Properties
Post by: sumit07 on November 18, 2012, 12:03:28 PM

Thanks a lot Phil. Now I am able to add properties and remove them too.
U r a genius. :)

-sumit
Title: Re: pdf Custom Properties
Post by: sumit07 on November 19, 2012, 08:57:15 AM
Hi Phil

Suppose I have two user defined properties in the PDF Info eg "Test1, Test2".
I use the below code to find the group of the properties present in the file and delete user defined properties ie Test1 and Test2 present in PDF Info using the previous code that u shared.

foreach my $tag ($exiftool->GetFoundTags('Group0'))
      {
         my    $propertyName = Image::ExifTool::GetTagName($tag);
               if($propertyName eq "Test1" || $propertyName eq "Test2")
{
         $group = $exiftool->GetGroup($propertyName);
                   print $group;
               if ($group eq "PDF")
             {
               remProp($propertyName);
              }
}
}

sub remProp
{
my $Name = shift;
my %tagInfo = ( Writable => 'string' );

   # add tag to writable lookup
   Image::ExifTool::TagLookup::AddTags({ $Name => \%tagInfo }, 'Image::ExifTool::PDF::Info');

   # add tag to the actual table
   my $table = Image::ExifTool::GetTagTable('Image::ExifTool::PDF::Info');
   Image::ExifTool::AddTagToTable($table, $Name, \%tagInfo);
      
   $exiftool->SetNewValue($Name);
           
   $exiftool->WriteInfo("pdf.pdf");
}

With this code the first property gets deleted but the 2nd does not because for the 2nd property there is no group name printed but it should have been "PDF".  I dont understand the reason behind this could u please help me .

-sumit
Title: Re: pdf Custom Properties
Post by: Phil Harvey on November 19, 2012, 09:28:59 AM
Hi Sumit,

There are 2 problems here:

1) You should call GetGroup() with $tag instead of $propertyName. -- This isn't causing a problem now, but it would if there were duplicate tags with the same name.

2) You should use a different ExifTool object to do the writing in remProp because calling WriteInfo() resets all of the tags in $exiftool from your last call to ImageInfo().  This is causing ExifTool to forget the group names for the tag.  An alternative (and preferred) method would be to not call WriteInfo() until after you have set all of the new values for your tags.  The way you are doing it here the file will be written multiple times, which is very slow.

- Phil
Title: Re: pdf Custom Properties
Post by: sumit07 on November 19, 2012, 10:10:09 AM
Thanks Phil It works a charm :)
Title: Re: pdf Custom Properties
Post by: sumit07 on January 22, 2013, 04:05:26 AM
Hi Phil,

I am facing another issue.
Suppose when i try to add a user defined tag to PDF INFO with value as "TEST VALUE" using the below statements

<code>

$wm_name  = shift;

# add tag to writable lookup
   Image::ExifTool::TagLookup::AddTags({ $wm_name => \%tagInfo }, 'Image::ExifTool::PDF::Info');

   # add tag to the actual table
   Image::ExifTool::AddTagToTable($table, $wm_name, \%tagInfo);

  ($add,$error) = $exiftool->SetNewValue($wm_name,"source");

</code>


I gives the following error:
Tag 'TEST' does not exist . It is not recognising the whole word "TEST AGAIN" some how.
Could you plz help

-sumit
Title: Re: pdf Custom Properties
Post by: Phil Harvey on January 22, 2013, 07:16:49 AM
Hi Sumit,

You need to add this as a user-defined tag.  See the sample config file (https://exiftool.org/config.html) for examples.

- Phil
Title: Re: pdf Custom Properties
Post by: sumit07 on January 22, 2013, 07:44:44 AM
Hi Phil,

This isn't working It says Tag 'Test Again' doesnot exist

add("Test Again");

add
{
$wm_name = shift;

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::PDF::Info' => {
        $wm_name => { },
    },
);

($add,$error) = $exiftool->SetNewValue($wm_name,"$SID $wm_value ".&date_time." $source");

}



-sumit
Title: Re: pdf Custom Properties
Post by: Phil Harvey on January 22, 2013, 08:01:47 AM
This will work, provided:

1) You don't call any other ExifTool functions before adding this tag.  (The user-defined tags are incorporated into the existing tag tables only once, so you can't add them dynamically after the table has already been used.)

2) You can only call your "add" routine once, because the second time will overwrite the previous UserDefined hash.

3) You must use valid tag names.  Valid characters are a-z, A-Z, 0-9, - and _.

- Phil
Title: Re: pdf Custom Properties
Post by: sumit07 on January 22, 2013, 08:15:07 AM
hi Phil,

That means we cannot add string that has spaces

-sumit
Title: Re: pdf Custom Properties
Post by: Phil Harvey on January 22, 2013, 08:18:53 AM
None of the ExifTool tag names have spaces. See FAQ number 2 (https://exiftool.org/faq.html#Q2) for an explanation.

- Phil
Title: Re: pdf Custom Properties
Post by: sumit07 on January 22, 2013, 08:21:20 AM
Hi Phil,


So is there any way to add key-value pair to PDF Info with spaces in the key

-sumit
Title: Re: pdf Custom Properties
Post by: Phil Harvey on January 22, 2013, 08:26:58 AM
Yes, this is how to define a tag with a name that is different from the ID:

%Image::ExifTool::UserDefined = (
    'Image::ExifTool::PDF::Info' => {
        'ID with spaces' => {
            Name => 'SomeTagName',
            Description => 'Some description',
        },
    },
);


- Phil