Error writing to PDF XmpMetadata. Argument exception

Hello,

I am trying to write metadata to a PDF file using Aspose.pdf library but get this exception:
Error writing to PDF XmpMetadata. Argument exception. \u001f’, hexadecimal value 0x1F, is an invalid character

Could I please get some help in getting this resolved?

Thanks

@malvikasharma

Thanks for contacting support.

Would you please share your sample PDF document along with sample code snippet which you are trying to set meta data. We will test the scenario in our environment and address it accordingly.

Hello Asad,

The file FileExample.pdf is (Dropbox - Adobe PDF - Simplify your life)

the sample code that we are using to write xmp metadata is:

using (var pdfDocument = new Document(stream))

{

if (pdfDocument.Metadata[TMIKey] == null)

{

pdfDocument.Metadata.RegisterNamespaceUri(TMINamespacePrefix, TMINamespace);

res.HasExistingMetadata = ExistingMetadata.No;

}

else

{

res.HasExistingMetadata = ExistingMetadata.Yes;

res.Messages.Add(

new MetadataMessage

{

Level = MetadataMessage.MessageLevel.Debug,

Message = “File has existing TMI Metadata, updating.”

});

}

var metadataString = Metadata.ToString(metadata, Metadata.Parser.Xml);

pdfDocument.Metadata[TMIKey] = metadataString;

pdfDocument.Save(stream);

res.Result = ResultCode.Success;

res.Messages.Add(

new MetadataMessage

{

Level = MetadataMessage.MessageLevel.Debug,

Message = $“Successfully wrote {metadataString.Length} characters to PDF XmpMetadata.”

});

}

}

Thank you

@malvikasharma

Thanks for sharing sample PDF and code snippet.

Please also share the values of TMINamespacePrefix, TMINamespace, TMIKey and metadataString variables being used in the code snippet, so that the code can be tested accordingly.

Hello Asad,

Please find below the values for the requested fields:

  1. TMINamespacePrefix - TMI
  2. TMINamespace - http://titus.com/tmi/1.0/
  3. TMIKey - TMI:Metadata
  4. metadatastring - <metadata xmlns:m=\"http://www.titus.com/ns/Asseco\" id=\"3144931c-d672-4449-877a-a143ae8eead1\"><m:Classification value=\"C3\"><alt>Classification=C3</alt></m:Classification></metadata>

Thank you

@malvikasharma

Thanks for providing requested details.

We have tested the scenario in our environment using your code snippet with Aspose.PDF for .NET 18.9 and were unable to notice any issue. The PDF was generated without throwing any exception. For your kind reference, an output PDF is also attached. MetaDataUpdated.pdf (382.8 KB)

Would you please try with latest version of the API and in case you still face any issue, please share a sample console application which is able to reproduce the error you are facing. We will again test the scenario in our environment and address it accordingly.

Thank you Asad, for your help. I don’t get any exceptions using 18.9 version and the metadata is added without any issues.

@malvikasharma

Thanks for your kind feedback.

It is good to know that things are working fine on your side by using latest version. Please keep using our API and in case of any further assistance, please feel free to let us know.