Error writing to PDF XmpMetadata, PDFFileInfo is not Initialized

Hello,

I am trying to write metadata to a PDF file using Adobe.pdf library but get this exception:
“PdfFileInfo is not initialized. Use constructors with parameters or properties for initialization. (Reason: Value does not fall within the expected range.: \u0001)”.

Any idea what does this mean?

Thanks

@malvikasharma

Thanks for contacting support.

Would you please share your sample PDF along with the code snippet you are using at your side. We will test the scenario in our environment and address it accordingly.

Hello Asad,

The File 10060.pdf is Dropbox - Adobe PDF - Simplify your life

Below is the sample of our code where we are applying the XMP metadata to the pdf:

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”>Classification=C3</m:Classification>

I get this exception at fileinfo.Isencrypted in this part of the code:

using (var stream = target.CreateStream(FileAccess.ReadWrite))
				{
					PdfHelper.Instance.SetLicense();
					var fileInfo = GetPdfFileInfo(stream);
					if (fileInfo.IsEncrypted)
					{
						res.Messages.Add(
							new MetadataMessage
								{
									Level = MetadataMessage.MessageLevel.Error,
									Message = "Error writing to PDF XmpMetadata. File is encrypted."
								});
						res.Result = ResultCode.Error;
						res.ErrorMessage = "File is encrypted.";
					}
					else
					{
						// open document
						using (var pdfDocument = new Document(stream))


**Definition for GetPdfFileinfo:**
internal static PdfFileInfo GetPdfFileInfo(Stream stream)
		{
			var task = Task.Run(() => new PdfFileInfo(stream));**/calling pdfFileinfo from Aspose.pdf.facades_
			if (task.Wait(TimeSpan.FromSeconds(5)))
				return task.Result;
			throw new Exception("Timed out");
		}

Please let me know if you need more info.
Thanks

@malvikasharma

Thanks for sharing more details.

We have tested the scenario in our environment using Aspose.PDF for .NET 18.9 with both .NET Core and Framework 4.0. We were unable to replicate the issue you mentioned. Would you please try using latest version of the API and in case you still experience any issue, please share a sample console application able to reproduce the error. We will again test the scenario in our environment and address it accordingly.

Hello Asad,

Thanks for info. Is it possible to get a trial license to try that version?

Malvika

@malvikasharma

You may surely apply for a 30-days temporary license to evaluate latest version of the API. In case of any further assistance, please feel free to let us know.

Hi Asad,
I clicked on Get Temporary License but nothinghappens

@malvikasharma

You will be able to apply for temporary license while completing Get Pricing Information wizard. In case you face any difficulty while applying free temporary license, you may please post your issue in Aspose.Purchase forums where we will assist you accordingly.

Hello Asad,

He works perfect with aspose.pdf 18.9 version, thank you so much for your help:)

Malvika

The files work perfect:)

@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.