Aspose.Slides & Aspose.Cells - Metadata Could Not Set in PDF after Converting from XLSX/PPTX to PDF with PdfFormat v1.4, v1.5, v1.6, v1.7, v2.0

We have a problem with setting metadata in Pdf when we use PdfCompliance v_1_7.

First we do something in a Word, Excel or Powerpoint document and after our change we try to convert this document to Pdf with a certain Pdf.Compliance.

One problem is that we cannot use the correct PdfCompliance when we save the pptx/excel as not all compliance modes are available.

It looks like the namespace for “dc” is not set in the document:
image.png (41,4 KB)

Here is the code from my test with LinqPad:

using Aspose.Pdf;

void Main()
{	
	"Word".Dump();
	SetMetaDataWord(PdfFormat.v_1_4).Dump();
	SetMetaDataWord(PdfFormat.v_1_5).Dump();
	SetMetaDataWord(PdfFormat.v_1_6).Dump();
	SetMetaDataWord(PdfFormat.v_1_7).Dump();
	SetMetaDataWord(PdfFormat.v_2_0).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_1A).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_1B).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_2A).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_2B).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_2U).Dump();

	"Excel".Dump();
	SetMetaDataExcel(PdfFormat.v_1_4).Dump();
	SetMetaDataExcel(PdfFormat.v_1_5).Dump();
	SetMetaDataExcel(PdfFormat.v_1_6).Dump();
	SetMetaDataExcel(PdfFormat.v_1_7).Dump();
	SetMetaDataExcel(PdfFormat.v_2_0).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_1A).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_1B).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_2A).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_2B).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_2U).Dump();

	"PowerPoint".Dump();
	SetMetaDataPowerPoint(PdfFormat.v_1_4).Dump();
	SetMetaDataPowerPoint(PdfFormat.v_1_5).Dump();
	SetMetaDataPowerPoint(PdfFormat.v_1_6).Dump();
	SetMetaDataPowerPoint(PdfFormat.v_1_7).Dump();
	SetMetaDataPowerPoint(PdfFormat.v_2_0).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_1A).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_1B).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_2A).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_2B).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_2U).Dump();
}

public string SetMetaDataWord(PdfFormat pdfFormat)
{
	try {
		using (var pdfStream = new MemoryStream())
		{
			var document = new Aspose.Words.Document();
			document.Save(pdfStream, new Aspose.Words.Saving.PdfSaveOptions() { Compliance = Aspose.Words.Saving.PdfCompliance.Pdf17 });

			using (var pdfDocument = new Document(pdfStream))
			{
				using (var pdfConversionLogOutputStream = new MemoryStream())
				{
					pdfDocument.Convert(pdfConversionLogOutputStream, pdfFormat, ConvertErrorAction.Delete);
				}

				pdfDocument.Metadata["dc:subject"] = "test";
				
				return "ok";
			}
		}
	} catch (Exception e) {
		return e.Message;
	}
}

public string SetMetaDataExcel(PdfFormat pdfFormat)
{
	try
	{
		using (var pdfStream = new MemoryStream())
		{
			var document = new Aspose.Cells.Workbook();
			document.Save(pdfStream, new Aspose.Cells.PdfSaveOptions() { Compliance = Aspose.Cells.Rendering.PdfCompliance.Pdf17 });

			using (var pdfDocument = new Document(pdfStream))
			{
				using (var pdfConversionLogOutputStream = new MemoryStream())
				{
					pdfDocument.Convert(pdfConversionLogOutputStream, pdfFormat, ConvertErrorAction.Delete);
				}

				pdfDocument.Metadata["dc:subject"] = "test";

				return "ok";
			}
		}
	}
	catch (Exception e)
	{
		return e.Message;
	}
}

public string SetMetaDataPowerPoint(PdfFormat pdfFormat)
{
	try
	{
		using (var pdfStream = new MemoryStream())
		{
			var document = new Aspose.Slides.Presentation();
			document.Save(pdfStream, Aspose.Slides.Export.SaveFormat.Pdf, new Aspose.Slides.Export.PdfOptions() { Compliance = Aspose.Slides.Export.PdfCompliance.Pdf17 });

			using (var pdfDocument = new Document(pdfStream))
			{
				using (var pdfConversionLogOutputStream = new MemoryStream())
				{
					pdfDocument.Convert(pdfConversionLogOutputStream, pdfFormat, ConvertErrorAction.Delete);
				}

				pdfDocument.Metadata["dc:subject"] = "test";

				return "ok";
			}
		}
	}
	catch (Exception e)
	{
		return e.Message;
	}
}

Output:
image.png (4,8 KB)

Thanks for your help.

@jschur,
Thank you for contacting free support.

I need some time to check the problem. I will get back to you soon.

@jschur,
Thank you for your patience. We need to conduct a more in-depth study of the problem. I’ve moved this thread to Aspose.Total forum. My colleagues from Aspose.PDF team will investigate the case and find out what is wrong with PDF documents created from Excel and PowerPoint files.
@asad.ali FYI

Hi @andrey.potapov,
Hi @asad.ali,
Is there possibly a workauround for this? We have implemented and tested various ideas with ChatGPT, but without success.

Thanks for your help.

@jschur

If possible, could you please share sample PDF document that you have obtained from PPTX/Word/Excel file(s) along with respective source file? We will test the scenario in our environment and address it accordingly.

@asad.ali

First of all, I wish you all a wonderful new year :slight_smile:

Here the code again, with saving the files:

System
System.Collections
System.Collections.Generic
System.Data
System.Diagnostics
System.IO
System.Linq
System.Linq.Expressions
System.Reflection
System.Text
System.Text.RegularExpressions
System.Threading
System.Transactions
System.Xml
System.Xml.Linq
System.Xml.XPath

Aspose.Pdf

void Main()
{
	"Word".Dump();
	SetMetaDataWord(PdfFormat.v_1_4).Dump();
	SetMetaDataWord(PdfFormat.v_1_5).Dump();
	SetMetaDataWord(PdfFormat.v_1_6).Dump();
	SetMetaDataWord(PdfFormat.v_1_7).Dump();
	SetMetaDataWord(PdfFormat.v_2_0).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_1A).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_1B).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_2A).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_2B).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_2U).Dump();

	"Excel".Dump();
	SetMetaDataExcel(PdfFormat.v_1_4).Dump();
	SetMetaDataExcel(PdfFormat.v_1_5).Dump();
	SetMetaDataExcel(PdfFormat.v_1_6).Dump();
	SetMetaDataExcel(PdfFormat.v_1_7).Dump();
	SetMetaDataExcel(PdfFormat.v_2_0).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_1A).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_1B).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_2A).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_2B).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_2U).Dump();

	"PowerPoint".Dump();
	SetMetaDataPowerPoint(PdfFormat.v_1_4).Dump();
	SetMetaDataPowerPoint(PdfFormat.v_1_5).Dump();
	SetMetaDataPowerPoint(PdfFormat.v_1_6).Dump();
	SetMetaDataPowerPoint(PdfFormat.v_1_7).Dump();
	SetMetaDataPowerPoint(PdfFormat.v_2_0).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_1A).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_1B).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_2A).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_2B).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_2U).Dump();
}

public string SetMetaDataWord(PdfFormat pdfFormat)
{
	try
	{
		using (var pdfStream = new MemoryStream())
		{
			var document = new Aspose.Words.Document();
			document.Save(pdfStream, new Aspose.Words.Saving.PdfSaveOptions() { Compliance = Aspose.Words.Saving.PdfCompliance.Pdf17 });
			document.Save("c:\\Temp\\Word_Pdf17.docx");

			using (var pdfDocument = new Document(pdfStream))
			{
				using (var pdfConversionLogOutputStream = new MemoryStream())
				{
					pdfDocument.Convert(pdfConversionLogOutputStream, pdfFormat, ConvertErrorAction.Delete);
				}

				pdfDocument.Metadata["dc:subject"] = "test";

				return "ok";
			}
		}
	}
	catch (Exception e)
	{
		return e.Message;
	}
}

public string SetMetaDataExcel(PdfFormat pdfFormat)
{
	try
	{
		using (var pdfStream = new MemoryStream())
		{
			var document = new Aspose.Cells.Workbook();
			document.Save(pdfStream, new Aspose.Cells.PdfSaveOptions() { Compliance = Aspose.Cells.Rendering.PdfCompliance.Pdf17 });
			document.Save("c:\\Temp\\Excel_Pdf17.xlsx");

			using (var pdfDocument = new Document(pdfStream))
			{
				using (var pdfConversionLogOutputStream = new MemoryStream())
				{
					pdfDocument.Convert(pdfConversionLogOutputStream, pdfFormat, ConvertErrorAction.Delete);
				}

				pdfDocument.Metadata["dc:subject"] = "test";

				return "ok";
			}
		}
	}
	catch (Exception e)
	{
		return e.Message;
	}
}

public string SetMetaDataPowerPoint(PdfFormat pdfFormat)
{
	try
	{
		using (var pdfStream = new MemoryStream())
		{
			var document = new Aspose.Slides.Presentation();
			document.Save(pdfStream, Aspose.Slides.Export.SaveFormat.Pdf, new Aspose.Slides.Export.PdfOptions() { Compliance = Aspose.Slides.Export.PdfCompliance.Pdf17 });
			document.Save("c:\\Temp\\Powerpoint_Pdf17.pptx", Aspose.Slides.Export.SaveFormat.Pptx);

			using (var pdfDocument = new Document(pdfStream))
			{
				using (var pdfConversionLogOutputStream = new MemoryStream())
				{
					pdfDocument.Convert(pdfConversionLogOutputStream, pdfFormat, ConvertErrorAction.Delete);
				}

				pdfDocument.Metadata["dc:subject"] = "test";

				return "ok";
			}
		}
	}
	catch (Exception e)
	{
		return e.Message;
	}
}

I used these packages:
image.png (3,5 KB)

I created these files with my code:
Created_Word_Excel_Powerpoint_Files.zip (45,6 KB)

I hope the data helps you.

@jschur

Thanks for sharing the requested information. Please also provide the exported PDF document in which the metadata is not being set after saving using Aspose.PDF for .NET.

Hi @asad.ali,

the error looks harder than expected…

After I generated all the output files with this LINQPad script:

void Main()
{
	"Word".Dump();
	SetMetaDataWord(PdfFormat.v_1_4).Dump();
	SetMetaDataWord(PdfFormat.v_1_5).Dump();
	SetMetaDataWord(PdfFormat.v_1_6).Dump();
	SetMetaDataWord(PdfFormat.v_1_7).Dump();
	SetMetaDataWord(PdfFormat.v_2_0).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_1A).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_1B).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_2A).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_2B).Dump();
	SetMetaDataWord(PdfFormat.PDF_A_2U).Dump();
	SetMetaDataWord(PdfFormat.PDF_UA_1).Dump();

	"Excel".Dump();
	SetMetaDataExcel(PdfFormat.v_1_4).Dump();
	SetMetaDataExcel(PdfFormat.v_1_5).Dump();
	SetMetaDataExcel(PdfFormat.v_1_6).Dump();
	SetMetaDataExcel(PdfFormat.v_1_7).Dump();
	SetMetaDataExcel(PdfFormat.v_2_0).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_1A).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_1B).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_2A).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_2B).Dump();
	SetMetaDataExcel(PdfFormat.PDF_A_2U).Dump();
	SetMetaDataExcel(PdfFormat.PDF_UA_1).Dump();

	"PowerPoint".Dump();
	SetMetaDataPowerPoint(PdfFormat.v_1_4).Dump();
	SetMetaDataPowerPoint(PdfFormat.v_1_5).Dump();
	SetMetaDataPowerPoint(PdfFormat.v_1_6).Dump();
	SetMetaDataPowerPoint(PdfFormat.v_1_7).Dump();
	SetMetaDataPowerPoint(PdfFormat.v_2_0).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_1A).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_1B).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_2A).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_2B).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_A_2U).Dump();
	SetMetaDataPowerPoint(PdfFormat.PDF_UA_1).Dump();
}

public string SetMetaDataWord(PdfFormat pdfFormat)
{
	try
	{
		using (var pdfStream = new MemoryStream())
		{
			var document = new Aspose.Words.Document();
			document.Save(pdfStream, new Aspose.Words.Saving.PdfSaveOptions() { Compliance = Aspose.Words.Saving.PdfCompliance.Pdf17 });
			document.Save("c:\\Temp\\Word_Pdf17.pdf");

			using (var pdfDocument = new Document(pdfStream))
			{
				using (var pdfConversionLogOutputStream = new MemoryStream())
				{
					pdfDocument.Convert(pdfConversionLogOutputStream, pdfFormat, ConvertErrorAction.Delete);
					pdfDocument.Save($"c:\\Temp\\Word_Pdf17_Save_As_Pdf_{pdfFormat.ToString()}_Without_Meta.pdf");
				}

				pdfDocument.Metadata["dc:subject"] = "test";
				pdfDocument.Save($"c:\\Temp\\Word_Pdf17_Save_As_Pdf_{pdfFormat.ToString()}_With_Meta.pdf");

				return "ok";
			}
		}
	}
	catch (Exception e)
	{
		return e.Message;
	}
}

public string SetMetaDataExcel(PdfFormat pdfFormat)
{
	try
	{
		using (var pdfStream = new MemoryStream())
		{
			var document = new Aspose.Cells.Workbook();
			document.Save(pdfStream, new Aspose.Cells.PdfSaveOptions() { Compliance = Aspose.Cells.Rendering.PdfCompliance.Pdf17 });
			document.Save("c:\\Temp\\Excel_Pdf17.pdf");

			using (var pdfDocument = new Document(pdfStream))
			{
				using (var pdfConversionLogOutputStream = new MemoryStream())
				{
					pdfDocument.Convert(pdfConversionLogOutputStream, pdfFormat, ConvertErrorAction.Delete);
					pdfDocument.Save($"c:\\Temp\\Excel_Pdf17_Save_As_Pdf_{pdfFormat.ToString()}_Without_Meta.pdf");
				}

				pdfDocument.Metadata["dc:subject"] = "test";
				pdfDocument.Save($"c:\\Temp\\Excel_Pdf17_Save_As_Pdf_{pdfFormat.ToString()}_With_Meta.pdf");

				return "ok";
			}
		}
	}
	catch (Exception e)
	{
		return e.Message;
	}
}

public string SetMetaDataPowerPoint(PdfFormat pdfFormat)
{
	try
	{
		using (var pdfStream = new MemoryStream())
		{
			var document = new Aspose.Slides.Presentation();
			document.Save(pdfStream, Aspose.Slides.Export.SaveFormat.Pdf, new Aspose.Slides.Export.PdfOptions() { Compliance = Aspose.Slides.Export.PdfCompliance.Pdf17 });
			document.Save("c:\\Temp\\Powerpoint_Pdf17.pdf", Aspose.Slides.Export.SaveFormat.Pdf);

			using (var pdfDocument = new Document(pdfStream))
			{
				using (var pdfConversionLogOutputStream = new MemoryStream())
				{
					pdfDocument.Convert(pdfConversionLogOutputStream, pdfFormat, ConvertErrorAction.Delete);
					pdfDocument.Save($"c:\\Temp\\Powerpoint_Pdf17_Save_As_Pdf_{pdfFormat.ToString()}_Without_Meta.pdf");
				}

				pdfDocument.Metadata["dc:subject"] = "test";
				pdfDocument.Save($"c:\\Temp\\Powerpoint_Pdf17_Save_As_Pdf_{pdfFormat.ToString()}_With_Meta.pdf");

				return "ok";
			}
		}
	}
	catch (Exception e)
	{
		return e.Message;
	}
}

Here are the generated files:
Generated_Pdf_Files.zip (2,3 MB)

I checked all the generated files with this LINQPad script:

void Main()
{
	var folder = @"C:\temp\";

	string[] pdfFiles = Directory.GetFiles(folder, "*.pdf");

	foreach (var currentPath in pdfFiles)
	{
		currentPath.Dump();
		using (var pdfStream = new MemoryStream(File.ReadAllBytes(currentPath)))
		{
			var documentOld = new Aspose.Pdf.Document(pdfStream);
			ShowDocumentInfo(documentOld);
		}
	}
}

public void ShowDocumentInfo(Aspose.Pdf.Document document)
{
	document.Info.Dump();
	document.Metadata
	.Select(x => new { key = x.Key, value = x.Value.ToString() }).Dump();
}

And I saw, that “dc:subject” was only written in this PDF files:

  • Excel_Pdf17_Save_As_Pdf_PDF_UA_1_With_Meta.pdf
  • Powerpoint_Pdf17_Save_As_Pdf_PDF_UA_1_With_Meta.pdf
  • Word_Pdf17_Save_As_Pdf_PDF_UA_1_With_Meta.pdf
  • Word_Pdf17_Save_As_Pdf_v_1_4_With_Meta.pdf
  • Word_Pdf17_Save_As_Pdf_v_1_5_With_Meta.pdf
  • Word_Pdf17_Save_As_Pdf_v_1_6_With_Meta.pdf
  • Word_Pdf17_Save_As_Pdf_v_1_7_With_Meta.pdf
  • Word_Pdf17_Save_As_Pdf_v_2_0_With_Meta.pdf

Here a screenshot:
image.png (57,9 KB)

And the Pdf files couldn’t saved for this PDF formats:

  • convert Excel to PdfFormat.v_1_4
  • convert Excel to PdfFormat.v_1_5
  • convert Excel to PdfFormat.v_1_6
  • convert Excel to PdfFormat.v_1_7
  • convert Excel to PdfFormat.v_2_0
  • convert PowerPoint to PdfFormat.v_1_4
  • convert PowerPoint to PdfFormat.v_1_5
  • convert PowerPoint to PdfFormat.v_1_6
  • convert PowerPoint to PdfFormat.v_1_7
  • convert PowerPoint to PdfFormat.v_2_0

becuse the ArgumentException was thrown with this message “Unknown prefix. Need to call method RegisterNamespaceURI” and this StackTrace

   at #=zbC94T4TWdbxK94r6gACg_aKU0qk2LRzfFwoFvqQ=.Add(String #=z$m6QZAI=, XmpValue #=z3VP47ak=)
   at Aspose.Pdf.Metadata.set_Item(String key, XmpValue value)
   at UserQuery.SetMetaDataExcel(PdfFormat pdfFormat) in C:\Repos\temp\UserTemp\LINQPad7\_fbnppyta\wrcyad\LINQPadQuery:line 92

This seems to be a bigger problem when I try to add metadata to PDF files, doesn’t it?

Thank you for your help

@jschur

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-58959

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.