pdf Custom Properties

Started by sumit07, November 16, 2012, 04:09:57 AM

Previous topic - Next topic

sumit07

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.

Phil Harvey

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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

sumit07

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");
}

Phil Harvey

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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

sumit07


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

-sumit

sumit07

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

Phil Harvey

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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

sumit07


sumit07

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

Phil Harvey

Hi Sumit,

You need to add this as a user-defined tag.  See the sample config file for examples.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

sumit07

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

Phil Harvey

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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

sumit07

hi Phil,

That means we cannot add string that has spaces

-sumit

Phil Harvey

None of the ExifTool tag names have spaces. See FAQ number 2 for an explanation.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

sumit07

Hi Phil,


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

-sumit

Phil Harvey

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
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).