Metadata and attachments

<< Click to Display Table of Contents >>

Navigation:  Commands >

Metadata and attachments

Read Metadata

 

 

COMPDF_Get_XMPBuffer = 598

 

This command can be used to read the loaded PDF XMP metadata.

 

The IntParam is used to select the loaded PDF-File to examine. (default=0).    

 

It is also possible to provide the NAME of the PDF-File to check in the StrParam.

The Result Value = -2 if IntParam is not valid or the provided PDF filename was not loaded before.

 

Example:

   ShowMessage( WPViewPDF1.CommandGetStr(COMPDF_Get_XMPBuffer,'',0)  );

 

 

Attachments, i.e. ZUGFeRD XML

 

With WPViewPDF PLUS it is also possible to extract the attachments in a PDF file. This can be useful to extract the invoice data stored using the ZUGFeRD standard.

 

COMPDF_Attachment_List = 590

 

Get the number of the attachments in the loaded PDF file.

Use the Index in next commands.

 

COMPDF_Attachment_Command = 589

 

Command to work with attachments. COMPDF_Attachment_List must be used first to initialize the list of attachments.

 

IntPar=id of attachment

StrParam=command. Currently supported are:

 'delete'  to delete attachment (it will not be written when the PDF is exported)

 'undelete'  to un-delete attachment (it will be written when the PDF is exported)

 'toggledelete' toggles between the two states, returns the new state (1=deleted)

 'isdeleted' checks the deletion state, returns 1 if the deltion flag is set

 

 

COMPDF_Attachment_GetProp  = 591

 

Get the name of a certain annotation. StrParam selects which PDF property to read. To read the attachment name pass an empty string parameter.

 

COMPDF_Attachment_GetData  = 592

 

Get the attachment data as memory buffer (Result = length).

StrParam can be "F" or "UF"

IntParam = index

 

The IntParam is the index of the attachment in the file.

 

Note: Use COMPDF_MakeGetMEMORY to read the data.

 

 

Example - Delphi:

 

On a form there is a TListbox to show all attachments, a SaveDialog and a button to start the extraction process.

 

Attachments_5

 

This code updates the listbox with a list of all attachments

 

procedure TWPViewListAttachments.Update;

var i, j : Integer;

begin

   ListBox1.Items.Clear;

   j := WPViewPDF.Command(COMPDF_Attachment_List);

   for i := 0 to j-1 do

     ListBox1.Items.Add( WPViewPDF.CommandGetStr( COMPDF_Attachment_GetProp, '', i ) );

end;

 

This code extracts the selected data

 

procedure TWPViewListAttachments.btnSaveClick(Sender: TObject);

var mem : TMemoryStream;

    l : Integer;

begin

   if ListBox1.ItemIndex>=0 then

   try

      mem := TMemoryStream.Create;

      l := WPViewPDF.Command( 

             COMPDF_Attachment_GetData, ListBox1.ItemIndex );

      if l>=0 then

      begin

        mem.SetSize(l);

        WPViewPDF.CommandEx( COMPDF_MakeGetMEMORY, 

        {$IFDEF WIN64} IntPtr {$ELSE} Cardinal {$ENDIF}( mem.Memory ) );

 

        SaveDialog1.FileName := WPViewPDF.CommandGetStr(

          COMPDF_Attachment_GetProp, 

             'F', ListBox1.ItemIndex  );

 

        if SaveDialog1.Execute then

        begin

           mem.SaveToFile(SaveDialog1.FileName);

        end;

      end;

   finally

      mem.Free;

   end;

end;

 

 

This code toggles the deletion state:

 

procedure TWPViewListAttachments.btnToggleDeletionClick(Sender: TObject);
var i, j : Integer;
begin
  i := ListBox1.ItemIndex;
  if i>=0 then
  begin
     // Toggle the deletion state (returns current state)
     j := WPViewPDF.CommandStrEx( 

           COMPDF_Attachment_Command, 'toggledelete', i);
     // Read the CURRENT state
     if  j=1 then
          ListBox1.Items[i] :=  'deleted: ' 

              + WPViewPDF.CommandGetStr( 

                   COMPDF_Attachment_GetProp, '', i )
     else ListBox1.Items[i] :=  

                WPViewPDF.CommandGetStr( 

                   COMPDF_Attachment_GetProp, '', i ) ;
  end;
end;

 

 

Example C#

 

This example uses a sub menu which is filled with all attachments in a PDF file. If the user clicks on one item, the command COMPDF_Attachment_GetData is used to extract the data. The data is retrieved from the viewer control with the function GetMemory which was implmented in the wrapper class.

 

This method fill the sub menu:

 

      private void infoToolStripMenuItem_DropDownOpening(object sender, EventArgs e)

       {

           menFileattachment.DropDownItems.Clear();

          int j = pdfViewer1.Command(commands.COMPDF_Attachment_List);

          for(int i = 0; i<j;i++)

           {

               System.Windows.Forms.ToolStripMenuItem men =

                  new System.Windows.Forms.ToolStripMenuItem();

               men.Text = pdfViewer1.CommandGetStr(

                  commands.COMPDF_Attachment_GetProp, "", i );

               men.Tag = i;

               men.Click += new System.EventHandler(OnClickAttachment);

               menFileattachment.DropDownItems.Add(men);

           }

          if(j<=0) menFileattachment.DropDownItems.Add("<empty>").Enabled = false;

 

       }

 

This method handles a click:

 

      private void OnClickAttachment(object sender, EventArgs e)

       {

           System.Windows.Forms.ToolStripMenuItem men =

               sender as System.Windows.Forms.ToolStripMenuItem;

          int l = pdfViewer1.Command(commands.COMPDF_Attachment_GetData, (int)men.Tag);

          if (l > 0)

           {

              byte[] databytes = pdfViewer1.GetMemory();

               saveFileDialog2.FileName = men.Text;

              if ( saveFileDialog2.ShowDialog()==System.Windows.Forms.DialogResult.OK )

               {

                   System.IO.FileStream file = new System.IO.FileStream(

                         saveFileDialog2.FileName,  System.IO.FileMode.Create );

                   file.Write(databytes,0,databytes.Length);

                   file.Close();

               }

           }

       }

 

 

Info: This is how GetMemory was implemented:

 

      public byte[] GetMemory()

       {

          int l = Command(commands.COMPDF_MakeGetMEMORY, null);

          IntPtr buffer = Marshal.AllocCoTaskMem(l + 16);

          byte[] databytes = new byte[l];

           Command(commands.COMPDF_MakeGetMEMORY, buffer);

          Marshal.Copy(buffer, databytes, 0, l);

          Marshal.FreeCoTaskMem(buffer);

          return databytes;

       }

 

 

 

COMPDF_Attachment_AddAF = 593

 

This command adds a structure to hold and embedded file. Result value is the index or -1 to be used in next commands.

The Index is different to the one used with COMPDF_Attachment_List! The added attachment will be listed by COMPDF_Attachment_List.

 

COMPDF_Attachment_SetProp  = 594

 

Set the name of a certain annotation. StrParam selects which property to modify. Currently only "Desc" is supported.

 

COMPDF_Attachment_SetData  = 595

 

Set the attachment data. The attachment is loaded from the file with the name strparam. The time stamp is also used.

As second parameter the index value returned by COMPDF_Attachment_AddAF must be used.

 

Example:

 

procedure TForm1.AddAttachmentClick(Sender: TObject);

var id : Integer;

begin

  with TOpenDialog.Create(Self) do

  try

    Filter := '*.*';

    if Execute then

    begin

      // Create a slot

      id := pdf.CommandStr(COMPDF_Attachment_AddAF, ExtractFileName(Filename) );

      if id<0 then  ShowMessage('The file was not attached') else

      begin

        // Load the embedded Data into the object with the given index

        pdf.CommandStrEx(

            COMPDF_Attachment_SetData, 

           Filename, id);

        // Set additional property (F already has been set)

        pdf.CommandStrEx(

           COMPDF_Attachment_SetProp, 

            'Desc=File was attached with WPViewPDF', id );

      end;

    end;

  finally

    Free;

  end;

end;